Table of contents
1.
Introduction
2.
What is Spring MVC?
3.
The Architecture of Spring MVC Framework
3.1.
Model
3.2.
View
3.3.
Controller
4.
The Flow of Spring MVC Framework
4.1.
Request
4.2.
Dispatcher Servlet
4.3.
Controller
4.4.
Model and View
4.5.
View Resolver
5.
Advantages of Spring MVC Framework
6.
Disadvantages of Spring MVC Framework
7.
Creating The First Spring MVC Web Application
7.1.
index.jsp
7.2.
about.jsp
7.3.
Output:
8.
Frequently Asked Questions
8.1.
Is spring MVC still being used?
8.2.
Which is better, spring boot or spring MVC?
8.3.
Why is spring MVC better than servlets?
8.4.
Is REST API a microservice?
8.5.
Why is MVC used in Java?
9.
Conclusion
Last Updated: Mar 27, 2024
Easy

Introduction to Spring MVC & Flow

Author Amit Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you worked on Spring Boot? Do you want to know what Spring MVC is?

Introduction to spring mvc & flow spring

Then you have come to the right place. This article is focused on an important concept known as Spring MVC. We will learn about its architecture. We will also learn the flow of Spring MVC with advantages and disadvantages. Finally, we will try to create a Web Application as an example. Let's dive into the article to learn more about the topic.

Read About, Interpolation in Angular

What is Spring MVC?

Spring MVC is a tool for building web applications using Java. It separates the application into three parts: the data, the way it looks, and the control between them. 

  • The data is the model,
     
  • the way it looks is the view, and
     
  • the controller handles the control between them.
     

DispatcherServlet is a crucial part of this framework. It manages and directs incoming requests to the right place in the application. The framework includes other useful features, such as Dependency Injection and Inversion of Control.

The Architecture of Spring MVC Framework

Now, let's move on to the architecture of the Spring MVC

architecture

Model

  • It represents the data used in the application.
     
  • It is the part of the application containing all the data and logic.
     
  • It has no instructions for displaying data to the user.
     
  • It is responsible for the logic and rules of the application.
     

View

  • It creates the GUI or graphical user interface.
     
  • It is usually made using Jakarta Server Page and JSP Standard Tag Library.
     
  • You can use it to display the information to the user. The information is present in the model.
     

Controller

  • It is the part of the application that controls the data flow.
     
  • It works on both the model and the view.
     
  • It manages the application flow. It also handles the update of the view whenever data changes.
     

The Flow of Spring MVC Framework

Let's try to understand the flow of the Spring MVC. The flow starts from the request and ends with the view.

flow

Request

A user requests the website. It goes to the DispatcherServlet in Spring MVC.

Dispatcher Servlet

The DispatcherServlet serves as the front controller. It determines which controller should handle the request based on the URL and other request parameters.

Controller

The controller chosen takes the request. It gets or updates data using the model and chooses a view to handle the request.

Model and View

The view takes the data and creates the response.

View Resolver

The DispatcherServlet sends the response back to the user. It also ensures that data flow between the model and view is done correctly and handles any errors.

Advantages of Spring MVC Framework

Now, let's see what the advantages of Spring MVC are.

  1. It uses a lightweight container for creating and deploying web applications.
     
  2. It offers various options to configure the framework and application classes. This makes it easy to reference them in different contexts. 
     
  3. It offers convenient testing options and the ability to use annotations for redirecting pages, providing flexibility in mapping.
     
  4. It will enable rapid and parallel development and encourages the reuse of existing business objects. 
     
  5. It has built-in support for form handling, validation, and data binding.
     

Disadvantages of Spring MVC Framework

After the advantages, let's see what the disadvantages of Spring MVC are.

  1. It can be challenging to set up and configure, especially for beginners.
     
  2. It may experience performance issues when handling high traffic or large amounts of data.
     
  3. It can be overbearing for small projects, as it may include more functionality than is needed.
     
  4. It can be difficult to troubleshoot and debug issues as the framework is large and complex.
     
  5. It requires developers to invest time and effort to become proficient as it has a steeper learning curve.
     

