Show List

Sample Spring MVC Application

A Spring MVC application is a web application that is built using the Spring MVC framework. The Spring MVC framework provides a flexible and modular architecture for building web applications and allows developers to write the application components in a clear and organized way.

In a Spring MVC application, the controllers are annotated with the @Controller annotation and implement the request handling methods that handle the incoming requests and return the appropriate response. The views can be JSP pages or templates and the model data can be any object that represents the data that needs to be displayed in the view.

In this demo we are going to create a Spring MVC web application.

In your IDE, create a new Maven project and select webapp archetype. Here I am using Eclipse IDE.
Here is the default project structure. 
pom.xml already includes Junit dependency. Add Spring MVC, Spring Web and Servlet dependencies.

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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>springmvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>springmvc Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>


		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.9.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.5</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>



	</dependencies>
	<build>
		<finalName>springmvc</finalName>
	</build>
</project>

web.xml
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

index.jsp
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
The web application is now ready to be run on the server.
Selecting the Tomcat server would launch the application on the browser.

Adding Spring MVC Framework

Currently the application is only accessible at index.jsp which is being displayed through the default routing for the web application.

To add Spring MVC framework, first we are going to add a front controller (Dispatcher servlet) in the web.xml.

Below is the project structure with different files we are going to add.




web.xml: 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
										  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>Archetype Created Web Application</display-name>

	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>


All the incoming requests will now be going through this dispatcher servlet.

Dispatcher servlet makes use of spring bean configuration file spring-servlet.xml to identify the handler controllers for different requests and view resolvers. Here is a sample:

WEB-INF/spring-servlet.xml 
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
	xmlns="http://www.springframework.org/schema/mvc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 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">


	<!-- Finding Controllers -->
	<annotation-driven />
	<context:component-scan base-package="springmvc" />

	<!-- Resolving views selected for data returned by @Controllers -->
	<beans:bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<beans:property name="prefix" value="/WEB-INF/views/" />
		<beans:property name="suffix" value=".jsp" />
	</beans:bean>

</beans:beans>
Adding controller to route the requests

FirstController.java
package springmvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FirstController {
	
	@RequestMapping("/")
	public String displayHomeForDefault(ModelMap  model) {
		model.addAttribute("message", "Welcome Home");
		return "home";
	}
	
	@RequestMapping("/home")
	public String displayHome(ModelMap  model) {
		model.addAttribute("message", "Welcome Home");
		return "home";
	}

	@RequestMapping("/about")
	public String displayAbout(ModelMap model) {
		model.addAttribute("message", "This page is to provide some information about our company");
		return "about";
	}

	@RequestMapping("/help")
	public String displayHelp(ModelMap model) {
		model.addAttribute("message", "This page is to provide help document");
		return "help";
	}
}
Here are the JSPs that have been added in the WEB-INF/views folder.

home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home</title>
</head>
<body>
${message}
</body>
</html>
about.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>About</title>
</head>
<body>

<h2>${message}</h2>

</body>
</html>
help.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page isELIgnored="false"%>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Help</title>
</head>
<body>

<h2>${message}</h2>

</body>
</html>
Now if we run the application on the server, different jsp views can be invoked by changing the url based on the controller paths.
Source Code:

https://github.com/it-code-lab/springmvc

    Leave a Comment


  • captcha text