【问题标题】:Spring MVC with multiple view resolvers具有多个视图解析器的 Spring MVC
【发布时间】:2013-08-14 03:16:58
【问题描述】:

我尝试使用 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. 响应。

春季版 - 3.2.3

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

案例1: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

方法A -> 确定;

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

【问题讨论】:

    标签: spring spring-mvc facelets


    【解决方案1】:

    我认为您误解了订单优先级。顺序最高的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)。两个解析器总是匹配返回的值。我认为你需要一些定制的东西才能做到这一点。

    【讨论】:

    • 检查我的编辑。你的用例是什么。为什么要使用这两个 UrlBasedViewResolver。
    【解决方案2】:

    将 InternalResourceViewResolver 中的顺序从 0 更改为 1。InternalResourceViewResolver 必须具有最大顺序(较低优先级)

    【讨论】:

    • 正如我所解释的,只有低阶视图解析器可用。如果我这样做,该应用程序将始终在 'pages' 中搜索带有后缀 = xhtml 的视图,如果在那里找不到,则会抛出错误...
    猜你喜欢
    • 2011-01-18
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2015-03-11
    • 2012-04-15
    • 2016-01-30
    相关资源
    最近更新 更多