In previous examples, we learned about XmlViewResolver and InternalResourceViewResolver view templates. In this post, we will learn about ResourceBundleViewResolver templates. ResourceBundleViewResolver
loads view beans from a resource bundle in the classpath root. Note that ResourceBundleViewResolver
can also take advantage of the resource bundle capability to load view beans from different resource bundles for different locales (this is not possible with other two view resolvers).
By default, ResourceBundleViewResolver
loads view names from “views.properties
” file present into classpath but this location can be overridden through the basename property.
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views" /> </bean>
In views.properties
resource bundle, you can declare view beans in the format of properties and their declaration is equivalent to the XML bean declaration which we saw in XmlViewResolver example.
<!--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-3.0.xsd"> <bean id="home" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/jsp/home.jsp" /> </bean> <bean id="admin/home" class="org.springframework.web.servlet.view.JstlView"> <property name="url" value="/WEB-INF/jsp/admin/home.jsp" /> </bean> <bean id="logOffRedirect" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="home" /> </bean> </beans--> //Equivalent resource bundle properites home.(class)=org.springframework.web.servlet.view.JstlView home.url=/WEB-INF/jsp/home.jsp adminHome.(class)=org.springframework.web.servlet.view.JstlView adminHome.url=/WEB-INF/jsp/admin/home.jsp logOffRedirect.(class)=org.springframework.web.servlet.view.RedirectView logOffRedirect.url=home
With above configuration, when controller return a logical view name “home” then
ResourceBundleViewResolver
will find the key starting with “home” inviews.properties
file, and return the corresponding view’s URL i.e. “/WEB-INF/jsp/home.jsp” back to the DispatcherServlet.
Drop me your queries if something needs more explanation.
Happy Learning !!