大佬教程收集整理的这篇文章主要介绍了具有多个视图解析器的Spring MVC,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我认为你误解了订单优先级。该VIEwResolver
最高顺序是链中最后一个解析器。由于你给的InternalresourceVIEwResolver
订单是0
,因此它将成为链中的第一个解析器,并且InternalresourceVIEwResolver
无论返回什么视图名称,都会解析该视图。因此,如果要使用多个解析器,则InternalresourceVIEwResolver
必须是顺序最高的解析器。
将InternalresourceVIEwResolver
订单值更改为2:
<?xml version="1.0" enCoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_246_11845@a/beans"
xmlns:context="http://www.springframework.org/scheR_246_11845@a/context"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_246_11845@a-instance" xmlns:mvc="http://www.springframework.org/scheR_246_11845@a/mvc"
xsi:scheR_246_11845@aLOCATIOn="
http://www.springframework.org/scheR_246_11845@a/beans
http://www.springframework.org/scheR_246_11845@a/beans/spring-beans.xsd
http://www.springframework.org/scheR_246_11845@a/context
http://www.springframework.org/scheR_246_11845@a/context/spring-context.xsd
http://www.springframework.org/scheR_246_11845@a/mvc
http://www.springframework.org/scheR_246_11845@a/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.evgeni.dfr.controller" />
<context:Annotation-config />
<mvc:Annotation-driven />
<bean ID="vIEwResolver"
class="org.springframework.web.servlet.vIEw.UrlBasedVIEwResolver">
<property name="cache" value="false" />
<property name="vIEwClass" value="com.evgeni.drf.faces.FacesVIEw" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".xhtml" />
<property name="order" value="1" />
</bean>
<bean
class="org.springframework.web.servlet.vIEw.InternalresourceVIEwResolver">
<property name="prefix">
<value>/WEB-INF/vIEws/</value>
</property>
<property name="suffix">
<value>.Jsp</value>
</property>
<property name="order" value="2" />
</bean>
</beans>
编辑:
检查javadoc后,似乎这两个解析程序无法链接,因为InternalresourceVIEwResolver
是UrlBasedVIEwResolver(InternalresourceVIEwResolver
扩展了UrlBasedVIEwResolver
)。两个解析器始终与返回值匹配。我认为你将需要一些自定义功能才能执行此操作。
我尝试使用2个视图解析器:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_246_11845@a/beans"
xmlns:context="http://www.springframework.org/scheR_246_11845@a/context"
xmlns:xsi="http://www.w3.org/2001/XMLscheR_246_11845@a-instance" xmlns:mvc="http://www.springframework.org/scheR_246_11845@a/mvc"
xsi:scheR_246_11845@aLOCATIOn="
http://www.springframework.org/scheR_246_11845@a/beans
http://www.springframework.org/scheR_246_11845@a/beans/spring-beans.xsd
http://www.springframework.org/scheR_246_11845@a/context
http://www.springframework.org/scheR_246_11845@a/context/spring-context.xsd
http://www.springframework.org/scheR_246_11845@a/mvc
http://www.springframework.org/scheR_246_11845@a/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.evgeni.dfr.controller" />
<context:Annotation-config />
<mvc:Annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="cache" value="false" />
<property name="viewClass" value="com.evgeni.drf.faces.FacesView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".xhtml" />
<property name="order" value="1" />
</bean>
<bean
class="org.springframework.web.servlet.view.InternalresourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="0" />
</bean>
</beans>
该应用程序始终仅使用顺序最低的一个,而不使用另一个。在当前情况下,如果我的控制器返回“ someView”,则The requested resource (/MyProject/WEB-INF/views/someView.jsp) is not available.即使存在“ pages / someView.xhtml” ,应用也会响应。
Spring version - 3.2.3
编辑:如果我在控制器中有2个方法,并且methodA返回“ viewA”,而methodB返回“ viewB”。我们在“ views”文件夹中有viewA.jsp,在“ pages”中有viewB.xhtml。
情况1:UrlBasedViewResolver-> order = 1,InternalresourceViewResolver-> order = 2
@H_694_2@methodA->The requested resource (/MyProject/WEB-INF/pages/viewA.xhtml) is not available.;
@H_688_3@methodB -> OK
情况2:UrlBasedViewResolver-> order = 2,InternalresourceViewResolver-> order = 1
方法A->确定;
@H_688_3@methodB -> `The requested resource (/MyProject/WEB-INF/views/viewB.jsp) is not available.`;
以上是大佬教程为你收集整理的具有多个视图解析器的Spring MVC全部内容,希望文章能够帮你解决具有多个视图解析器的Spring MVC所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。