Spring MVC follows the Model-View-Controller (MVC) architecture to separate application logic, user interface, and request handling. It helps developers build organized and maintainable web applications using simple Java classes. ViewResolvers are used to map logical view names returned by controllers to actual view pages.
- Controllers handle user requests and manage application flow.
- Models store and transfer application data between components.
- ViewResolvers resolve and render the appropriate view pages like JSP files.
Spring MVC Request Flow: From Client to View

Component of Spring Mvc
There are four main components of Spring Mvc.
1. Model
A Model in Spring MVC represents the application data that is transferred between the controller and the view. It stores the information required for processing and displaying results to the user.
- Contains business or application data in object form.
- Helps pass data from the controller to the view page.
Example: A User object with name, email, and age
2. View
A View in Spring MVC is responsible for presenting application data to the user through the user interface. It displays the processed data received from the controller in a readable format.
- Used to render output pages for users.
- Technologies like JSP, Thymeleaf, and FreeMarker are commonly used for creating views.
Example: An HTML page showing user details.
3. Controller
A Controller in Spring MVC handles user requests and controls the application flow. It processes input data, interacts with the model, and returns the appropriate view to the user.
- Marked using the @Controller annotation.
- Acts as a bridge between the model and the view.
4. Front Controller
A Front Controller in Spring MVC is responsible for handling all incoming requests and directing them to the appropriate controller. It acts as a central entry point of the application.
- DispatcherServlet works as the Front Controller in Spring MVC.
- It manages request routing and application flow control.
Prerequisites
- Eclipse (EE version).
- Tomcat Apache latest version
Steps to Build Spring MVC App with Java-Based Configuration
Folllow these steps to create a Spring Mvc Application with java based configuration.
Step 1: Create a Maven Project
Go to File menu > Click on New > Select Maven Project.

Step 2: Search for Maven Project
In the search bar, type maven, select Maven Project, and click Next.

Step 3: Keep Default Settings
Ensure the default settings remain unchanged and click Next.

Step 4: Select Maven Archetype
Select maven-archetype-webapp for web applications and click Next.

Step 5: Configure Group ID and Artifact ID
Provide a Group ID and Artifact ID.

Step 6: Configure Tomcat Runtime
Right-click on the project > Properties

Click on Targeted Runtimes > Select the installed Apache Tomcat > Click Apply and Close.

Step 7: Create Java Folder
Ensure Java files are in src/main/java to build a Spring MVC project.
- Go to the src folder in the project.
- Right-click on main and select New Folder.

Name the folder as java.

Step 8: Create Java Class
Create a Java class named "AddController" inside com.geeksforgeeks.springmvc under src/main/java.

Step 9: Add Dependencies in pom.xml
This file contains the Maven dependencies for the Spring framework.
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.springmvc</groupId>
<artifactId>SpringMVCApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.34</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCApp</finalName>
</build>
</project>
Step 10: Create Java Configuration Files(WebInitializer.java)
WebInitializer replaces with web.xml. The getServletMappings() function receives requests corresponding to the '/' URL mapping. getServletConfigClasses() configures the dispatcher servlet and transfers the handler to MVCconfig.class.
package com.geeksforgeeks.web;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MVCconfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 10: Create MVCconfig.java
This file replaces the dispatcher servlet. The @ComponentScan annotation enables component scanning.
package com.geeksforgeeks.web;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan("com.geeksforgeeks.web")
public class MVCconfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
Step 11: Create GreetController.java
This controller handles the /greet request.
package com.geeksforgeeks.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class GreetController {
@RequestMapping("/greet")
public ModelAndView showView() {
ModelAndView mv = new ModelAndView();
mv.setViewName("result"); // Logical view name
mv.addObject("result", "GeeksForGeeks Welcomes you to Spring!");
return mv;
}
}
Step 12: Create index.jsp
This is the landing page of the application.
<html>
<body>
<h2>Hello World!</h2>
<form action="greet">
<input type="submit" value="Press to greet">
</form>
</body>
</html>
Step 13: Create result.jsp
This page is displayed when the button in index.jsp is pressed.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>
Run Application
- Right-click project → Run on Server
- Start Tomcat server
Open browser type below URL:
http://localhost:8080/SpringMVCApp/index.jsp
We will see a simple HTML page with:
- A heading: "Hello World!"
- A form with a submit button labeled "Press to greet"
Output:

When you click "Press to greet", it sends a request to the /greet endpoint, which is handled by the GreetController which loads result.jsp and displays.
