Configure Multiple View Resolvers in Spring MVC

In my previous tutorial I have explined about content negotiation startegy introduced in Spring MVC 3.2. This tutorial focuses on how to configure multiple view resolvers for your spring web application. We have different view resolvers for different purposes : XmlViewResolver, ResourceBundleViewResolver, and InternalResourceViewResolver.

XmlViewResolver:

An implementation of ViewResolver that accepts a configuration file written in XML. This configuration file contains list of beans defined which will be used as the view names. The default configuration file is /WEB-INF/views.xml. You can change the configuration file name in the spring xml file.

InternalResourceViewResolver:

It is the sub class of UrlBasedViewResolver. Using this resolver we can specify any particular technology and URL to make the view name. It is most commonly used resolver for mapping the simple URLs to the view name and maping the JSP files.

ResourceBundleViewResolver:

If you want to configure your views in the properties files, you can use this resolver. It is not widely used since the most prefered way is XML files.

Look at the below example to understand how to configure multile view resolvers.

ViewResolverController.java

package javabeat.net;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewResolverController {
	@RequestMapping("viewdemo")
	public String getView(){
		return "view";
	}
	@RequestMapping("viewdemoprop")
	public String getViewFromProperty(){
		return "prop";
	}
}

example-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	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


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="javabeat.net" />

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
	     <property name="basename" value="javabeat.net.ViewProps" />
	     <property name="order" value="1"/>
	</bean>

	<bean id="excelView" class="org.springframework.web.servlet.view.XmlViewResolver">
		<property name="location" value="/WEB-INF/views.xml"/>
	</bean>

	<bean id="beanNameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>

</beans>

views.xml


<?xml version="1.0" encoding="UTF-8"?>
<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="view" class="org.springframework.web.servlet.view.JstlView">
		<property name="url" value="/WEB-INF/jsp/view.jsp" />
	</bean>
	<bean name="report" class="org.springframework.example.ReportExcelView" />
</beans>

ViewProps.properties

prop.(class)=org.springframework.web.servlet.view.JstlView
prop.url=/WEB-INF/jsp/prop.jsp

If you have any questions on understanding the above example, please write it in the comments.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*