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.

Download Source Code

References

  1. Spring MVC InternalResourceViewResolver example
  2. Spring MVC XmlViewResolver example
  3. Spring MVC ResourceBundleViewResolver example

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
9 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
Abhijit GhoshJack MPandu MbiradarNarayanaVishal Recent comment authors
newest oldest most voted
Abhijit Ghosh
Guest
Abhijit Ghosh

thank you..its working .

Jack M
Guest
Jack M

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

Pandu Mbiradar
Guest
Pandu Mbiradar

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

Narayana
Guest
Narayana

Hi I have used .jsp in VIEW resolver as the same time I want to navigate controller some time what should i DO

Vishal
Guest
Vishal

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

paul
Guest
paul

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!

harish
Guest
harish
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
	<property name="location">
		<value>/WEB-INF/dispatcher-view.xml</value>
	</property>
</bean>
 

in :-dispatcher-view.xml

<bean
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix">
		<value>/WEB-INF/jsp/</value>
	</property>
	<property name="suffix">
		<value>.jsp</value>
	</property>
</bean>
 

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

trackback
Spring MVC Tutorial

[…] in XML file.ResourceBundleViewResolver example Puts view beans in “.properties” file.Configure multiple view resolvers priority If multiple view resolver strategies are applied, you have to declare priority to avoid conflict […]

trackback
[spring-mvc] exception handler ? viewResolver ?? « ??? ????

[…] ???? ??? ?? viewResolver? ???? ??? ?????. (??) ??? ???? order? ????. <bean id="beanNameviewResolver" […]