Creating The First Spring MVC Web Application

Let's create a web application using Spring MVC. The name of the Web Application is "springmvc." 

Step 1: You can create and set up a project in Eclipse IDE. The project structure will be as follows:
 

structure

 

Step 2: The next step is to add the dependencies in the "pom.xml" file. The "pom.xml" file will look like this.

<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.spring.mvc</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>
  	<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
	<dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-webmvc</artifactId>
    	<version>5.2.4.RELEASE</version>
	</dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>springmvc</finalName>
    <plugins>
		<plugin>
    		<groupId>org.apache.maven.plugins</groupId>
    		<artifactId>maven-war-plugin</artifactId>
    		<version>3.3.1</version>
		</plugin>
		<plugin>
      		<groupId>org.apache.maven.plugins</groupId>
      		<artifactId>maven-compiler-plugin</artifactId>
      		<version>3.8.1</version>
		</plugin>
	</plugins>
  </build>
</project>
You can also try this code with Online Java Compiler
Run Code

 

Must Read Apache Server

Step 3: Now, we can create a Controller class named "TestController.java," which will handle the Requests.

package springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

	@RequestMapping("/home")
	public String homepage() {
		System.out.println("This is the HomePage.");
		return "index";
	}

	@RequestMapping("/about")
	public String aboutus() {
		System.out.println("This is the About us page.");
		return "about";
	}

}
You can also try this code with Online Java Compiler
Run Code

 

Step 4: We will create a "springmvc-servlet.xml" file. The content of the file will look like this.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 
	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="springmvc.controller">
	</context:component-scan>

	<bean 
		class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
		name="viewResolver">

		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

 

Step 5: We will modify the content of the "web.xml" file in the WEB-INF folder. The "web.xml" will look like this.

<!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>
  
  <!-- Configuration of dispatcher servlet. -->
  
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

 

Step 6: We will create a "views" folder to create the views. We will create ".jsp" files as the views of the project. We have created two files.

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Home Page</title>
	</head>
	<body>
		<h1>Home Page</h1>
		<h1>TestController made the call.</h1>
		<h1>URL: /home</h1>
	</body>
</html>

about.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>About Page</title>
	</head>
	<body>
		<h1>About Me</h1>
		<h2>Learn Coding with CodingNinjas</h2>
		<h3>Writer: Shadow</h3>
	</body>
</html>

Output:

URL: http://localhost:8080/springmvc/home
output

 

URL: http://localhost:8080/springmvc/about
output

You can also check out What is Servlet here.

Frequently Asked Questions

Is spring MVC still being used?

You can create a web application using Spring by using Spring MVC.

Which is better, spring boot or spring MVC?

Spring MVC makes it simple to create applications. Spring Boot's starters and auto-configuration capabilities make it simple and rapid to construct applications. Since Spring Boot handles all tasks connected to dependencies, development time is reduced.

Why is spring MVC better than servlets?

The advantage of Spring MVC is that after you've bootstrapped the application context and the database connection, it becomes easy to create new controllers. It also adheres to a much more logical architecture, which novice developers may find easier as they become more accustomed.

Is REST API a microservice?

REST APIs act as the glue or the bridge that connects these several microservices, which are the building parts of your application and carry out various tasks.

Why is MVC used in Java?

Model-view-controller or MVC is the name of a methodology or design pattern for successfully and effectively connecting the user interface to underlying data models in object-oriented computer development.

Conclusion

So that's the end of the article. 

After reading about the introduction to Spring MVC and flow, Are you interested in reading/exploring more themes on Spring MVC? Don't worry; Coding Ninjas has you covered.

Check out some articles on Spring MVC like spring-vs-spring-boot-vs-spring-mvc and top-spring-mvc-interview-questions.

However, if you want to give your work an edge over the competition, you might choose to enroll in one of our premium courses.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available; take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Merry Learning!

Live masterclass