Sponsored Links

Ad by Google
In our previous post we have seen an example of ResourceBundleViewResolver, and now here we are going to show you a very simple example of XmlViewResolver. There are three different implementation of ViewResolver provided by Spring MVC and those are listed here.

What is XmlViewResolver?

The XmlViewResolver is an implementation of ViewResolver interface and uses bean definitions from the dedicated xml file, the default name of the XML file is views.xml and default location of the views.xml file is in class path. You can map your view name with views inside views.xml file, but XmlViewResolver will not support internationalization.

Let's start implementation by following below given simple steps.

Tools and Technologies we are using here:

  • JDK 7
  • Spring 4.1.1
  • Eclipse Kepler 4.3
  • Maven 3.2
Overview of the Project Structure:

Main Objects of this project are:
  • pom.xml
  • web.xml
  • views.xml
  • view-resolver-servlet.xml
  • XMLViewResolverController.java

Below are the list of dependencies required for this project:
  • spring-core
  • spring-web
  • spring-webmvc
  • jstl
  • javax.servlet-api

Step 1. Create a Maven Project:
Step A: Go to File->New->Other..

Step B: Select Maven Project from the select wizard.

Step C: Select project name and location from New Maven Project wizard.
Note: The below screen shot is from our previous, so change here the name of the project.

Step D: Configure project, provide GroupId, artifactId, project name etc. See the screenshot we have selecting packaging type as war because we are going to develop web project. Again this screen shot is from our previous post, so change here GroupId, artifactId, project name etc.

After completion of all the above steps now your project will look like this screen shot.

2. Add project dependencies into pom.xml file:
Double click on your project's pom.xml file, it will open pom.xml file having very limited information. Add required Spring dependencies into pom.xml, below is the complete entry of pom.xml file

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <description>XmlViewResolver Example</description>
 <!-- Dependencies -->


Step 3: Create WEB-INF folder inside webapp, and create web.xml inside WEB-INF folder.

Here is a complete web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 id="WebApp_ID" version="3.0">
 <display-name>Spring MVC XmlViewResolver</display-name>

  <servlet-name>view-resolver</servlet-name>    <!-- Servlet name can be anything -->
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <!-- Known as front controller in spring -->
In web.xml we used DispatcherServlet, DispatcherServlet is known as front controller in Spring MVC. You can read more about front controller from one of our previous post Spring front controller.

Step 4: Create views.xml file and place inside class path.

<beans xmlns="http://www.springframework.org/schema/beans"
    <bean id="home"
        <property name="url" value="/WEB-INF/pages/xml-home.jsp" />
Step 5: Create view-resolver-servlet.xml, parallel to web.xml and place the below code.

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

 <context:component-scan base-package="com.javamakeuse.springmvc" />

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

Step 6: Create index.jsp Right click on webapp folder then New->JSP File and provide name as index.jsp

Here is a complete index.jsp code place this code in your index.jsp page.
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC XMLViewResolver Example</title>
 <div style="width: 200px; margin: auto;">
   Click <a href="xmlViewResolver.htm">here</a> to return another view by using XMLViewResolver.
Step 7: Create a new folder named it as pages inside webapp->WEB-INF->pages and create another jsp file xml-home.jsp inside this folder.

Here is a complete xml-home.jsp file.
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC XmlViewResolver</title>
 <div style="width: 200px; margin: auto;">
Step 8: Create XMLViewResolverController.java
package com.javamakeuse.springmvc;

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

public class XMLViewResolverController {

 public ModelAndView getMessageForXmlViewResolver(){
  String msg = "This message is @ xml-home.jsp page";
  return new ModelAndView("home","msg",msg);

Finally Right click on Project and select Run As->Run On Server

URL: http://localhost:8080/XmlViewResolverExample/

Done !!

Download the complete example from here Source Code

Sponsored Links


Post a Comment