Spring MVC Framework (original) (raw)

Last Updated : 10 Mar, 2025

The **Spring MVC Framework follows the Model-View-Controller architectural design pattern, which works around the **Front Controller, i.e., the **Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all incoming HTTP requests to the appropriate controller. It uses @Controller and @RequestMapping as default request handlers. The ****@Controller** annotation defines that a particular class is a controller. The ****@RequestMapping** annotation maps web requests to Spring Controller methods. The terms model, view, and controller are defined as follows:

Dispatcher Servlet

The Dispatcher Servlet is the front controller that manages the entire HTTP request and response handling process. Now, the question is: **What is a Front Controller? It is quite simple, as the name suggests:

Spring MVC Flow Diagram

Spring-MVC-Framework-Control-flow-Diagram

**Spring MVC Framework works as follows:

Advantages of Spring MVC Framework

Disadvantages of Spring MVC Framework

Steps to Create a Spring MVC Application

Step 0: Set Up the Project

Set up your project with Maven, using the required archetype to create the necessary folder structure, and configure the server with your project.

Step 1: Add Dependencies in pom.xml

Load the Spring JAR files or add the dependencies if Maven is used. Add the following dependencies in pom.xml:

**pom.xml:

XML `

4.0.0

com.geeksforgeeks
SpringMVC
war
0.0.1-SNAPSHOT
SpringMVC Maven Webapp
http://maven.apache.org

<!-- Spring Web MVC -->
<dependency>  
  <groupId>org.springframework</groupId>  
  <artifactId>spring-webmvc</artifactId>  
  <version>5.3.30</version>  
</dependency>  

<!-- Jakarta Servlet API (Provided Scope) -->
<dependency>    
  <groupId>jakarta.servlet</groupId>    
  <artifactId>jakarta.servlet-api</artifactId>    
  <version>5.0.0</version>    
  <scope>provided</scope>    
</dependency>  
SpringMVC

`

Step 2: Define the Controller

Create a controller class that maps a web request to a response.

**HelloGeek.java:

Java `

@Controller
public class HelloGeek {
@RequestMapping("/")
public String display()
{
return "hello";
}
}

`

Step 3: Configure web.xml

Specify the DispatcherServlet in the web.xml file:

**web.xml:

XML `


SpringMVC

spring
org.springframework.web.servlet.DispatcherServlet
1


spring
/

`

Step 4: Define the Bean Configuration

We have to define the bean in a separate XML file. We have specified the view components in this file. It is located in the WEB-INF directory.

spring-servlet.xml:

XML `

<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>

`

In this configuration,

Step 5: Create a JSP File

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

**index.jsp:

HTML `

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Spring MVC Example

${message}

`

This JSP file will display the message passed from the controller.

Step 6: Run the Application

Start the server and run the project. The output will be displayed as expected like below:

Spring MVC Tutorial!!