Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC with multiple view resolvers

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>

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 version - 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'.

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

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

methodB -> OK

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

methodA -> OK ;

methodB -> `The requested resource (/MyProject/WEB-INF/views/viewB.jsp) is not available.`;
like image 920
Evgeni Dimitrov Avatar asked Aug 13 '13 17:08

Evgeni Dimitrov


People also ask

Can we use multiple view resolvers in Spring MVC?

Spring MVC also supports multiple view resolvers to rely on others if one fails, in case of multiple view resolvers we need to mention the order or the priority of those view resolver that we want them to work according to.

What is true regarding using multiple view resolvers in a Spring application?

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.

When chaining more than one view resolver ___ must be the last in the chain?

The InternalResourceViewResolver must always assign with the lowest priority because it will resolve the view whatever view name is returned.


2 Answers

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.

like image 160
Jean-Philippe Bond Avatar answered Sep 29 '22 02:09

Jean-Philippe Bond


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

like image 30
yname Avatar answered Sep 29 '22 01:09

yname