先说web.xml,配置如下:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/spring/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class> <async-supported>true</async-supported> </filter> <!-- 编码--> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!—sitemesh-->
<filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!—springmvc --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/spring/springMvc/spring_mvc_base.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!—freemarker 配置-->
<servlet> <servlet-name>sitemesh-freemarker</servlet-name> <servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class> <init-param> <param-name>TemplatePath</param-name> <param-value>/</param-value> </init-param> <init-param> <param-name>default_encoding</param-name> <param-value>utf-8</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!—freemarker 页面配置 -->
<servlet-mapping> <servlet-name>sitemesh-freemarker</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping>
spring mvc 以及spring配置无需多讲;
在这里,有两个地方;
HttpServletRequest request = (HttpServletRequest) rq; HttpServletResponse response = (HttpServletResponse) rs; ServletContext servletContext = filterConfig.getServletContext(); SiteMeshWebAppContext webAppContext = new SiteMeshWebAppContext(request, response, servletContext); // ....省略很多.. try { // 得到由mvc框架等渲染完成的页面 Content content = obtainContent(contentProcessor, webAppContext, request, response, chain); if (content == null) { request.setAttribute(ALREADY_APPLIED_KEY, null); return; } // 进行装饰 Decorator decorator = decoratorSelector.selectDecorator(content, webAppContext); decorator.render(content, webAppContext);
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="configLocation" value="/WEB-INF/freemarker.properties" /> <!-- 使用freemarker自动导入功能,主要包含一些基础的配置,如时间格工等 --> <property name="templateLoaderPath" value="/WEB-INF/" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="cache" value="false" /> <property name="prefix" value="" /> <property name="contentType" value="text/html;charset=UTF-8" /> <property name="suffix" value=".htm" /> </bean>在springmvc的配置中,freemarkerconfig的配置其实与上面FreemarkerDecoratorServlet 是类似的,但又有区别; springMvc配置中的freemarker是用于spring mvc页面渲染页面所有,而由于在sitemesh的filter中后期,这个渲染是结束了的。故在装饰器中,springmvc里面配置的freemarker不起做用。起作用是web.xml配置 FreemarkerDecoratorServlet 。 而且这个问题在struts 是不会出现的。
<filter> <filter-name>struts-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class></filter> <filter> <filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class></filter> <filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class></filter>我个人目前简单觉得,struts 基于filter,分离出prepare 和 execute两个场景,形分而意不分,使插件十分容易扩展且又充分的解藕。这点springmvc的确比不上。再加上struts官方有sitemesh插件支持, 且sitemesh执行完之后,简单从filter来看,仍是处理struts的执行链中,却没有脱离。 因为这一点,我在装饰器犯了非常多的错。主要便是一些宏没法使用,但又明明自动引入了。
最后,发现碰到问题,要多思考,多想,多debug,了解清楚,一步一个脚印。
新闻热点
疑难解答