×
DocsConsole

DocsConsole

Spring5 MVC ResourceBundleViewResolver XMLConfig Example

By | | Updated : 2019-11-13 | Viewed : 532 times

Spring5 MVC ResourceBundleViewResolver XMLConfig Example

The current lesson will explain about the Spring5 MVC ResourceBundleViewResolver XMLConfig Example. ResourceBundleViewResolver will resolve the view name by reading the resource bundle (properties file). We will learn about the configuration also here. Previous lesson (click here) will explain about the Spring5 MVC XmlViewResolver JavaConfig Example.

The current lesson will explain about the Spring5 MVC ResourceBundleViewResolver XMLConfig Example. ResourceBundleViewResolver will resolve the view name by reading the resource bundle (properties file). We will learn about the configuration also here. Previous lesson (click here) will explain about the Spring5 MVC XmlViewResolver JavaConfig Example.

Spring5 MVC ResourceBundleViewResolver XML Configuration

To generate the model control should go to the handler (controller). Please find the below given code for handler.

Code snippet for HomeController.java
@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";

	}
}
Code snippet for spring-dispatcher-servlet.xml
	<bean class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
		<property name="basename" value="views" />
	</bean>

Here configured the ResourceBundleViewResolver with property “basename”. Here property “basename” will be used for finding the name of property file. So property file will be views.properties.

views.properties
home.(class)=org.springframework.web.servlet.view.JstlView

home.url=/WEB-INF/views/home.jsp

If you observe the view.properties file, Spring MVC uses [viewname].(class) as ViewResolver class name and [viewname].(url) as view location. So if you observe the handler method (getHelloMessage) is returning the home. So it will verify the properties file to get [viewname].(class) and [viewname].(url). Here those properties are home.(class) and home.url.

Spring5 MVC ResourceBundleViewResolver XMLConfig Example

Please create the maven project as given below and update with pom.xml.

Create Maven project for Spring5 MVC ResourceBundleViewResolver XMLConfig Example
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-ResourceBundleViewResolver-XMLConfig-Example as Artifact Id 
--> Select packaging option as War
--> Click on Finish
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.docsconsole.tutorials.springmvc5</groupId>
	<artifactId>Spring5-MVC-ResourceBundleViewResolver-XMLConfig-Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Spring5-MVC-ResourceBundleViewResolver-XMLConfig-Example</name>
	<description>Spring5-MVC-ResourceBundleViewResolver-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>

Next we need to create the configuration files web.xml and spring-dispatcher-servlet.xml.

web.xml
<?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>
spring-dispatcher-servlet.xml
<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.ResourceBundleViewResolver">
		<property name="basename" value="views" />
	</bean>
	<context:component-scan base-package="com.docsconsole.tutorials.springmvc5.*" />

</beans>

Now we will write code file for Controller. Please find below given code file for HomeController.java

HomeController.java
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";

	}
}

In views resolution, ResourceBundleViewResolver will use properties file. So we will configure a property file for views resolution.Create views.properties file under resources directory and update file with below given configuration.

views.properties
home.(class)=org.springframework.web.servlet.view.JstlView

home.url=/WEB-INF/views/home.jsp

Write a view file for populating the model in the browser.

home.jsp
<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Spring5 MVC ResourceBundleViewResolver XMLConfig Example</title>
   </head>
   
   <body>
      <h2 style="color: #528396;padding: 20 0 0 40;">${msg}</h2>
   </body>
</html>

The project directory structure will be as given below.

Spring5 MVC ResourceBundleViewResolver XMLConfig Example

When you hit the url “http://localhost:8080/Spring5-MVC-ResourceBundleViewResolver-XMLConfig-Example/getHelloMessage” after deployment, you can see the below given output.

Spring5 MVC ResourceBundleViewResolver XMLConfig Example

Download Source Code Spring5-MVC-ResourceBundleViewResolver-XMLConfig-Example.zip

Leave A Reply