sitemesh和spring MVC装饰器模式问题
问题内容:
我有与spring工作的sitemesh,这是配置:decorator.xml
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/styles">
<excludes>
<pattern>/exclude.jsp</pattern>
<pattern>/exclude/*</pattern>
</excludes>
<decorator page="application/themeManager/theme.jsp" name="dos">
<pattern>/*</pattern>
</decorator>
</decorators>
这是我的 web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- The master configuration file for this Spring web application -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/web-application-config.xml
</param-value>
</context-param>
<!-- Enables Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Agregamos el filtro de sitemesh que permite interceptar todas las llamadas que necesitamos -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<!-- Loads the Spring web application context -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Serves static resource content from .jar files such as spring-faces.jar -->
<servlet>
<servlet-name>Resources Servlet</servlet-name>
<servlet-class>org.springframework.js.resource.ResourceServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<!-- Map all /resources requests to the Resource Servlet for handling -->
<servlet-mapping>
<servlet-name>Resources Servlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这项工作,但是当我在decorator.xml中将模式更改为类似
<decorator page="application/themeManager/theme.jsp" name="dos">
<pattern>/spring/cliente/index</pattern>
</decorator>
它不起作用,我尝试了很多组合,却一无所获。然后像这样在web.xml中更改spring servlet的映射
Spring MVC Dispatcher Servlet * .htm
并定义一个像这样的 新模式 :
<decorator page="application/themeManager/theme.jsp" name="dos">
<pattern>/cliente/index.htm</pattern>
</decorator>
而且它有效,因此有什么方法可以使它与spring servlet的此映射一起使用?
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
问题答案:
问题是SiteMesh使用Request.getServletPath(),在您的Spring MVC应用程序中,该函数将为所有内容返回“ /
spring”。我通过实现com.opensymphony.module.sitemesh.DecoratorMapper接口并代替常规的ConfigDecoratorMapper来找到它。然后,我可以检查用于将装饰器映射到请求的各种参数。不幸的是,我认为这给您留下的唯一选择是在DispatcherServelet映射或其某些变体中使用*
.html后缀。
另一个选择是配置PageDecoratorMapper并在原始未装饰页面中使用此标记来指定要使用的布局:
<meta name="decorator" content="layoutName" />
尽管那样,您便失去了URL映射的好处。