具有多个视图解析器的Spring MVC

时间:2013-08-13 17:22:09

标签: spring spring-mvc facelets

我尝试使用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="0" />
    </bean>
</beans>

应用程序始终只使用最低顺序而不是另一个。在当前情况下,如果我的控制器返回“someView”,即使有“pages / someView.xhtml”,应用程序也会以The requested resource (/MyProject/WEB-INF/views/someView.jsp) is not available.响应。

Spring版本 - 3.2.3

编辑: 如果我在控制器中有2个方法,则方法A返回“viewA”,方法B返回“viewB”。我们在'views'文件夹中有viewA.jsp,在'pages'中有viewB.xhtml。

案例1:UrlBasedViewResolver - &gt; order = 1,InternalResourceViewResolver - &gt;顺序= 2

方法A - &gt; The requested resource (/MyProject/WEB-INF/pages/viewA.xhtml) is not available.;

methodB -> OK

案例2:UrlBasedViewResolver - &gt; order = 2,InternalResourceViewResolver - &gt;顺序= 1

方法A - &gt;好的;

methodB -> `The requested resource (/MyProject/WEB-INF/views/viewB.jsp) is not available.`;

2 个答案:

答案 0 :(得分:11)

我认为您误解了订单优先顺序。具有最高顺序的ViewResolver是链中的最后一个解析器。由于您为InternalResourceViewResolver提供了0的订单,因此它将成为链中的第一个解析程序,InternalResourceViewResolver将解析视图返回的任何视图名称。因此,如果您需要多个解析器,InternalResourceViewResolver必须是具有最高顺序的解析器。

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后,似乎无法链接这两个解析器,因为InternalResourceViewResolverUrlBasedViewResolver(InternalResourceViewResolver扩展了UrlBasedViewResolver)。两个解析器始终匹配返回的值。我想你需要一些自定义才能做到这一点。

答案 1 :(得分:1)

InternalResourceViewResolver中的Chage顺序从0到1. InternalResourceViewResolver必须具有最大顺序(较低优先级)