Spring MVC with multiple views

I tried using 2 view permissions:

<?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> 

An application always uses only the one that has the smallest order, and not the other. In the current case, if my controller returns "someView", the application 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 the controller and method A returns "viewA" and methodB returns "viewB". And we have viewA.jsp in the 'views' folder and viewB.xhtml in the '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.`; 
+6
source share
2 answers

I think you misunderstood the priority of the order. ViewResolver highest order ViewResolver is the last resolver in the chain. Since you gave the InternalResourceViewResolver order 0 , this will be the first resolver in the chain, and the InternalResourceViewResolver allow the view in which any view name will be displayed. So, if you want multiple resolvers, InternalResourceViewResolver should be the highest order converter.

Change the order value of InternalResourceViewResolver 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 bound, because InternalResourceViewResolver is UrlBasedViewResolver (InternalResourceViewResolver extends UrlBasedViewResolver). Both resolvers always match the return value. I think you will need something ordinary to do this.

+11
source

The Chage order in InternalResourceViewResolver is from 0 to 1. InternalResourceViewResolver must have the highest order (lower priority)

+1
source

Source: https://habr.com/ru/post/951665/


All Articles