程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了具有多个视图解析器的Spring MVC大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决具有多个视图解析器的Spring MVC?

开发过程中遇到具有多个视图解析器的Spring MVC的问题如何解决?下面主要结合日常开发的经验,给出你关于具有多个视图解析器的Spring MVC的解决方法建议,希望对你解决具有多个视图解析器的Spring MVC有所启发或帮助;

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

InternalresourceVIEwResolver订单值更改为2:

<?xml version="1.0" enCoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_246_11845@a/beans"
    xmlns:context="http://www.springframework.org/scheR_246_11845@a/context"
    xmlns:xsi="http://www.w3.org/2001/XMLscheR_246_11845@a-instance" xmlns:mvc="http://www.springframework.org/scheR_246_11845@a/mvc"
    xsi:scheR_246_11845@aLOCATIOn="
        http://www.springframework.org/scheR_246_11845@a/beans     
        http://www.springframework.org/scheR_246_11845@a/beans/spring-beans.xsd
        http://www.springframework.org/scheR_246_11845@a/context 
        http://www.springframework.org/scheR_246_11845@a/context/spring-context.xsd
        http://www.springframework.org/scheR_246_11845@a/mvc
        http://www.springframework.org/scheR_246_11845@a/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)。两个解析器始终与返回值匹配。我认为你将需要一些自定义功能才能执行此操作。

解决方法

我尝试使用2个视图解析器:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/scheR_246_11845@a/beans"
    xmlns:context="http://www.springframework.org/scheR_246_11845@a/context"
    xmlns:xsi="http://www.w3.org/2001/XMLscheR_246_11845@a-instance" xmlns:mvc="http://www.springframework.org/scheR_246_11845@a/mvc"
    xsi:scheR_246_11845@aLOCATIOn="
        http://www.springframework.org/scheR_246_11845@a/beans     
        http://www.springframework.org/scheR_246_11845@a/beans/spring-beans.xsd
        http://www.springframework.org/scheR_246_11845@a/context 
        http://www.springframework.org/scheR_246_11845@a/context/spring-context.xsd
        http://www.springframework.org/scheR_246_11845@a/mvc
        http://www.springframework.org/scheR_246_11845@a/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”,则The requested resource (/MyProject/WEB-INF/views/someView.jsp) is not available.即使存在“ pages / someView.xhtml” ,应用也会响应。

Spring version - 3.2.3

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

情况1:UrlBasedViewResolver-> order = 1,InternalresourceViewResolver-> order = 2

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

@H_688_3@methodB -> OK

情况2:UrlBasedViewResolver-> order = 2,InternalresourceViewResolver-> order = 1

方法A->确定;

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

大佬总结

以上是大佬教程为你收集整理的具有多个视图解析器的Spring MVC全部内容,希望文章能够帮你解决具有多个视图解析器的Spring MVC所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。