XmlViewResolver Example


XmlViewResolver Example in Spring 2.5 MVC

Example of XmlViewResolver in Spring 2.5 MVC.

The XmlViewResolver offers us some basic functionality to retrieve views from a BeanFactory.  The XmlViewResolver implements ViewResolver interface that uses XML file to define the bean definations and  specified by resource location. The project contain this file inside the WEB-INF directory, the default name of the XML file is views.xml. The XmlViewResolver are not support Locale so if you want to used local in your project then use XmlViewResolver. In this example, we will discuss about how to use XmlViewResolver.

Step 1:

Now we will create a index.jsp file that will have a link to generate user request. The code of the index.jsp is:

<%@page contentType="text/html" pageEncoding="UTF-8"%>



<title>XML View Resolver</title>




<a href="xmlviewresolver.html">XML View Resolver</a>




Step 2:

Now we will customize the the web.xml file for the DispatcherServlet and set index.jsp as a welcome file . The code of the web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"










</servlet> 0



<url-pattern>*.html</url-pattern> 1



<welcome-file>index.jsp</welcome-file> 2



<taglib-uri>/spring</taglib-uri> 3



</web-app> 4

Step 3:

Now we will create dispatcher-servlet.xml file inside the /WEB-INF/ directory that will have configuration code for the project. We will also configure the code for XmlViewResolver in this file. The code is:

<bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver"> 5

<property name="location">


</property> 6


In this code we have set bean id as "viewResolver" and set the class for this bean that indicate, the "XmlViewResolver" are used here. We have set the location property for this bean and provide value for this property as "/WEB/spring-views.xml". The spring-views.xml file is that file which is contain the actual view name. The code of the spring-views.xml file for this project is:

<?xml version="1.0" encoding="UTF-8"?> 7

<beans xmlns="http://www.springframework.org/schema/beans"


xsi:schemaLocation="http://www.springframework.org/schema/beans 8


<bean id="xmlviewresolver"

class="org.springframework.web.servlet.view.JstlView"> 9

<property name="url" value="/WEB-INF/jsp/xmlviewresolver.jsp" />


</beans> 0

The url property is provide the actual actual view for the particular response. The full code of the dispatcher-servlet.xml is:

<?xml version="1.0" encoding="UTF-8"?>

<beans 1



xsi:schemaLocation="http://www.springframework.org/schema/beans http:


<bean id="viewResolver" class=

<property name="location"> 3



</bean> 4

<bean id="urlMapping"


<property name="interceptors"> 5


<ref local="localeChangeInterceptor"/>

</list> 6


<property name="urlMap">

<map> 7

<entry key="/xmlviewresolver.html">

<ref bean="xmlviewresolverController"/>

</entry> 8



</bean> 9

<bean id="xmlviewresolverController" class=


<bean id="localeChangeInterceptor" class=
> 0

<property name="paramName" value="hl"/>


<bean id="localeResolver" class=
/> 1


Step 4:

Now we will create xmlviewresolver.jsp file inside the /WEB-INF/jsp/  folder. This file display as response when call by the viewResolver. The code of the xmlviewresolver.jsp is: 2

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"


<html> 3


<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>XML View Resolver Example</title> 4



<center> 5



<td> 6

<b>This is XML View Resolver Example.</b>


</tr> 7



</body> 8


Step 5:

Now we will create a controller class to control the user request and return ModelAndView as the response. In this class we will implements the AbstractController. We will create this class inside the src folder  and the code of the  XmlViewResolverController.java is: 9

package net.roseindia.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class XmlViewResolverController extends AbstractController {
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse responsethrows Exception {
String Mess = "XmlViewResolver Example";
System.out.println("------>" + Mess);
ModelAndView modelAndView = new ModelAndView("xmlviewresolver");  
 return modelAndView;

Step 6: 

Now we will run this application and see the output like:


After click on this link we will see like:


Download code 1

Download this example code