Spring MVC Framework is a Java-based web framework built on the Model-View-Controller design pattern. It is part of the Spring Framework and is used to develop flexible and loosely coupled web applications.
- It uses DispatcherServlet as the front controller to handle all incoming requests and route them to appropriate controllers.
- It separates application logic into Model (data), View (UI), and Controller (request handling) for better organization.
- It supports features like form handling, validation, and RESTful web services for building modern web applications.
Dispatcher Servlet
DispatcherServlet is the central component of Spring MVC that handles all incoming HTTP requests and manages the complete request-response flow. It acts as the front controller and coordinates between controllers and views.
- Acts as the Front Controller for all HTTP requests.
- Determines the appropriate controller to handle a request.
- Dispatches the request to the selected controller.
- Resolves the view using the View Resolver and renders the response.
Spring MVC Flow Diagram
Spring MVC flow is the process where a client request is handled by the DispatcherServlet, routed to a Controller via HandlerMapping, and the response is returned through a ViewResolver and View.

- DispatcherServlet intercepts incoming requests.
- Retrieves the handler mapping and forwards the request to the controller.
- The controller processes the request and returns a ModelAndView object.
- DispatcherServlet uses the view resolver to render the appropriate view.
Steps to Create a Spring MVC Application
Step 1: Set Up the Project
Create a Maven-based Spring MVC project. Ensure the required folder structure and server configuration are in place.
Add the dependencies . Include the following dependencies in pom.xml:
pom.xml:
<dependencies>
<!-- Spring Web MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.30</version>
</dependency>
<!-- Jakarta Servlet API -->
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Step 2: Define the Controller
Create a controller class to handle requests.
HelloGeek.java:
@Controller
public class HelloGeek {
@RequestMapping("/")
public String display(Model model) {
model.addAttribute("message", "Spring MVC Tutorial!!");
return "index";
}
}
Step 3: Configure Java Config
Specify the DispatcherServlet
public class WebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return null;
}
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MyAppConfig.class };
}
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 4: Define Bean Configuration
Place the configuration in WEB-INF to enable component scanning and view resolution:
@Configuration
@EnableWebMvc
@ComponentScan("com.geeksforgeeks.controller")
public class MyAppConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Step 5: Create a JSP File
We will now create a JSP file to display the message.
index.jsp in /WEB-INF/views/:
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>Spring MVC Example</title>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
This JSP file will display the message passed from the controller.
Step 6: Run the Application
Start the server and access the application:
http://localhost:8080/
Output:
Spring MVC Tutorial!!
Advantages of Spring MVC Framework
- Uses a lightweight servlet container for the development and deployment of applications.
- Enables rapid and parallel development.
- Facilitates fast application development.
- Allows multiple developers to work together efficiently.
- Makes updating the application easier.
- Enhances debugging due to multiple levels in the application.
Disadvantages of Spring MVC Framework
- It has high complexity when developing applications using this pattern.
- It is not suitable for small applications as it may affect performance and design.