Spring Mvc Xmlviewresolver Example
By AmarSivas | | Updated : 2021-03-15 | Viewed : 8327 times

Table of Contents:
In this current lesson, we will write and execute example application for XmlViewResolver. We will also learn configuration of the XmlViewResolver in Spring5 MVC. As we discussed earlier about XmlViewResolver (click here), which allows us to configured the views in the XML.
In this current lesson, we will write and execute example application for XmlViewResolver. We will also learn configuration of the XmlViewResolver in Spring5 MVC. As we discussed earlier about XmlViewResolver (click here), which allows us to configured the views in the XML.
Spring5 MVC XmlViewResolver XML Configuration
Configuration: We will learn about the configuration of XmlViewResolver here.
Please find the code snippet for controller.Here it is returning the string “home” as response.
@Controller
public class HomeController {
@RequestMapping(value="/getHelloMessage", method = RequestMethod.GET)
public String getHelloMessage(ModelMap model) {
model.addAttribute("msg", "Spring5 MVC XmlViewResolver XMLConfig Example !!");
return "home";
}
}
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
</bean>
In the code snippet of spring-dispatcher-servlet.xml, we have configured XmlViewResolver as spring bean and configured property “location” with value “/WEB-INF/spring-views.xml”. Please find below given code snippet for spring-views.xml.
<bean id="home" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/views/home.jsp" />
</bean>
If you observe, JstlView bean id in spring-views.xml and the return value in HomeController are same. So resulted view is home.jsp where html template will be prepared.
Spring5 MVC XmlViewResolver XMLConfig Example
Requirements
-
Spring5 Web MVC
-
Java 1.8+
-
Eclipse 4.8.0
-
Servlet 4.0
-
apache-tomcat-9.0.12
Click on File tab
--> New
--> Click on Maven Project
--> Please check on Create Simple Project (Skip architype selection)
--> Click on Next --> Enter the values com.docsconsole.tutorials.springmvc5 as Group Id, Spring5-MVC-XmlViewResolver-XMLConfig-Example as Artifact Id
--> Select packaging option as War
--> Click on Finish
Please find the updated pom.xml file given below.
<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.docsconsole.tutorials.springmvc5</groupId>
<artifactId>Spring5-MVC-XmlViewResolver-XMLConfig-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring5-MVC-XmlViewResolver-XMLConfig-Example</name>
<description>Spring5-MVC-XmlViewResolver-XMLConfig-Example</description>
<properties>
<springframework.version>5.1.2.RELEASE</springframework.version>
<maven.war.plugin.version>3.2.2</maven.war.plugin.version>
<servlets.version>4.0.0</servlets.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.2.2</jstl.version>
<tld.version>1.1.2</tld.version>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring core dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring MVC dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlets.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${tld.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven.war.plugin.version}</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>${project.artifactId}</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Please create web.xml and spring-dispatcher-servlet.xml as given below under WEB-INF directory.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0">
<display-name>Basic Example for Spring5 MVC</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:context = "http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
</bean>
<context:component-scan base-package="com.docsconsole.tutorials.springmvc5.*" />
</beans>
To handle the request, we need to write the one Controller class. Please write code for HomeController.java as given below.
package com.docsconsole.tutorials.springmvc5.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class HomeController {
@RequestMapping(value = "/getHelloMessage", method = RequestMethod.GET)
public String getHelloMessage(ModelMap model) {
model.addAttribute("msg", "Spring5 MVC XmlViewResolver XMLConfig Example !!");
return "home";
}
}
To display results as view we need to create home.jsp. Please find below given code snippets for the same.
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Spring5-MVC-XmlViewResolver-XMLConfig-Example</title>
</head>
<body>
<h2 style="color: #528396;padding: 20 0 0 40;">${msg}</h2>
</body>
</html>
spring-views.xml is the xml where we can configure the views in this xml.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="home" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/views/home.jsp" />
</bean>
</beans>
Please find the below given screenshot for final project architecture.

We will create war by using maven and then will deploy the war in tomcat server. When we fire the URL “http://localhost:8080/Spring5-MVC-XmlViewResolver-XMLConfig-Example/getHelloMessage” then we can see below given output.

Please refer to the Github repository here Spring5-MVC-XmlViewResolver-XML-Example-App