xmlviewresolver

xmlviewresolver

xmlviewresolver

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"%>

<html>

<head>

<title>XML View Resolver</title>

</head>

<body>

<center>

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

</center>

</body>

</html>

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"

xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"

version="2.5">

<servlet>

<servlet-name>dispatcher</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet
</
servlet-class>

<load-on-startup>1</load-on-startup>

</servlet> 0

<servlet-mapping>

<servlet-name>dispatcher</servlet-name>

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

</servlet-mapping>

<welcome-file-list>

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

</welcome-file-list>

<taglib>

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

<taglib-location>/WEB-INF/spring.tld</taglib-location>

</taglib>

</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">

<value>/WEB-INF/spring-views.xml</value>

</property> 6

</bean>

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"

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

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

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="xmlviewresolver"

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

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

</bean>

</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

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-2.5.xsd"
2

xmlns:p="http://www.springframework.org/schema/p">

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

<property name="location"> 3

<value>/WEB-INF/spring-views.xml</value>

</property>

</bean> 4

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="interceptors"> 5

<list>

<ref local="localeChangeInterceptor"/>

</list> 6

</property>

<property name="urlMap">

<map> 7

<entry key="/xmlviewresolver.html">

<ref bean="xmlviewresolverController"/>

</entry> 8

</map>

</property>

</bean> 9

<bean id="xmlviewresolverController" class=
"net.roseindia.web.XmlViewResolverController"
>

</bean>

<bean id="localeChangeInterceptor" class=
"org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
> 0

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

</bean>

<bean id="localeResolver" class=
"org.springframework.web.servlet.i18n.SessionLocaleResolver"
/> 1

</beans>

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"

pageEncoding="ISO-8859-1"%>

<html> 3

<head>

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

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

</head>

<body>

<center> 5

<table>

<tr>

<td> 6

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

</td>

</tr> 7

</table>

</center>

</body> 8

</html>

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 {
  @Override
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:

  0

After click on this link we will see like:

 

Download code 1

Download this example code