SpringMVC3.2 与 Freemarker 整合小记
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 8 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> 9 10 <!-- Enables the Spring MVC @Controller programming model --> 11 <mvc:annotation-driven/> 12 <context:component-scan base-package="com.liuyu.learn.freemarker.controller" /> 13 <bean id="freemarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" > 14 <property name="configuration" ref="freemarkerConfiguration" /> 15 </bean> 16 17 <bean id="freemarkerConfiguration" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 18 <property name="templateLoaderPaths"> 19 <list> 20 <value>/WEB-INF/ftl</value> 21 </list> 22 </property> 23 <!-- 配置文件,全局设置 --> 24 <property name="configLocation"> 25 <value>classpath:freemarker.properties</value> 26 </property> 27 <!-- 全局变量 --> 28 <property name="freemarkerVariables"> 29 <map> 30 <entry key="base" value="@{base}" /> 31 <entry key="resRoot" value="@{resRoot}" /> 32 <entry key="versionNo" value="@{appVersion}" /> 33 <entry key="buildNo" value="@{buildNo}" /> 34 </map> 35 </property> 36 <property name="defaultEncoding" value="utf-8" /> 37 </bean> 38 39 <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 40 <property name="cache" value="false" /> 41 <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> 42 <property name="viewNames"> 43 <array> 44 <value>*.ftl</value> 45 </array> 46 </property> 47 <!-- Note : 配置了 "viewNames"后一定不要配置 suffix=.ftl ,原因见下述 48 <property name="prefix" value=""/> 49 <property name="suffix" value=".ftl"/> --!> 50 <property name="contentType" value="text/html;charset=UTF-8"></property> 51 <property name="requestContextAttribute" value="request" /> 52 <property name="exposeSpringMacroHelpers" value="true" /> 53 <property name="exposeRequestAttributes" value="true" /> 54 <property name="exposeSessionAttributes" value="true" /> 55 </bean> 56 </beans>
备注:
1) "viewNames" 与 "suffix" 尽量不要同时配置 UrlBasedViewResolver#createView时会判断 viewName是否匹配配置的viewNames,代码段如下:
1 protected boolean canHandle(String viewName, Locale locale) { 2 String[] viewNames = getViewNames(); 3 return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName)); 4 }
在配置suffix的情况下,Controller 返回modelAndView时代码通常如下:
1 return "index"; // 非"index.ftl"
此时, canHandle方法将验证不能过,导致无法找到相应的View.
按上面的配置文件配置时,Controller 返回modelAndView时代码通常如下:
1 return "index.ftl"; // 非"index"
2) 在配置多个 viewResolver 时, 尽量配置viewNames,即按上述代码,以免导致 多个view重叠时,按order来加载view的情况,并会引申问题 3)
3) Spring 视图判断问题: AbstractUrlBasedView#checkResource 判断view对应的资源是否存在时, 代码如下:
1 public boolean checkResource(Locale locale) throws Exception { 2 return true; 3 }
很显然,在配置多个viewResolver 时,一定会出问题..即 有 a.ftl 文件 , 但 jsp viewResolver order更高,即使你是想让Spring匹配a.ftl,但它在匹配viewResolver 时,就已经假设该资源&View一定存在,但 Dispatcher.render view时,就会报错.

浙公网安备 33010602011771号