In this section, you will learn about view resolvers in Spring.
In spring, for resolving view, you need to implement some view resolver. The configuration of two most commonly used view resolvers(ResourceBundleViewResolver and InternalResourceViewResolver , declared in the WebApplicationContext) are given below :
<!-- the ResourceBundleViewResolver --> <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"> <property name="basename" value="views"/> </bean>
ResourceBundleViewResolver needs a properties file for defining view names. The property file related to the above configuration is views. properties, which is given below :
home.(class)=org.springframework.web.servlet.view.JstlView home.url=/WEB-INF/jsp/home.jsp employeeList.(class)=org.springframework.web.servlet.view.JstlView employeeList.url=/WEB-INF/jsp/employeelist.jsp
As you can see above, the property file must have mapping for 1) a class 2) a URL.
The ResourceBundleViewResolver can handle various views using only one resolver as shown below :
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean>
You must put your JSP file under 'WEB-INF' to protect it from client's direct access.
Posted on: February 7, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles