Home Spring xmlviewresolver
Questions:Ask|Latest

Related Tutorials


 
 

Share on Google+Share on Google+

xmlviewresolver

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

<servlet-mapping>

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

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

</servlet-mapping>

<welcome-file-list>

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

</welcome-file-list>

<taglib>

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

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

</taglib>

</web-app>

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

<property name="location">

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

</property>

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

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

<bean id="xmlviewresolver"

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

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

</bean>

</beans>

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

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"

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

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

<property name="location">

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

</property>

</bean>

<bean id="urlMapping"

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

<property name="interceptors">

<list>

<ref local="localeChangeInterceptor"/>

</list>

</property>

<property name="urlMap">

<map>

<entry key="/xmlviewresolver.html">

<ref bean="xmlviewresolverController"/>

</entry>

</map>

</property>

</bean>

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

</bean>

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

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

</bean>

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

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

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

pageEncoding="ISO-8859-1"%>

<html>

<head>

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

<title>XML View Resolver Example</title>

</head>

<body>

<center>

<table>

<tr>

<td>

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

</td>

</tr>

</table>

</center>

</body>

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

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:

 

After click on this link we will see like:

 

Download code

Download this example code 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 2, 2011

Related Tutorials

Ask Questions?      

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
jeet
May 17, 2014
Spring 3

form handler example in spring framework with database
DMCA.com