🌐 Spring MVC Architecture – A Complete Guide
The Spring MVC (Model-View-Controller) architecture is one of the most widely used patterns in Java web development. It separates the different aspects of the application, such as input logic, business logic, and UI logic, while promoting a clean and maintainable code structure.
In this blog post, we’ll dive deep into the architecture of Spring MVC, its components, request flow, configuration styles, and how it simplifies building web applications.
📖 What is Spring MVC?
Spring MVC is a web framework built on the powerful features of the Spring Framework. It follows the Model-View-Controller design pattern to develop flexible and loosely coupled web applications.
🧱 Core Components of Spring MVC
1. Model
Represents the application data. It holds business logic or interacts with the database.
2. View
Responsible for rendering the model data. Typically implemented using JSP, Thymeleaf, or other templating engines.
3. Controller
Handles user input and maps it to the appropriate service or model updates. It's where the request processing logic resides.
🔁 Spring MVC Request Flow
Let’s break down what happens when a client sends an HTTP request to a Spring MVC application.
Client --> DispatcherServlet --> HandlerMapping --> Controller --> Service/DAO --> ViewResolver --> View --> Client
🔄 Step-by-step Flow:
-
Client Request
-
Browser sends a request to the server.
-
-
DispatcherServlet
-
Central dispatcher to handle all incoming HTTP requests.
-
It's configured in
web.xml
or auto-registered in Spring Boot.
-
-
HandlerMapping
-
Identifies the appropriate controller based on URL mapping.
-
-
Controller
-
Handles the request, interacts with services, and returns a
ModelAndView
.
-
-
Model
-
Business logic is executed, often via service and DAO layers.
-
-
ViewResolver
-
Resolves the logical view name into a physical view (like a JSP page).
-
-
View
-
Renders the response with model data and sends it back to the client.
-
⚙️ DispatcherServlet Configuration
🧾 XML Configuration:
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
🚀 Spring Boot Auto Configuration:
No explicit configuration required. Just annotate with @SpringBootApplication
.
📦 Common Annotations in Spring MVC
@Controller
Marks a class as a web controller.
@RequestMapping
Maps HTTP requests to handler methods.
@RequestMapping("/greet")
public String greet(Model model) {
model.addAttribute("message", "Hello, Spring MVC!");
return "greeting";
}
@GetMapping
, @PostMapping
, etc.
Convenient shortcuts for @RequestMapping
.
@ModelAttribute
Binds request parameters to a model object.
@ResponseBody
Sends the return type directly in the HTTP response.
🛠 View Technologies
Spring MVC supports various view technologies:
-
JSP + JSTL
-
Thymeleaf
-
Freemarker
-
PDF, Excel
-
JSON/XML (with
@RestController
)
Example using Thymeleaf:
<p th:text="${message}">Message goes here</p>
🧪 Example Spring MVC Controller
@Controller
public class HelloController {
@GetMapping("/hello")
public String sayHello(Model model) {
model.addAttribute("message", "Hello World!");
return "hello"; // View resolver maps this to hello.jsp or hello.html
}
}
📦 RESTful Web Services in Spring MVC
Spring MVC also supports building REST APIs using:
-
@RestController
-
@RequestMapping
-
@GetMapping
,@PostMapping
@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/data")
public Data getData() {
return new Data("Sample", 123);
}
}
✅ Best Practices for Spring MVC
-
Use
@Controller
for MVC and@RestController
for REST APIs -
Separate business logic into services (annotated with
@Service
) -
Use a layered architecture (Controller → Service → Repository)
-
Leverage Spring Boot for simplified setup
-
Externalize configuration with
application.properties
oryml
🔁 Comparison: Spring MVC vs Spring WebFlux
Feature | Spring MVC | Spring WebFlux |
---|---|---|
Model | Traditional Servlet | Reactive Programming |
Concurrency | One thread per request | Event loop, non-blocking |
Use Case | Standard web apps | Reactive, high-throughput apps |
📚 Conclusion
Spring MVC is a robust and well-established framework for building Java web applications. With its clean separation of concerns, annotation-driven configuration, and support for multiple view technologies, it continues to be a popular choice for Java developers.
Whether you're creating simple web pages or complex RESTful services, understanding the architecture of Spring MVC will help you write scalable, maintainable, and elegant code.
Sign up here with your email
ConversionConversion EmoticonEmoticon