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

12 comments on “Spring MVC ResourceBundleViewResolver example

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

    Reply
  2. 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"/>
    	
    	<!--  register the bean -->
    	<bean class="common.controller.WelcomeController"/>
    	
    	<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
    		<property name="basename" value="spring-views"/>
    	</bean>
    	
    </beans>	
    

    is in the WEB-INF folder along with the web.xml :

    <web-app id="WebApp_ID" version="2.4" 
    	xmlns="http://java.sun.com/xml/ns/j2ee" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    	
      	<display-name>ResourceBundleViewResolverExample</display-name>
      
      	<servlet>
      		<servlet-name>mvc-dispatcher</servlet-name>
        	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        	<load-on-startup>1</load-on-startup>
      	</servlet>
      
      	<servlet-mapping>
     		<servlet-name>mvc-dispatcher</servlet-name>
        	<url-pattern>*.htm</url-pattern>
      	</servlet-mapping>
    
        <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
      	</context-param>
      
      	<listener>
        	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      	</listener>
      
    </web-app>
    

    When I run this in eclipse I get a 404 no matter what combination of addresses I type in the url bar. I have successfully run all the tutorials before this so the issue is not jars. I dont know what I am supposed to do to pull up the page. If I move the jsp to just inside the WebContent folder, I can just type WelcomePage.jsp right after /projectname/ after running and it will load just fine but thats not what the tutorial is set to accomplish. Please help.

    Reply
    1. 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

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

        Reply
  3. 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”?

    Reply
  4. 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

    Reply
      1. Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.mkyong.common.controller.WelcomeController] for bean with name ‘com.mkyong.common.controller.WelcomeController#0’ defined in ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: com.mkyong.common.controller.WelcomeController
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1141)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:524)
        at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1177)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:222)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:202)
        at org.springframework.context.support.AbstractApplicationContext.getBeanNamesForType(AbstractApplicationContext.java:933)
        at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.detectHandlers(AbstractDetectingUrlHandlerMapping.java:72)
        at org.springframework.web.servlet.handler.AbstractDetectingUrlHandlerMapping.initApplicationContext(AbstractDetectingUrlHandlerMapping.java:57)
        at org.springframework.context.support.ApplicationObjectSupport.initApplicationContext(ApplicationObjectSupport.java:119)
        at org.springframework.web.context.support.WebApplicationObjectSupport.initApplicationContext(WebApplicationObjectSupport.java:69)
        at org.springframework.context.support.ApplicationObjectSupport.setApplicationContext(ApplicationObjectSupport.java:73)
        at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:70)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:350)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1331)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
        … 22 more
        Caused by: java.lang.ClassNotFoundException: com.mkyong.common.controller.WelcomeController
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
        at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
        at org.springframework.util.ClassUtils.forName(ClassUtils.java:211)
        at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:385)
        at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1138)
        … 36 more

        Reply

Leave a Comment

Your email address will not be published. Required fields are marked *