轉自:http://www.tashan10.com/spring-mvcde-duo-shi-tu-jie-xi-qi-pei-zhi/
一、從freemarker談起
Freemarker使用模板技術進行視圖的渲染。自從看了Struts標簽、Freemarker、JSTL的性能對比后,我毅然決定放棄Struts標簽了!效率太差……
Spring本身支持了對Freemarker的集成。只需要配置一個針對Freemarker的視圖解析器即可。
二、Spring MVC視圖解析器
視圖解析器的工作流程大致是這樣的:
- Controller的某個方法執行完成以后,返回一個視圖(比如:listUser)
- 視圖解析器要做的工作就是找到某個對象來完成視圖的渲染,或者跳轉到其他的邏輯視圖。這里的渲染對象通常就是我們的jsp文件或者我們下面用的Freemarker(例如listUser.jsp或者listUser.ftl)。
- 渲染完成以后,將解析結果發送到客戶端瀏覽器
下面介紹一下本文需要用到的解析器(更多解析器資料):
- 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";
}
……
}
在視圖解析器中有一個<property name="order" value="orderValue"/>
的配置,這個配置表示解析器的優先級別。我們將FreeMarkerViewResolver
的級別設為0
,將InternalResourceViewResolver
的級別設為1
。這樣,解析器就會優先使用 FreeMarkerViewResolver
進行解析,如果找不到相應的模板,就使用InternalResourceViewResolver
進行解析,如果還找不到頁面,就會產生一個404
錯誤!
在本例中,我們在/WEB-INF/page/
下有一個showUser.jsp
頁面,在/WEB-INF/ftl/
下有一個listUser.ftl
的模板文件。那么當訪問findAllUsers.do
的時候,Controller
返回一個listUser
視圖,根據解析器配置,先使用FreeMarkerViewResolver
進行解析。它會根據模板路徑templateLoaderPath
的配置/WEB-INF/ftl/
下去找是否有一個listUser
並且以后綴配置suffix
值.ftl
,即listUser.ftl
文件,如果找到則使用該模板進行解析。這里我們實現已經創建了這個模板文件,所以user列表成功被顯示出來。
當用戶訪問findUserByName.do
的時候,返回showUser
視圖,毅然先使用FreeMarkerViewResolver
進行解析,結果發現在/WEB-INF/ftl/
下並沒有showUser.ftl
這個模板文件,於是使用InternalResourceViewResolver
進行解析,於是開始尋找/WEB-INF/page/
下是否有showUser.jsp
文件。由於我們已經創建了這個文件,於是最終使用showUser.jsp
進行渲染。那么如果沒有找到showUser.jsp
,就會拋出404
錯誤。
這里還要注意的是,如果Controller
中返回視圖加了后綴jsp
或者ftl
,在配置中就不要加入suffix
配置,否則會找不到頁面。