Key Components of Spring MVC Architecture
Spring MVC Architecture follows the Model-View-Controller (MVC) design pattern and has several key components that work together to handle web requests and responses efficiently. Below are the primary components:
1. DispatcherServlet
Acts as the front controller in the Spring MVC architecture. It intercepts all incoming requests and delegates them to the appropriate controller.
2. Handler Mapping:
Maps incoming requests to appropriate handler methods of the controllers.
3. Controller
Contains the business logic and interacts with the model. It processes user requests and returns a view name.
4. Model
Represents the application data. The controller can add data to the model, which is then accessible by the view.
5. View:
Responsible for rendering the model data. It can be a JSP, Thymeleaf template, or any other view technology.
6. View Resolver:
Resolves view names to actual view pages.
7. ModelAndView:
A container object that holds both the model data and the view name.
Table of Contents
Java Examples
1. Project Structure
```
src
└── main
└── java
└── com
└── example
└── config
└── WebConfig.java
└── controller
└── HomeController.java
└── resources
└── application.properties
└── webapp
└── WEB-INF
└── views
└── home.jsp
└── web.xml
```
2. Maven Dependencies (pom.xml)
```xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
```
3. Web Configuration (WebConfig.java)
```java
package com.example.config;
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.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controller")
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
}
```
4. Controller (HomeController.java)
```java
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HomeController {
@GetMapping("/")
public String home(Model model) {
model.addAttribute("message", "Hello, Spring MVC!");
return "home"; // returns the view name "home"
}
}
```
5. View (home.jsp)
```jsp
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
```
6. web.xml Configuration
```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_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>