问题描述
我尝试使用2个视图解析器:
I tried to use 2 view resolvers:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<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",则即使存在"pages/someView.xhtml",应用程序也会使用The requested resource (/MyProject/WEB-INF/views/someView.jsp) is not available.
进行响应.
The application always uses only the one with the lowest order and not the other. In the current case if my controller return "someView" the app will respond with The requested resource (/MyProject/WEB-INF/views/someView.jsp) is not available.
even if there is "pages/someView.xhtml".
春季版-3.2.3
如果我在控制器中有2个方法,并且methodA返回"viewA",而methodB返回"viewB".我们在"views"文件夹中有viewA.jsp,在"pages"中有viewB.xhtml.
If I have 2 methods in controller and methodA returns "viewA" and methodB returns "viewB". And we have viewA.jsp in 'views' folder and viewB.xhtml in 'pages'.
案例1:UrlBasedViewResolver-> order = 1,InternalResourceViewResolver-> order = 2
Case1: UrlBasedViewResolver -> order=1,InternalResourceViewResolver -> order=2
方法A-> The requested resource (/MyProject/WEB-INF/pages/viewA.xhtml) is not available.
;
methodB -> OK
案例2:UrlBasedViewResolver-> order = 2,InternalResourceViewResolver-> order = 1
Case2: UrlBasedViewResolver -> order=2,InternalResourceViewResolver -> order=1
methodA->确定;
methodA -> OK ;
methodB -> `The requested resource (/MyProject/WEB-INF/views/viewB.jsp) is not available.`;
推荐答案
我认为您误解了订单优先级.顺序最高的ViewResolver
是链中的最后一个解析器.由于您给InternalResourceViewResolver
分配了0
的顺序,因此它将成为链中的第一个解析器,并且InternalResourceViewResolver
将解析该视图,无论返回的视图名称是什么.因此,如果要使用多个解析器,则InternalResourceViewResolver
必须是具有最高顺序的解析器.
I think you misunderstood the order priority. The ViewResolver
with the highest order is the last resolver in the chain. Since you gave the InternalResourceViewResolver
an order of 0
, it will be the first resolver in the chain and the InternalResourceViewResolver
will resolve the view whatever view name is returned. So, if you want multiple resolvers, the InternalResourceViewResolver
must be the resolver with the highest order.
将InternalResourceViewResolver
的订单值更改为2
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<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),因此似乎无法链接这两个解析器.两个解析器始终与返回值匹配.我认为您将需要一些自定义功能才能做到这一点.
After checking the javadoc, it seems that these two resolvers cannot be chained since the InternalResourceViewResolver
is a UrlBasedViewResolver
(InternalResourceViewResolver extends UrlBasedViewResolver). Both resolver always match the returned value. I think you will need something custom to be able to do this.
这篇关于具有多个视图解析器的Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!