📅  最后修改于: 2023-12-03 14:47:33.628000             🧑  作者: Mango
Spring MVC is a popular web framework built on top of the Spring framework for building dynamic web applications using Java. In this introduction, we will cover some of the key features of Spring MVC.
Spring MVC follows the Model-View-Controller (MVC) architecture pattern. The Model represents the application data, the View presents the data to the user, and the Controller handles user input and updates the Model and View as necessary.
Spring MVC provides a DispatcherServlet that maps incoming requests to handlers based on URL patterns specified in the application's configuration. Handlers can be implemented as Java classes or methods that return a view name or a ModelAndView object containing a view name and associated model data.
Controllers are Java classes that handle requests and produce responses. They define handler methods that are mapped to URL patterns.
@Controller
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public ModelAndView getUser(@PathVariable("id") Long id) {
User user = userService.getUser(id);
ModelAndView mav = new ModelAndView("user");
mav.addObject("user", user);
return mav;
}
// Other handler methods
}
In the above example, we have defined a UserController that maps incoming requests to the "/users" URL pattern. The getUser() method handles GET requests to the "/users/{id}" URL pattern, where {id} is a path variable.
Views are responsible for presenting the data to the user. They can be implemented using technologies like JSP, Thymeleaf, or Velocity. Views often receive data from the Controller via the Model.
<!DOCTYPE html>
<html>
<head>
<title>User Info</title>
</head>
<body>
<h1>User Info</h1>
<div>
<p>Name: ${user.name}</p>
<p>Email: ${user.email}</p>
</div>
</body>
</html>
In the above example, we have defined a JSP view that displays user information. The values for the ${user.name} and ${user.email} placeholders are provided by the UserController via the model.
Spring MVC configuration can be done via XML or Java-based configuration. Java-based configuration using annotations is becoming more popular due to its simplicity and flexibility.
@Configuration
@ComponentScan(basePackages = "com.example")
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
// Other configuration methods
}
In the above example, we have defined an AppConfig that configures Spring MVC via annotations. The @ComponentScan annotation tells Spring where to find the Controller classes, @EnableWebMvc enables Spring MVC support, and the addViewControllers() method maps the root URL ("/") to the "home" view. The viewResolver() method configures a JSP view resolver that looks for JSP files in the "/WEB-INF/views/" directory with a ".jsp" extension.
Spring MVC is a powerful web framework for building dynamic web applications using Java. It follows the Model-View-Controller (MVC) architecture pattern, provides powerful request handling, and can be configured using XML or Java-based configuration.