Open In App

Spring - MVC Framework

Last Updated : 06 Oct, 2025
Comments
Improve
Suggest changes
21 Likes
Like
Report

Spring MVC follows the Model-View-Controller pattern centered around the Front Controller, DispatcherServlet, which routes all HTTP requests to the appropriate controller. The @Controller annotation marks a class as a controller, and @RequestMapping maps web requests to specific controller methods.

The terms model, view, and controller are defined as follows:

  • Model: The Model encapsulates the application data.
  • View: The View renders the model data and generates HTML output that the client's browser can interpret.
  • Controller: The Controller processes the user requests and passes them to the view for rendering.

Dispatcher Servlet

The DispatcherServlet is the central component of Spring MVC:

  • 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-Framework-Control-flow-Diagram

  • 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 0: Set Up the Project

Create a Maven-based Spring MVC project. Ensure the required folder structure and server configuration are in place.

Step 1: Add dependencies in pom.xml

Include the following dependencies

pom.xml:

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:

Java
@Controller
public class HelloGeek {

    @RequestMapping("/")
    public String display(Model model) {
        model.addAttribute("message", "Spring MVC Tutorial!!");
        return "index";
    }
}

Step 3: Configure web.xml

Specify the DispatcherServlet in the web.xml file:

web.xml:

XML
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_0.xsd" 
         version="3.0">

    <display-name>SpringMVC</display-name>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

</web-app>

Step 4: Define Bean Configuration (spring-servlet.xml)

Place the configuration in WEB-INF to enable component scanning and view resolution:

spring-servlet.xml:

XML
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
                           http://www.springframework.org/schema/mvc 
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.geeksforgeeks.controller"/>
    <mvc:annotation-driven/>

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

</beans>

In this configuration,

  • We have added the necessary XML namespaces and schema locations.
  • <context:component-scan> tells Spring where to look for annotated components.
  • <mvc:annotation-driven> enables Spring MVC annotations.
  • The InternalResourceViewResolver bean is defined to resolve view names. It looks for JSP files in the /WEB-INF/views/ directory and appends .jsp to the view name.

Step 5: Create a JSP File

We will now create a JSP file to display the message.

index.jsp in /WEB-INF/views/:

HTML
<%@ 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.

Explore