2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
SpringMVC is a Java open source framework and an independent module in the Spring Framework ecosystem. It is a lightweight web framework based on Spring that implements the request-driven type of the Web MVC (data, business, and presentation) design pattern, providing great convenience for simplifying daily development.
DispatcherServlet front controller
Responsible for receiving and distributing requests
Handler
The processor includes interceptors, methods in controllers, etc., and is mainly responsible for processing requests
HandlerMapping
Parse configuration files, scan annotations, and match requests with processors
HandlerAdpter Processor Adapter
Finding a matching processor based on the request is called adaptation
ViewResolver
The result obtained after the processor is executed may be a view, but this view is a logical view (there is logical code in the page, such as loops and judgments), which needs to be processed by the view decoder. This process is called rendering the view.
<!--低版本-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!--配置Servlet初始化参数-->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--前置控制器要接收所有的请求,因此在容器启动的时候就应该完成初始化-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--视图解析器:在控制器返回视图的时候生效-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图资源的前缀-->
<property name="prefix" value="/" />
<!--视图资源的后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--处理器映射的方式:使用bean的名字或者id的值来与请求匹配-->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
public class UserController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//这里使用配置的视图解析器进行解析 user => / + user + .jsp => /user.jsp
return new ModelAndView("user");
}
}
<!--通过id值匹配请求的URL-->
<bean id="/view" class="com.qf.spring.mvc.controller.UserController" />
Think about it: According to this way of matching requests, each request requires a corresponding controller. This is the same as using Servlet development, which will require writing a large number of controllers, resulting in extremely low development efficiency. How to solve this problem?
Spring 提供了方法名来匹配请求来解决这个问题
Spring provides a resolver for method names in controllers, InternalPathMethodNameResolver, which uses method names as the basis for matching URL requests and associates them with controllers.
Spring provides a MultiActionController controller class for other controller classes to inherit. In its subclasses, developers can write multiple methods to handle requests and then use the method name resolver to match requests.
public class UserMultiController extends MultiActionController {
//这个方法就匹配 /login 请求
//请求格式必须是
//ModelAndView 方法名(HttpServletRequest req, HttpServletResponse resp){}
public ModelAndView login(HttpServletRequest req, HttpServletResponse resp){
return new ModelAndView("login");
}
//这个方法就匹配 /register 请求
public ModelAndView register(HttpServletRequest req, HttpServletResponse resp){
return new ModelAndView("register");
}
}
<!--方法名解析器-->
<bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />
<!-- /login 请求使用该bean对象处理-->
<bean id="/login" class="com.qf.spring.mvc.controller.UserMultiController">
<property name="methodNameResolver" ref="methodNameResolver" />
</bean>
<!-- /register 请求使用该bean对象处理-->
<bean id="/register" class="com.qf.spring.mvc.controller.UserMultiController">
<property name="methodNameResolver" ref="methodNameResolver" />
</bean>
Think about it: According to this way of matching requests, if a controller has to handle multiple requests, it will lead to the problem of numerous configuration information, which will be difficult to maintain later. How to solve it?
Spring 提供了 SimpleUrlHandlerMapping 映射器, 该映射器支持一个控制器与多个请求匹配的同时也解决了配置信息繁多的问题。
To use SimpleUrlHandlerMapping, you only need to modify the spring-mvc.xml configuration.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--视图解析器:在控制器返回视图的时候生效-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图资源的前缀-->
<property name="prefix" value="/" />
<!--视图资源的后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--处理器映射的方式:使用bean的名字或者id的值来与请求匹配-->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>-->
<!--通过id值匹配请求的URL-->
<!-- <bean id="/view" class="com.qf.spring.mvc.controller.UserController" />-->
<!--方法名解析器-->
<!-- <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />-->
<!-- /login 请求使用该bean对象处理-->
<!-- <bean id="/login" class="com.qf.spring.mvc.controller.UserMultiController">-->
<!-- <property name="methodNameResolver" ref="methodNameResolver" />-->
<!-- </bean>-->
<!-- /register 请求使用该bean对象处理-->
<!-- <bean id="/register" class="com.qf.spring.mvc.controller.UserMultiController">-->
<!-- <property name="methodNameResolver" ref="methodNameResolver" />-->
<!-- </bean>-->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/view">userController</prop>
<prop key="/user/*">userMultiController</prop>
</props>
</property>
</bean>
<bean id="userController" class="com.qf.spring.mvc.controller.UserController" />
<bean id="userMultiController" class="com.qf.spring.mvc.controller.UserMultiController" />
</beans>
Thinking: As the project development progresses, more and more business functions are developed, the number of controllers will also increase, and the matching of requests will also increase, which will also cause problems that are difficult to maintain in the later stage. How to solve it?
Spring 提供了 DefaultAnnotationHandlerMapping 映射器,支持使用注解来匹配请求,这样就解决了请求匹配导致配置信息繁多的问题,同时还提升了开发效率。
@Controller
public class UserAnnotationController {
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(){
return "login";
}
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(){
return "register";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--视图解析器:在控制器返回视图的时候生效-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图资源的前缀-->
<property name="prefix" value="/" />
<!--视图资源的后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--处理器映射的方式:使用bean的名字或者id的值来与请求匹配-->
<!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>-->
<!--通过id值匹配请求的URL-->
<!-- <bean id="/view" class="com.qf.spring.mvc.controller.UserController" />-->
<!--方法名解析器-->
<!-- <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver" />-->
<!-- /login 请求使用该bean对象处理-->
<!-- <bean id="/login" class="com.qf.spring.mvc.controller.UserMultiController">-->
<!-- <property name="methodNameResolver" ref="methodNameResolver" />-->
<!-- </bean>-->
<!-- /register 请求使用该bean对象处理-->
<!-- <bean id="/register" class="com.qf.spring.mvc.controller.UserMultiController">-->
<!-- <property name="methodNameResolver" ref="methodNameResolver" />-->
<!-- </bean>-->
<!--<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/view">userController</prop>
<prop key="/login">userMultiController</prop>
<prop key="/register">userMultiController</prop>
</props>
</property>
</bean>
<bean id="userController" class="com.qf.spring.mvc.controller.UserController" />
<bean id="userMultiController" class="com.qf.spring.mvc.controller.UserMultiController" />-->
<!--类上的注解处理器-->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!--方法上的注解处理器-->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!--扫描包,使得该包下类以及类中定义的方法上所使用的注解生效-->
<context:component-scan base-package="com.qf.spring.mvc.controller" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--视图解析器:在控制器返回视图的时候生效-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--视图资源的前缀-->
<property name="prefix" value="/" />
<!--视图资源的后缀-->
<property name="suffix" value=".jsp" />
</bean>
<!--较新的版本使用该标签开启注解支持-->
<mvc:annotation-driven />
<!--扫描包,使得该包下类以及类中定义的方法上所使用的注解生效-->
<context:component-scan base-package="com.qf.spring.mvc.controller" />
</beans>
This annotation is a controller identifier
@Controller
public class UserController{
}
This annotation is used to match requests
@Controller
@RequestMapping("/user")
public class UserController{
@RequestMapping(value="/login", method=RequestMethod.POST)
public int login(){
return 1;
}
}
This annotation can only be applied to method parameters to obtain data from the request body and inject it into the parameters.
@Controller
@RequestMapping("/user")
public class UserController{
@RequestMapping(value="/login", method=RequestMethod.POST)
public int login(@RequestBody User user){
return 1;
}
}
This annotation is used to pass data to the page
@Controller
@RequestMapping("/user")
public class UserController{
@RequestMapping(value="/login", method=RequestMethod.POST)
@ResponseBody
public int login(@RequestBody User user){
return 1;
}
}
This annotation can only be applied to method parameters to obtain data from the request header and inject it into the parameters.
@Controller
@RequestMapping("/user")
public class UserController{
@RequestMapping(value="/search", method=RequestMethod.GET)
@ResponseBody
public List<User> searchUsers(@RequestParam(value="name") String name){
return new ArrayList<>();
}
}
This annotation can only be applied to method parameters to obtain data from the request path and inject it into the parameters.
@Controller
@RequestMapping("/user")
public class UserController{
// /user/admin
@RequestMapping(value="/{username}", method=RequestMethod.GET)
@ResponseBody
public User queryUser(@PathVariable("username") String username){
return new User();
}
}
This annotation can only be used on class definitions to put input into the session
@SessionAttributes(types=User.class) //会将model中所有类型为 User的属性添加到会话中。
@SessionAttributes(value={“user1”, “user2”}) //会将model中属性名为user1和user2的属性添加到会话中。
@SessionAttributes(types={User.class, Dept.class}) //会将model中所有类型为 User和Dept的属性添加到会话中。
@SessionAttributes(value={“user1”,“user2”},types={Dept.class}) //会将model中属性名为user1和user2以及类型为Dept的属性添加到会话中。
This annotation can only be applied to method parameters and is used to obtain data from the request header.
@RequestMapping("/find")
public void findUsers(@RequestHeader("Content-Type") String contentType) {//从请求头中获取Content-Type的值
}
This annotation can only be applied to method parameters and is used to obtain the cookie value from the request.
@RequestMapping("/find")
public void findUsers(@CookieValue("JSESSIONID") String jsessionId) {//从请cookie中获取jsessionId的值
}
This annotation can only be applied to a class, indicating that this class is the controller that handles exceptions
/**
* 异常处理的控制器
*/
@ControllerAdvice //这个注解就是spring mvc提供出来做全局异常统一处理的
public class ExceptionController {
}
This annotation can only be applied to methods of classes marked with @ControllerAdvice or @RestControllerAdvice to handle exceptions.
/**
* 异常处理的控制器
*/
@ControllerAdvice //这个注解就是spring mvc提供出来做全局异常统一处理的
public class ExceptionController {
@ExceptionHandler //异常处理器
@ResponseBody //响应至页面
public String handleException(Exception e){
return e.getMessage();
}
}
JSR stands for Java Specification Requests, which means Java specification proposal. JSR-303 is a standard framework provided by Java for Java Bean data validity verification. It defines a set of verification annotations that can be marked on member variables, attributes and methods. Hibernate Validator provides an implementation of this standard.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.1.Final</version>
<!-- 最新7.0.1.Final -->
</dependency>
annotation | explain | annotation | explain |
---|---|---|---|
@Null | Must be null | @NotNull | Cannot be null |
@AssertTrue | Must be true | @AssertFalse | Must be false |
@Min | Must be a number whose value is greater than or equal to the specified minimum value | @Max | Must be a number whose value is less than or equal to the specified maximum value |
@DecimalMin | Must be a number whose value is greater than or equal to the specified minimum value | @DecimalMax | Must be a number whose value is less than or equal to the specified maximum value |
@Size | The length of the collection | @Digits | Must be a number and its value must be within the acceptable range |
@Past | Must be a date in the past | @Future | Must be a future date |
@Pattern | Must match the regular expression | Must be in email format | |
@Length(min=,max=) | The size of the string must be within the specified range | @NotEmpty | Cannot be null, length greater than 0 |
@Range(min=,max=,message=) | The element must be in the appropriate range | @NotBlank | Cannot be null, string length is greater than 0 (string only) |
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.1.Final</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- web.xml -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/" p:suffix=".jsp" />
<mvc:annotation-driven>
<mvc:message-converters>
<!--处理字符串的消息转换器-->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<!--处理JSON格式的消息转换器-->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<context:component-scan base-package="com.qf.spring.controller" />
</beans>
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.NotNull;
public class User {
@NotNull(message = "账号不能为空")
@Length(min = 8, max = 15, message = "账号长度必须为8~15位")
private String username;
@NotNull(message = "密码不能为空")
@Length(min = 8, max = 20, message = "密码长度必须为8~20位")
private String password;
@Range(min = 0, max = 120, message = "年龄只能在0~120岁之间")
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/add")
@ResponseBody
public Object saveUser(@Valid User user, BindingResult result){
if(result.hasErrors()) return result.getAllErrors();
return 1;
}
}
REST stands for Representational State Transfer,express Representational State Transfer。
RESTFUL has the following characteristics:
/user GET => 获取用户资源
/user POST => 增加用户资源
/user PUT => 修改用户资源
/user DELETE => 删除用户资源
/user/{username} GET => 获取指定用户资源 这是RESTFUL风格中子资源的表述方式
This annotation can only be applied to a class, which is equivalent to the combination of @Controller and @ResponseBody annotations. It means that the results returned by all methods in the class after execution are directly output to the page.
Static resources include html, js, css, images, font files, etc. Static files do not have a url-pattern, so they are not accessible by default. The reason why they can be accessed is that there is a global servlet in tomcat: org.apache.catalina.servlets.DefaultServlet, whose url-pattern is "/", so all static resource requests that cannot be matched in the project are handled by this servlet. However, in SpringMVC, DispatcherServlet also uses "/" as the url-pattern, so the global Serlvet will no longer be used in the project, which makes it impossible to access static resources.
The url-pattern corresponding to DispathcerServlet can be changed to other matching patterns other than "/". For example, *.do, *.action. After this modification, when sending a request, the request URL must match .do or .action.
<!-- web.xml -->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<!-- spring-mvc.xml -->
<!--
这个handler就是处理静态资源的,它的处理方式就是将请求转会到tomcat中名为default的Servlet
-->
<mvc:default-servlet-handler/>
<!-- mapping是访问路径,location是静态资源存放的路径 -->
<mvc:resources mapping="/static/**" location="/static/" />
<?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<!--字符编码过滤器-->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<!--编码格式-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<!--强制编码-->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
checkMultipart(request); //检测是否是多部分请求,这个只可能在文件上传的时候为真
getHandler(processedRequest); //获取处理器 => 遍历HandlerMapping,找到匹配当前请求的执行器链
//没有找到执行器链 就直接向页面报一个404
noHandlerFound(processedRequest, response);
//找到处理当前请求的适配器
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
//控制器之前执行的拦截器将先执行,如果拦截器不通过,则方法直接结束
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
//控制器处理请求,可能会得到一个ModelAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//控制器之后的拦截器执行
mappedHandler.applyPostHandle(processedRequest, response, mv);
//处理分发的结果:这个结果就是控制器处理后的结果
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
//拦截器在控制器给出的结果DispatcherServlet处理后执行
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);