一、从freemarker谈起
Freemarker使用模板技术进行视图的渲染。自从看了Struts标签、Freemarker、JSTL的性能对比后,我毅然决定放弃Struts标签了!效率太差……
spring本身支持了对Freemarker的集成。只需要配置一个针对Freemarker的视图解析器即可。
二、Spring MVC视图解析器
视图解析器的工作流程大致是这样的:Controller的某个方法执行完成以后,返回一个视图(比如:listUser),视图解析器要做的工作就是找到某个对象来完成视图的渲染,或者跳转到其他的逻辑视图。这里的渲染对象通常就是我们的jsp文件或者我们下面用的Freemarker(例如listUser.jsp或者listUser.ftl)。渲染完成以后,将解析结果发送到客户端浏览器
下面介绍一下本文需要用到的解析器(更多解析器资料,请参考 http://e-freya.iteye.com/blog/384083 ):
InternalResourceViewResolver:这是一个最常用的解析器。通常使用它指定渲染对象为jsp页面
FreeMarkerViewResolver:这就是Spring与Freemarker整合需要用到的解析器
三、配置多视图,支持freemarker
我们通常不希望所有的动态页面请求都使用Freemarker来渲染,那就需要配置多个视图解析器。网上有很多这方面的帖子。我看到很多人的做法是在web.xml中配置两个DispatcherServlet,一个拦截*.do,一个拦截*.ftl;然后再写两个dispatcherServlet.xml,配置两个视图解析器;jsp页面、ftl模板就各司其职。
其实没有那么复杂。
1.Web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.4" 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">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <!-- ======================================================== -->
- <!-- Spring MVC Config Servlet -->
- <!-- ======================================================== -->
- <!-- JSP DispatcherServlet -->
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
- </init-param>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <!-- ======================================================== -->
- <!-- Spring MVC Config Mapping -->
- <!-- ======================================================== -->
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- </web-app>
2.dispatcherServlet.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:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
- <!--通用视图解析器-->
- <bean id="viewResolverCommon" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/page/"/>
- <property name="suffix" value=".jsp"/><!--可为空,方便实现自已的依据扩展名来选择视图解释类的逻辑 -->
- <property name="viewClass">
- <value>org.springframework.web.servlet.view.InternalResourceView</value>
- </property>
- <property name="order" value="1"/>
- </bean>
- <!-- 配置freeMarker视图解析器 -->
- <bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
- <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
- <property name="contentType" value="text/html; charset=utf-8"/>
- <property name="cache" value="true" />
- <property name="suffix" value=".ftl" />
- <property name="order" value="0"/>
- </bean>
- <!-- 配置freeMarker的模板路径 -->
- <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
- <property name="templateLoaderPath">
- <value>/WEB-INF/ftl/</value>
- </property>
- <property name="freemarkerVariables">
- <map>
- <entry key="xml_escape" value-ref="fmXmlEscape" />
- </map>
- </property>
- <property name="defaultEncoding">
- <value>utf-8</value>
- </property>
- <property name="freemarkerSettings">
- <props>
- <prop key="template_update_delay">3600</prop>
- </props>
- </property>
- </bean>
- <bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
- <!-- 注解支持 -->
- <mvc:annotation-driven/>
- <!-- 对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
- <context:component-scan base-package="com.hl.usersmanager">
- <!-- 允许定义过滤器将基包下的某些类纳入或排除
- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> -->
- </context:component-scan>
- </beans>
3.Controller
- package com.hl.usersmanager.controller;
- import java.util.List;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.ModelMap;
- import org.springframework.web.bind.annotation.RequestMapping;
- import com.hl.usersmanager.model.Users;
- import com.hl.usersmanager.service.IUserService;
- @Controller
- public class UserController {
- // 使用注解实现自动装配 不需要再写get set方法以及在context中配置bean
- @Autowired
- private IUserService userService;
- @RequestMapping(value = "findUserByName.do")
- public String findUserByName(String name,ModelMap model) {
- Users users = userService.findUserByName(name);
- model.addAttribute("userPhone",users.getPhone());
- System.out.println("userPhone:" + users.getPhone());
- return "showUser";
- }
- @RequestMapping(value = "findAllUsers.do")
- public String findAllUsers(ModelMap model) {
- List<Users> users = userService.findAllUsers();
- model.addAttribute("users",users);
- return "listUser";
- }
- ……
- }