Spring's ResourceBundleViewResolver
by Abhijeet Kashnia
In a web application, jsp pages could be placed in a folder hierarchy. You could have a public folder that contains corresponding pages, or maybe a user folder. For such a structure, Spring's ResourceBundleViewResolver seems more useful than InternalResourceViewResolver.
The internal resource view resolver needs a hardcoded prefix, which means that your controllers must provide the folder prefix in the code.
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
So, if the controller wants to point to the jsp at WEB-INF/jsp/public/page1.jsp, the controller will need:
ModelAndView mv = new ModelAndView();
mv.setViewName("public/page1");
For this purpose, the ResourceBundleViewResolver seems a better approach.
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
The base name implies that Spring should look for a file called views.properties somewhere on the classpath, and that this file should contain entries like:
<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename" value="views" />
</bean>
The base name implies that Spring should look for a file called views.properties somewhere on the classpath, and that this file should contain entries like:
page1.(class)=org.springframework.web.servlet.view.JstlView
page1.url=/WEB-INF/jsp/public/page1.jsp
Now, the controller just needs to say:
mv.setViewName("page1");
BTW, if you are working on Spring 3.0 onwards, don't forget to put brackets around the "class" in the property file. As per a comment at:
"This is supposed to indicate that you can't use get/set pair to set the class. What is normally specified as an attribute on the bean element needs to be enclosed in a ()"