View Resolver In Spring Mvc Java Config
By AmarSivas | | Updated : 2021-03-16 | Viewed : 8842 times

Table of Contents:
We have already learned in previous lesson about XmlViewResolver XMLConfig Example (click here). We will try same example with JavaConfig. Here we will also learn about the java configuration for XmlViewResolver.
We have already learned in previous lesson about XmlViewResolver XMLConfig Example (click here). We will try same example with JavaConfig. Here we will also learn about the java configuration for XmlViewResolver.
Spring5 MVC XmlViewResolver Java Configuration
Java Configuration: WebMvcConfig.java will be used as spring-dispatcher servlet configuration. Please find the below given code snippet for the same.
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.docsconsole.tutorials.springmvc5.*"})
public class WebMvcConfig {
@Autowired
ServletContext servletContext;
@Bean
public XmlViewResolver xmlViewResolver() {
XmlViewResolver xmlViewResolver = new XmlViewResolver();
Resource resource = new FileSystemResource(new File( servletContext.getRealPath("/WEB-INF/spring-views.xml") ));
xmlViewResolver.setLocation(resource);
return xmlViewResolver;
}
}
Here we configured XmlViewResolver by using @Bean. By using FileSystemResource class, spring-views.xml is been configured. Please observed code above given.
Now we need to code for handler (Controller). Please find below given code snippet for the same.
@Controller
public class HomeController {
@RequestMapping(value="/getHelloMessage", method = RequestMethod.GET)
public String getHelloMessage(ModelMap model) {
model.addAttribute("msg", "Spring5 MVC XmlViewResolver JavaConfig Example !!");
return "home";
}
}
Next we need to configure one more xml file (spring-views.xml) for view configuration under WEB-INF directory.
<?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>
After preparation of model to be displayed, controller need to send that model to particular View. In controller, the returned response is string "home" which is same as bean id of the JstlView in spring-views.xml. So the View name will be returned to the controller. This finalized View will be used for preparing the html template.
Spring5 MVC XmlViewResolver JavaConfig 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-JavaConfig-Example as Artifact Id
--> Select packaging option as War
--> Click on Finish
<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-JavaConfig-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Spring5-MVC-XmlViewResolver-JavaConfig-Example</name>
<description>Spring5-MVC-XmlViewResolver-JavaConfig-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 find the below given java files for Servlet configuration and Spring Dispatcher Servlet configuration.
package com.docsconsole.tutorials.springmvc5.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebAppInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebMvcConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
package com.docsconsole.tutorials.springmvc5.config;
import java.io.File;
import javax.servlet.ServletContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.XmlViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.docsconsole.tutorials.springmvc5.*"})
public class WebMvcConfig {
@Autowired
ServletContext servletContext;
@Bean
public XmlViewResolver xmlViewResolver() {
XmlViewResolver xmlViewResolver = new XmlViewResolver();
Resource resource = new FileSystemResource(new File( servletContext.getRealPath("/WEB-INF/spring-views.xml") ));
xmlViewResolver.setLocation(resource);
return xmlViewResolver;
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource source = new ResourceBundleMessageSource();
source.setBasename("messages");
return source;
}
}
To handle the request Handler (Controller) is required. Code which is given below for the same.
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 JavaConfig Example !!");
return "home";
}
}
Next we need to write one xml file for views configuration under WEB-INF directory.
<?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>
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
<head>
<title>Spring5 MVC XmlViewResolver JavaConfig Examplee</title>
</head>
<body>
<h2 style="color: #528396;padding: 20 0 0 40;">${msg}</h2>
</body>
</html>
Please find the below given screenshot for final project architecture.

After deploying the war, you will have to use URL http://localhost:8080/Spring5-MVC-XmlViewResolver-JavaConfig-Example/getHelloMessage. You can see the below given output in the browser.
