标签:style blog http io color ar 使用 sp for
先说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>
<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>
最后,发现碰到问题,要多思考,多想,多debug,了解清楚,一步一个脚印。
springMvc sitemesh freemarker 整合总结
标签:style blog http io color ar 使用 sp for
原文地址:http://www.cnblogs.com/flatychen/p/4088583.html