0
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

pom.xml
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javamakeuse.springmvc</groupId>
  <artifactId>XmlViewResolverExample</artifactId>
  <version>1.0</version>
  <packaging>war</packaging>
  <name>XmlViewResolverExample</name>
  <description>XmlViewResolver Example</description>
  
  
 <!-- Dependencies -->
 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-web</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>4.1.4.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
   <scope>runtime</scope>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.0.1</version>
   <scope>provided</scope>
  </dependency>
   </dependencies>

 <build>
  <finalName>XmlViewResolverExample</finalName>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

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"
 xmlns="http://java.sun.com/xml/ns/javaee"
 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>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>

 <servlet>
  <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 -->
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>view-resolver</servlet-name>
  <url-pattern>*.htm</url-pattern>
 </servlet-mapping>
</web-app>
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.

views.xml
<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="home"
        class="org.springframework.web.servlet.view.JstlView">
        <property name="url" value="/WEB-INF/pages/xml-home.jsp" />
    </bean>
 
</beans>
Step 5: Create view-resolver-servlet.xml, parallel to web.xml and place the below code.

view-resolver-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 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-4.1.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-4.1.xsd">

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

 <bean id="viewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
   <property name="location">
   <value>/WEB-INF/views.xml</value>
  </property>
 
  </bean>

</beans>  
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.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC XMLViewResolver Example</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>
   Click <a href="xmlViewResolver.htm">here</a> to return another view by using XMLViewResolver.
  </p>
 </div>
</body>
</html>
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.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC XmlViewResolver</title>
</head>
<body>
 <div style="width: 200px; margin: auto;">
  <p>${msg}</p>
 </div>
</body>
</html>
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;

@Controller
public class XMLViewResolverController {

 @RequestMapping(value="xmlViewResolver")
 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

OUT PUT:
URL: http://localhost:8080/XmlViewResolverExample/

Done !!

Download the complete example from here Source Code


Sponsored Links

0 comments:

Post a Comment