具有多个视图解析器的 Spring MVC [英] Spring MVC with multiple view resolvers

查看:16
本文介绍了具有多个视图解析器的 Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 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",应用程序将响应 请求的资源 (/MyProject/WEB-INF/views/someView.jsp) 不可用. 即使有pages"/someView.xhtml".

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

Spring 版本 - 3.2.3

Spring version - 3.2.3

如果控制器中有 2 个方法,方法 A 返回viewA",方法 B 返回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

methodA -> 请求的资源(/MyProject/WEB-INF/pages/viewA.xhtml)不可用.;

methodA -> 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 :

Change the InternalResourceViewResolver order value to 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 extends 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