Hello viewers ! Today, I learnt about Spring MVC framework which is much used widely. We have three important parameters in any web application-
- Model: Application data represented as POJO/Bean class.
- View: Front-end showing data after execution of business logic
- Controller: Which actually performs the business operations
Spring MVC allows the loose coupling between them and allows to manage them independently.
As, I have discussed the advantages of using Maven this blog, so I will tell you how to integrate both of them i.e. Spring MVC and Maven.
I have told about this in my previous blog. Just go through that.
- Add the required dependencies in pom.xml
You need to include three dependencies- Spring core, Spring web, Spring webmvc. Go to https://mvnrepository.com/ and add the dependencies for three of them in pom.xml.
Second thing, Maven allows you to specify the version of any dependency in separate tag . You can specify the version variable name as I have specified as and used this in required dependencies as a variable. This gives a great advantage of changing the version for new release at single place rather than going to every dependency and modifying. Cool! 😎
<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.mitaly</groupId>
<artifactId>SpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring-version>4.3.9.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVC</finalName>
</build>
</project>
- Check the project structure
Once you have created the Maven project, see the project structure. If your project structure is something like the shown below then you need to modify this.

Actually, Java Resources here only contains src/main/resources and Libraries. Though it should contain src/main/java where we create our beans and controller.
If you have this problem, go through this stackoverflow answer. Then project structure will look something like this.

Create the index page where you will write the code which needs to be submitted to Controller which will process the data.
See, the action is hello. This is actually the pattern which you specify for particular controller method which needs to be called. We’ll see that later.
<html>
<body>
<h2>Spring MVC</h2>
<form action="hello" method="post">
Enter Name
<input type="text" name="name">
Enter Message
<input type="text" name="message">
<input type="submit" value="Enter">
</form>
</body>
</html>
- Create the controller class ‘HelloWorld.java’
As previously we had Servlets for receiving the user data and process that. Now, we have a simple class which acts as Controller. Use the @Controller annotation for that.
Define the @RequestMapping annotation for the business method to be executed. Here, we provide the mapping name to be /hello. Remember we used the form action to be hello. This means when form is submitted this method is executed.
Through @RequestParam we are getting the form input fields’ value.
The method must return ModelAndView object. In this object we specify all the data that needs to be transferred to view helloworld.jsp. We passed this view name in constructor of ModelAndView but we need not to specify .jsp, the reason for this we’ll see later.
package com.mitaly;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloWorld {
@RequestMapping("/hello")
public ModelAndView showMessage(
@RequestParam(value = "name", required = false, defaultValue = "World") String name,
@RequestParam(value = "message", required = false, defaultValue = "Message") String message) {
ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
- Create the view file ‘helloworld.jsp’
Create a folder named views, though it can be named anything. Create a jsp file and the ModelAndView object’s data.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
Welcome, ${name}
Message : ${message}
</body>
</html>
Even if we don’ create any Servlet, a servlet known as DispatcherServlet gets created when application is loaded. We provide the entry for this servlet in web.xml file. This servlet class is responsible for invoking the required controller class or more specifically which business method in the controller class.
An xml file named dispatcher-servlet.xml is created in WEB-INF directory whose entry has to be specified in web.xml as context parameter.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
- Create and configure the ‘dispatcher-servlet.xml’
Here , we need to specify context:component-scan which is nothing but the package name where DispatcherServlet will search for the required Controller.
There’s entry of InternalResourceViewResolver class which is a view resolver whose task is to pick and show the desired view. We here, specify prefix of view file as directory in which it is contained and suffix as .jsp. Remember, at the time of specifying the view name in ModelAndView constructor, there was no need of specifying .jsp because in this file we specify the suffix as .jsp.
<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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.mitaly" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
This was all till now, but not the end 😉 . Now, I have to merge all the concepts like Hibernate, JDBC, AOP, Services etc. in this.