Spring MVC ResourceBundleViewResolver example

In Spring MVC, ResourceBundleViewResolver is used to resolve “view named” based on view beans in “.properties” file.

By default, ResourceBundleViewResolver will loads the view beans from file views.properties, which located at the root of the project class path. However, this location can be overridden through the “basename” property, for example,


<beans ...>
	<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
		<property name="basename" value="spring-views" />
	</bean>
</beans>

In above case, it loads the view beans from “spring-views.properties“, which located at the root of the project class path.

Note
The ResourceBundleViewResolver has the ability to load view beans from different resource bundles for different locales, but this use case is rarely required.

ResourceBundleViewResolver example to show you how it works :

1. Controller

A controller class, return a view, named “WelcomePage“.


//...
public class WelcomeController extends AbstractController{
	
	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		ModelAndView model = new ModelAndView("WelcomePage");
		
		return model;
	}
}

2. ResourceBundleViewResolver

Register ResourceBundleViewResolver in the Spring’s bean configuration file, change the default view beans location to “spring-views.properties“.


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

  <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
	
	<!-- Register the bean -->
	<bean class="com.mkyong.common.controller.WelcomeController" />

	<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
		<property name="basename" value="spring-views" />
	</bean>

</beans>

3. View beans

Declare each view bean as a normal resource bundle style (key & message), where

  1. WelcomePage” is the view name to match.
  2. .(class)” is the type of view.
  3. .url” is the view’s URL location.

File : spring-views.properties


WelcomePage.(class)=org.springframework.web.servlet.view.JstlView
WelcomePage.url=/WEB-INF/pages/WelcomePage.jsp
Note
Put this “spring-views.properties” file on your project class path.
How it works ?
When view name “WelcomPage” is returned by controller, the ResourceBundleViewResolver will find the key start with “WelcomPage” in “spring-views.properties” file, and return the corresponds view’s URL “/WEB-INF/pages/WelcomPage.jsp” back to the DispatcherServlet.

Download Source Code

Reference

  1. ResourceBundleViewResolver documentation

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
6 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
8 Comment authors
nupColin HilbertTreeman CheungSyed SuhaibKristen Duley Recent comment authors
newest oldest most voted
nup
Guest
nup

what will happen if i dont provide .url property in view.properties file ?

Colin Hilbert
Guest
Colin Hilbert

I have the downloaded project set up, word for word, with WelcomeController.java: package common.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.AbstractController; public class WelcomeController extends AbstractController{ @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView model = new ModelAndView("WelcomePage"); return model; } } this file is in the common.controller package; My spring-views.properties file: WelcomePage.(class)=org.springframework.web.servlet.view.JstlView WelcomePage.url=/WEB-INF/pages/WelcomePage.jsp this is in the src folder My WelcomePage.jsp: <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> <html> <body> <h1>Spring MVC ResourceBundleViewResolver example</h1> </body> </html> this is in the /WEB-INF/pages/ folder my mvc-dispatcher-servlet.xml : <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <!--… Read more »

Colin Hilbert
Guest
Colin Hilbert

sorry about the extra imports in the controller (controller and request mapping) those are left over from me trying different things to make this work

Colin Hilbert
Guest
Colin Hilbert

ok I figured it out the call to the WelcomePage in the url has to be all lowercase:

http://localhost/PROJECTNAME/welcomepage.htm not WelcomePage.htm or any of the other hundred combinations that I tried before that….SMH

Treeman Cheung
Guest
Treeman Cheung

Could you tell me how to access the test page?

Treeman Cheung
Guest
Treeman Cheung

OK, I found it, thx a lot~

Colin Hilbert
Guest
Colin Hilbert

I wish you would post i, I cannot figure it out to save my own life

Syed Suhaib
Guest
Syed Suhaib

Can you please explain how control flows(the way you have mentioned above in “How it Works” section) when we are using Tiles. we have resourcebundleviewresolver, tiles-def.xml, views.properties, internalresourceviewresolver, I want to understand how control flows once view name is returned from controller.

Ex:

I) tiles-def has a template with 1) header 2)left-nav 3) body 4) footer (all will invoke different controller)

II) views.properties has mapping to all jsp

III) controller returns a view name.

Also, as mentioned above in views.properties url is the complete path as shown below
WelcomePage.url=/WEB-INF/pages/WelcomePage.jsp
Is this required when we are using both “resourcebundleviewresolver” and “internalresourceviewresolver”?

Kristen Duley
Guest
Kristen Duley

I truly appreciate this post.Thanks Again. Fantastic.

reos
Guest
reos

I got this error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘WelcomePage’: Instantiation of bean failed; nested exception is java.lang.IllegalStateException: No bean class specified on bean definition