• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Configure Multiple View Resolvers in Spring MVC

October 26, 2013 //  by Krishna Srinivasan//  Leave a Comment

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.

Category: Spring FrameworkTag: Spring Framework, Spring MVC

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.

Previous Post: « How to use Spring HATEOAS LinkBuilder API with Spring REST?
Next Post: Difference Between @Resource, @Autowired and @Inject in Spring Injection »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact