標籤:
今天自己配置了一下SpringMVC 的多視圖,本以為很簡單,實踐後發現各種問題,在網上查了很多資料,最後還是選擇了看源碼,終於知道為什麼失敗了,下面介紹一下.
失敗配置! 成功只是改了幾個小地方.
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"xmlns:task="http://www.springframework.org/schema/task"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <context:component-scan base-package="com.controllers,com.services" /><mvc:annotation-driven enable-matrix-variables="true" /> <!-- 設定JSP的設定檔路徑 --><bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="viewNames" value="*.jsp" /> <property name="prefix" value="/WEB-INF/classes/com/jsp"/> <property name="suffix" value=".jsp"/> <property name="order" value="1"/> </bean> <!-- 設定freeMarker的設定檔路徑 --><bean id="freemarkerConfiguration"class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="location" value="classpath:freemarker.properties"/></bean><!-- 配置freeMarker的模板路徑 --><bean id="freemarkerConfig"class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"><property name="freemarkerSettings" ref="freemarkerConfiguration"/><property name="templateLoaderPath" value="classpath:com/jsp"/><property name="freemarkerVariables"><map><entry key="xml_escape" value-ref="fmXmlEscape" /></map></property></bean><bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape" /><!-- 配置freeMarker視圖解析器 --><bean id="viewResolver"class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"><property name="viewNames" value="*.ftl" /><property name="contentType" value="text/html; charset=utf-8" /><property name="cache" value="true" /><property name="prefix" value=""/> <property name="suffix" value=".ftl"/> <property name="order" value="0"/> <property name="exposeRequestAttributes" value="true" /><property name="allowSessionOverride" value="true" /><property name="exposeSessionAttributes" value="true" /><property name="exposeSpringMacroHelpers" value="true" /></bean></beans>
以上是我在網上搜找到的大部分配置,問題出在,以jsp配置為例:
<property name="viewNames" value="*.jsp" /> <property name="suffix" value=".jsp"/> <property name="order" value="1"/>
第一:有一部分人說order屬性不管用,我在看源碼debug時發現是有用的,他會指定使用哪一個配置進行建立視圖,(數字越小優先順序越高),例如:你的項目大部分是jsp很少一部分是ftl或其他視圖,沒有特別要求的話肯定要jsp優先順序別高一些,這樣他會直接匹配jsp視圖,匹配成功後就不會在去找ftl視圖了.
下面進入正題,也是出問題的地方,
viewNames:屬性代表你在return 視圖的名稱時.檔案名稱必須帶尾碼,這樣spring回去判斷是否是以.jsp結尾,
假如說你確實是返回的檔案名稱+尾碼名,但是suffix:屬性會在建立視圖前幫你加上尾碼.jsp,這樣spring就幫你又加了一遍.jsp,這肯定最後是找不到檔案的會異常.
部分源碼:
public static boolean simpleMatch(String pattern, String str) {if (pattern == null || str == null) {return false;}int firstIndex = pattern.indexOf(‘*‘);if (firstIndex == -1) {return pattern.equals(str);}if (firstIndex == 0) {if (pattern.length() == 1) {return true;}int nextIndex = pattern.indexOf(‘*‘, firstIndex + 1);if (nextIndex == -1) {return str.endsWith(pattern.substring(1));}String part = pattern.substring(1, nextIndex);int partIndex = str.indexOf(part);while (partIndex != -1) {if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {return true;}partIndex = str.indexOf(part, partIndex + 1);}return false;}return (str.length() >= firstIndex &&pattern.substring(0, firstIndex).equals(str.substring(0, firstIndex)) &&simpleMatch(pattern.substring(firstIndex), str.substring(firstIndex)));}
正確配置是
<property name="viewNames" value="*.jsp" /> <property name="suffix" value=""/>
java:
return "/jsp.jsp";
如果說返回時不帶尾碼名,
<property name="viewNames" value="" /> <property name="suffix" value=".jsp"/>
java:
return "/jsp";
不知道這麼說大家會不會明白,這2個屬性不能都設定,spring後自動幫你找到你要的視圖,也不用重新實現ViewResolver介面,有特殊情況的可以實現自己的邏輯,
稍後上傳項目
SpringMVC配置多視圖JSP+freemarker,實踐成功!(網上好多坑)