首页 > 代码库 > SpringMVC+Freemarker环境搭建
SpringMVC+Freemarker环境搭建
一:web.xml文件配置
<servlet> </servlet-mapping> |
添加编码过滤器
<filter> <filter-name>encoding</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <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-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
logback集成到SpringMVC项目中
<context-param> <param-name>logbackConfigLocation</param-name> <param-value>classpath:logback.xml</param-value> </context-param> <listener> <listener-class>ch.qos.logback.ext.spring.web.LogbackConfigListener</listener-class> </listener> |
二:spring-mvc.xml文件配置
<context:component-scan base-package="com.ultrapower.bjd.web"> </context:component-scan> |
<mvc:default-servlet-handler /> |
配置freeMarker的模板配置信息
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPaths"> <array> <value>/WEB-INF/freemarker/</value> <value>/WEB-INF/freemarker/mgr/</value> <value>/WEB-INF/freemarker/anls/</value> </array> </property> <property name="freemarkerSettings"> <props> <prop key="incompatible_improvements">2.3.22</prop> <prop key="template_exception_handler">rethrow</prop> <prop key="default_encoding">UTF-8</prop> <prop key="locale">zh_CN</prop> <prop key="date_format">yyyy-MM-dd</prop> <prop key="time_format">HH:mm:ss</prop> <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop> <prop key="number_format">0.################</prop> </props> </property> </bean> |
配置Freemarker视图解析器
<bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="prefix" value="" /> <property name="suffix" value="http://www.mamicode.com/.html" /> <property name="contentType" value="http://www.mamicode.com/text/html;charset=UTF-8" /> <property name="order" value="http://www.mamicode.com/0"></property> <property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.freemarker.FreeMarkerView" /> </bean> |
配置其他视图解析器(处理JSP类型的试图解析器 处理的视图为:InternalResourceView或者JstlView)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="" /> <property name="suffix" value="http://www.mamicode.com/.jsp" /> <property name="contentType" value="http://www.mamicode.com/text/html;charset=UTF-8" /> <property name="order" value="http://www.mamicode.com/1"></property> </bean> |
配置全局的拦截器
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.ultrapower.bjd.web.interceptor.UserVilitorInterceptor" /> </mvc:interceptor> </mvc:interceptors> |
其它(使用FastJson)
<mvc:annotation-driven > class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> |
三:使用
@Controller @RequestMapping(value = http://www.mamicode.com/{"**/{page}.html", "**/{page}.xhtml"}) @ResponseBody |
public class ExcelXlsView extends AbstractXlsView{
@Override HttpServletRequest request, response.setContentType("application/vnd.ms-excel;charset=utf-8"); + URLEncoder.encode(fileName, "UTF-8") + ".xls"); } } 参考: |
部分Maven配置
<properties> </properties> </dependencies> |
<build> <finalName>guest</finalName> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> <include>**/*.conf</include> <include>**/*.drl</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5</version> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> <pluginManagement> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.2</version> <configuration> <server>tomcat6</server> <path>/guest</path> <charset>UTF-8</charset> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <server>tomcat7</server> <path>/guest</path> <charset>UTF-8</charset> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </pluginManagement> </build> |
本文出自 “因简单而自在” 博客,请务必保留此出处http://dengshuangfu.blog.51cto.com/8794650/1908370
SpringMVC+Freemarker环境搭建