Spring MVC with multiple view resolvers

前端 未结 2 947
栀梦
栀梦 2020-12-03 06:15

I tried to use 2 view resolvers:




        
相关标签:
2条回答
  • 2020-12-03 06:51

    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.

    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>
    

    EDIT :

    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.

    0 讨论(0)
  • 2020-12-03 07:02

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

    0 讨论(0)
提交回复
热议问题