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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18215402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:09:22  来源:igfitidea点击:

Spring MVC with multiple view resolvers

springspring-mvcfacelets

提问by Evgeni Dimitrov

I tried to use 2 view resolvers:

我尝试使用 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>

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".

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

Spring version - 3.2.3

春季版本 - 3.2.3

Edit: 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'.

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

Case1: UrlBasedViewResolver -> order=1,InternalResourceViewResolver -> order=2

Case1: UrlBasedViewResolver -> order=1,InternalResourceViewResolver -> order=2

methodA -> The requested resource (/MyProject/WEB-INF/pages/viewA.xhtml) is not available.;

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

methodB -> OK

Case2: UrlBasedViewResolver -> order=2,InternalResourceViewResolver -> order=1

Case2: UrlBasedViewResolver -> order=2,InternalResourceViewResolver -> order=1

methodA -> OK ;

方法A->确定;

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

回答by Jean-Philippe Bond

I think you misunderstood the order priority. The ViewResolverwith the highest order is the last resolver in the chain. Since you gave the InternalResourceViewResolveran order of 0, it will be the first resolver in the chain and the InternalResourceViewResolverwill resolve the view whatever view name is returned. So, if you want multiple resolvers, the InternalResourceViewResolvermust be the resolver with the highest order.

我认为您误解了订单优先级。该ViewResolver最高顺序是链中最后一个解析器。由于您给出了InternalResourceViewResolver的命令0,它将成为链中的第一个解析器,并且InternalResourceViewResolver无论返回什么视图名称,它都将解析视图。因此,如果您想要多个解析器,则InternalResourceViewResolver必须是顺序最高的解析器。

Change the InternalResourceViewResolverorder value to 2:

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>

EDIT :

编辑 :

After checking the javadoc, it seems that these two resolvers cannot be chained since the InternalResourceViewResolveris 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.

检查javadoc后,这两个解析器似乎无法链接,因为它InternalResourceViewResolver是一个UrlBasedViewResolver(InternalResourceViewResolver extends UrlBasedViewResolver)。两个解析器始终匹配返回值。我认为你需要一些定制的东西才能做到这一点。

回答by yname

Chage order in InternalResourceViewResolver from 0 to 1. InternalResourceViewResolver must have largest order (lower priority)

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