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.
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
- “WelcomePage” is the view name to match.
- “.(class)” is the type of view.
- “.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
Put this “
spring-views.properties
” file on your project class path.
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.
what will happen if i dont provide .url property in view.properties file ?
I have the downloaded project set up, word for word, with WelcomeController.java:
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:
this is in the /WEB-INF/pages/ folder
my mvc-dispatcher-servlet.xml :
is in the WEB-INF folder along with the web.xml :
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.
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
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
Could you tell me how to access the test page?
OK, I found it, thx a lot~
I wish you would post i, I cannot figure it out to save my own life
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”?
I truly appreciate this post.Thanks Again. Fantastic.
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
Please post your last caused by error message
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