加入freemark.jar包,在web-inf下面写一个ftldpt-servlet.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!-- 自动扫描 -->
<context:component-scan base-package="com.pacific.product.controller" />
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 总错误处理 -->
<bean id="customResolver"
class="com.pacific.product.custom.exception.CustomSimpleMappingExceptionResolver"></bean>
<!-- 资源管理 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/upload/" mapping="/upload/**" />
<mvc:resources location="/source/" mapping="/source/**" />
<!-- 上传文件解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485670" /> <!-- 10M -->
</bean>
<!-- freemarker的配置 -->
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />
<property name="defaultEncoding" value="utf-8" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">10</prop>
<prop key="locale">zh_CN</prop>
<prop key="datetime_format">yyyy-MM-dd</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="number_format">#.##</prop>
</props>
</property>
</bean>
<!-- FreeMarker视图解析 如返回student。。在这里配置后缀名ftl和视图解析器。。 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"></property>
<property name="suffix" value=".ftl" />
<property name="contentType" value="text/html;charset=utf-8" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<property name="exposeSpringMacroHelpers" value="true" />
</bean>
</beans>
在spring-mvc.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
<!-- 自动扫描 -->
<context:component-scan base-package="com.pacific.product.controller" />
<!-- 注解驱动 -->
<mvc:annotation-driven />
<!-- 总错误处理 -->
<bean id="customResolver"
class="com.pacific.product.custom.exception.CustomSimpleMappingExceptionResolver"></bean>
<!-- 资源管理 -->
<mvc:resources location="/resources/" mapping="/resources/**" />
<mvc:resources location="/upload/" mapping="/upload/**" />
<mvc:resources location="/source/" mapping="/source/**" />
<!-- 上传文件解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10485670" /> <!-- 10M -->
</bean>
<!-- 内部资源视图解析器 prefix + logicName + suffix /WEB-INF/jsps/ + index + .jsp -->
<bean id="internalResourceViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
上面两个文件分别定义了不同的模板引擎
第二步,在web.xml文件配一下,何时使用哪一个引擎
在web.xml文件中加入如下内容
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- freemark -->
<servlet>
<servlet-name>ftldpt</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- freemark mapping -->
<servlet-mapping>
<servlet-name>ftldpt</servlet-name>
<url-pattern>*.ftl</url-pattern>
</servlet-mapping>
从上面的内容来来看,当以.do结尾的就用jsp引擎,当使用ftl结尾的仿问就用freemark来处理
测试如下:
@Controller
@RequestMapping("/freemark")
public class FreemarkTestController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("userName","lin");
return "index";
}
}
上面是一个controller,当仿问index时,会通过不同的结尾调用不同的模板引擎
项目中尽量使用freemark这种静态模板,如果页面修改比较多时,每一次修改,jsp都会编译一下比较慢,而freemark不会。另外使用freemark可以方便实现页面静态化,因为它的输出不一定是servlet容器,而可以是file文件。