今天自己配置了一下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接口,有特殊情况的可以实现自己的逻辑,
稍后上传项目
时间: 2024-10-11 20:42:15