Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
7 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Abhijit Ghosh
8 years ago

thank you..its working .

Jack M
9 years ago

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
9 years ago

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
10 years ago

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

Vishal
10 years ago

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
10 years ago

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
12 years ago
<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 ???