Configure multiple view resolvers priority in Spring MVC
Problem
In Spring MVC application, often times, you may applying few view resolver strategies to resolve the view name. For example, combine three view resolvers together : InternalResourceViewResolver, ResourceBundleViewResolver and XmlViewResolver.
<beans ...>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
</bean>
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="spring-views" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
But, if a view name is returned, which view resolver strategy will be used?
Solution
If multiple view resolver strategies are applied, you have to declare the priority through “order” property, where the lower order value has a higher priority, for example :
<beans ...>
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
<property name="order" value="0" />
</bean>
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="spring-views" />
<property name="order" value="1" />
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<property name="order" value="2" />
</bean>
</beans>
Now, if a view name is returned, the view resolving strategy works in the following order :
XmlViewResolver --> ResourceBundleViewResolver --> InternalResourceViewResolver
Note
The InternalResourceViewResolver must always assign with the lowest priority (largest order number), because it will resolve the view no matter what view name is returned. It caused other view resolvers have no chance to resolve the view if they have lower priority.
The InternalResourceViewResolver must always assign with the lowest priority (largest order number), because it will resolve the view no matter what view name is returned. It caused other view resolvers have no chance to resolve the view if they have lower priority.
Download Source Code
Download it – SpringMVC-ViewResolver-Priority-Example.zip (7KB)
thank you..its working .
I’m also facing the same problem… I have views in multiple folders under root folder Web Pages/Views. Sub folders are /Web Pages/Views/Home and /Web Pages/Views/Product..
How do I resolve these views using “InternalResourceViewResolver”???
Hi Sir , I want to use the order to my XMLViewResolver , i have the requirement where i have money rules which is in XML format i want read those rules in order way using spring like
/WEB-INF/dispatcher-view.xml
/WEB-INF/spring-views.xml
is this way is possible that can we order only in the form of xmlviewresolver ,Can you -please help me on this
Thanks
Hi I have used .jsp in VIEW resolver as the same time I want to navigate controller some time what should i DO
Hi, if I want to use multiple prefix for different folder structure like /WEF-INF/admin/*.jsp and /WEF-INF/user/*.jsp for same resolver “InternalResourceViewResolver” then??
Thank you again! Think I’ve lost count of the number of times I was stuck with something and then your tutorials offered a solution.
Cheers!
in :-dispatcher-view.xml
i mentioned these details in my web app i got an error for every request for a jsp.
IF i write some thing below in dispatcher-view.xml it ll work .
<pre lang="xml"
<property name="url" value="/WEB-INF/jsp/reservationQuery.jsp" />
</bean>
<bean id="showReservation" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/jsp/showReservation.jsp" />
</bean>
i wants to provide InternalResourceViewResolver in XmlViewResolver xml file.How to do it ???