今天本身配置了一下SpringMVC 的多視圖,本覺得很簡單,實踐後發現各類問題,在網上查了不少資料,最後仍是選擇了看源碼,終於知道爲何失敗了,下面介紹一下.html
失敗配置! 成功只是改了幾個小地方.java
<?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配置爲例:
git
<property name="viewNames" value="*.jsp" /> <property name="suffix" value=".jsp"/> <property name="order" value="1"/>
第一:有一部分人說order屬性無論用,我在看源碼debug時發現是有用的,他會指定使用哪個配置進行建立視圖,(數字越小優先級越高),例如:你的項目大部分是jsp不多一部分是ftl或其餘視圖,沒有特別要求的話確定要jsp優先級別高一些,這樣他會直接匹配jsp視圖,匹配成功後就不會在去找ftl視圖了.web
下面進入正題,也是出問題的地方, spring
viewNames:屬性表明你在return 視圖的名稱時.文件名必須帶後綴,這樣spring回去判斷是不是以.jsp結尾,apache
假如說你確實是返回的文件名+後綴名,可是suffix:屬性會在建立視圖前幫你加上後綴.jsp,這樣spring就幫你又加了一遍.jsp,這確定最後是找不到文件的會異常.spring-mvc
部分源碼:tomcat
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))); }
正確配置是
mvc
<property name="viewNames" value="*.jsp" /> <property name="suffix" value=""/>
java:
jsp
return "/jsp.jsp";
若是說返回時不帶後綴名,
<property name="viewNames" value="" /> <property name="suffix" value=".jsp"/>
java:
return "/jsp";
不知道這麼說你們會不會明白,這2個屬性不能都設置,spring後自動幫你找到你要的視圖,也不用從新實現ViewResolver接口,有特殊狀況的能夠實現本身的邏輯,
http://yunpan.cn/cjBvaI3ehdRQR 提取碼 26d0 想使用git的不大會用 22端口被封,先這麼用吧360雲盤 直接tomcat打包