Spring5 MVC HelloWorld XMLConfig Example

By | | Updated : 2018-11-28 | Viewed : 464 times

Spring5 MVC HelloWorld XMLConfig Example

In this current lesson, we are going learn about the Spring5 Web MVC HelloWorld example.

    Requirements

  • Spring5 Web MVC

  • Java 1.8+

  • Eclipse 4.8.0

  • Servlet 4.0

  • apache-tomcat-9.0.12

As we are using the Maven as build management tool, we will create a maven project in eclipse as given below.

Create Maven project for Spring5 MVC HelloWorld 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-HelloWorld-XMLConfig-Example as Artifact Id 
--> Click on Finish

Once project created successfully, we need to replace the pom.xml with below given 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.docsconsole.tutorials.springmvc5</groupId>
	<artifactId>Spring5-MVC-HelloWorld-XMLConfig-Example</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>Spring5-MVC-HelloWorld-XMLConfig-Example</name>
	<description>Basic Example for Spring5 MVC with XML</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 is creating the web.xml file. Here have to create the WEB-INF directory. The web.xml should be in WEB-INF directory. Please follow the below given steps to create the web.xml file.

Creation of web.xml
Right click on WEB-INF directory
--> New 
--> Click on Other 
--> select XML File 
--> Click Next 
--> Enter value for File Name web.xml

Please update web.xml file with below given code.

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>

Now we need to create the spring-dispatcher-servlet.xml. Please follow the below given steps to create the spring-dispatcher-servlet.xml file.

Creation of spring-dispatcher-servlet.xml
Right click on WEB-INF directory
--> New 
--> Click on Other 
--> select XML File 
--> Click Next 
--> Enter value for File Name spring-dispatcher-servlet.xml
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">

   <context:component-scan base-package = "com.docsconsole.tutorials.springmvc5.*" />

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/views/" />
      <property name = "suffix" value = ".jsp" />
   </bean>

</beans>

Please create a HomeController.java class for handling the request. Follow below given steps to create java file in eclipse.

Creation of HomeController.
Right click on src/main/java 
--> New 
--> Click on Class 
--> Enter value for Package as com.docsconsole.tutorials.springmvc5.conntroller and Enter value for Name HomeController.

Please find below given code for MainController class.

MainController.java
package com.docsconsole.tutorials.springmvc5.conntroller;

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", "Hello Spring5MVC World !!");
		return "home";
		
	}
}

To display the result we have to create jsp page. Below given steps are for creating the home.jsp. As we configured /WEB-INF/views/ “” for prefix, all jsp pages should be placed in Views folder.

Creation of home.jsp

Right click on views 
--> New 
--> Click on Other 
--> JSP File (under Web)
--> Click on Next 
--> Enter value for Filename as home.jsp and  and click finish

Please update below given code for home.jsp

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

After creation of the project the directory will be as given below.

Spring5-MVC-HelloWorld-XMLConfig-Example

Please create war and deploy in tomcat server. Once it deployed please type below given URL

Please create war and deploy in tomcat application server. Once it deployed please type below given URL "http://localhost:8080/Spring5-MVC-HelloWorld-XMLConfig-Example/getHelloMessage" then you can see the below given output.

Spring5 MVC HelloWorld Example
Leave A Reply