SpringMVC使用指南
一、SpringMVC的工作原理
1.1 SpringMVC的原理流程


- 用户发起请求,请求被拦截到DispatcherServlet中(前端控制器FrontController),它是Spring的核心,
- 将请求的URL,使用HandlerMapping处理,找到处理对应请求的处理器Handler
- HandlerMapping将找到的控制器信息和处理后的请求信息封装到HandlerExcution中,返回给DispatcherServlet
- DispatcherServlet将处理后的Handler信息传递给HandlerAdapter
- HandlerAdapter找到对应的Controller
- Controller处理请求后,将数据和要返回的视图信息封装到ModelAndView中,返回给HandlerAdapter
- HandlerAdapter将ModelAndView返回给DispatcherServlet
- DispatcherServlet将ModelAndView传给视图解析器ViewResolver
- 视图解析器将视图信息解析传来的视图名,并将具体的视图地址返回给DispatcherServlet
- DispatcherServlet根据视图地址找到视图,并将模型数据传递传给视图显示
- 视图渲染后返回给DispatcherServlet
- DispatcherServlet将渲染后的视图作为响应返回给用户
1.2 SpringMVC接口
流程中包含了几个接口或类:
- DispatcherServlet类:所有的请求都经过它,在DispatcherServlet将请求发给Controller之前需要借助HandlerMapping定位到具体的Controller
- HandlerMapping接口:负责请求与Controller的映射
- Controller接口:处理用户请求,与JavaEE的Servlet一致,Controller处理请求后将返回ModelAndView对象,给DispatcherServlet前端控制器
- ViewResolver接口:视图解析器,在Web应用中查找View对象,将Model渲染返回给用户
- ModelAndView类:存储Controller处理后返回的数据模型Model和视图View
二、SpringMVC的简单使用(不使用注解)
2.1 创建一个maven项目
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>top.lan-mao.computer-world.study-2022</groupId>
    <artifactId>SpringMVC-Demo02</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>SpringMVC-Demo02</name>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.8.2</junit.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.14</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <compilerArguments>
                        <extdirs>src/main/webapp/WEB-INF/lib</extdirs><!-- 无此指定则需要手动把maven的jar包,映射到lib下面 -->
                    </compilerArguments>
                    <!-- 跳过测试用例 ,也不编译-->
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
2.2 配置web.xml
<?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">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <!--启动级别1:指服务启动时就启动这个servlet-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
需要注意,DispatcherServlet匹配的路径是/,而不是/*,是因为而是/*是路径匹配,会拦截*.jsp请求;而/是Servlet中的缺省匹配,优先级最低,*.jsp会被Tomcat中jsp解析器拦截,而不是被SpringMVC拦截。
SpringMVC默认的Spring配置文件位置是WEB-INF/springmvc-servlet.xml,springmvc是web.xml中配置的Servlet名字。如果不在此处则需要配置。可以在两处地方配置:1. DispatcherServlet中使用init-param标签;2. web.xml中的context-param标签中配置contextConfigLocation项
DispatcherServlet中配置的load-on-startup项的1指在web项目启动时就启动该Servlet
2.3 配置springmvc-servlet.xml
<?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!--处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value="*.jsp"/>
    </bean>
    <bean name="/hello" class="top.lan_mao.computer_world.study_2022.springmvc_demo02.HelloServlet"/>
</beans>
- BeanNameUrlHandlerMapping:是处理器映射器,将Controller与请求路径匹配,按照Bean的name和id属性匹配
- SimpleControllerHandlerAdapter:适配器,将匹配后的处理器执行
- InternalResourceViewResolver:视图解析器,解析视图的路径等,加前后缀等
- 处理器、映射器、视图解析器在Spring4.0之后,不是必须要配置的。
2.4 创建一个Controller类
public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", "HELLO");
        modelAndView.setViewName("hello"); //WEB-INF/jsp/hello.jsp
        return modelAndView;
    }
}
三、使用注解
SpringMVC在2.5版本之后就提供了基于注解配置的方式,所以实际使用基本都是用注解来配置。
如果不使用注解,使用接口创建Controller类,一个类中只能有一个请求处理方法。
3.1 在项目中启用注解
<?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 http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!--对包进行注解扫描-->
    <context:component-scan base-package="top.lan_mao.computer_world.study_2022.springmvc_demo03.controller"/>
    <!--将对静态文件的请求过滤,不经过DispatcherServlet,也可以使用<mvc:resource location="/html"/>标签进行过滤-->
    <mvc:default-servlet-handler/>
    <!--启用SpringMVC对使用注解时的默认映射器、适配器-->
    <mvc:annotation-driven/>
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value="*.jsp"/>
    </bean>
</beans>
3.2 基于注解创建控制器Controller
@Controller
@RequestMapping("/hello")
public class HelloController {
    @RequestMapping("/h1")
    public String hello(Model model) {
        model.addAttribute("msg", "Hello");
        return "hello";
    }
}
- @Controller注解表示该类被Spring容器包含
- @RequestMapping:可以用于类和方法,表示请求的路径。 
  - 可以通过method属性确定请求的类型。或使用@GetMapping、@PostMapping、@PutMapping、@PutMapping、@PutMapping、@PatchMapping等替换和明确请求方式。但只能生效一个。 
    - @RequestMapping(value = "/h1",method = {RequestMethod.GET,RequestMethod.POST})
 
- 作用于类的路径是方法的父路径
 
- 可以通过method属性确定请求的类型。或使用@GetMapping、@PostMapping、@PutMapping、@PutMapping、@PutMapping、@PatchMapping等替换和明确请求方式。但只能生效一个。 
    
四、@Controller的处理方法
4.1 @Controller允许的参数类型
- ServletRequest /ServletResponse /HttpServletRequest /HttpServletResponse
- HttpSession
- InputStream / Reader / OutputStream / Writer
- Local/ TimeZone/ ZoneId
- java.security.Principal
- @PathVariable(RestFul风格)、@MatrixVariable、@RequestParam(接收请求参数)、@RequestHeader、@RequestBody、@RequestPart、@SessionAttribute、@RequestAttribute注解
- HttpMethod
- Map/ Model/ ModelMap
- RedirectAttributes(用于重定向,在Session中传参,接收后就移除)
- Errors/ BindingResult
- SessionStatus/ UriComponentsBuilder
- HttpEntity<?>
4.2 允许的返回值类型
- ModelAndView
- Model
- View
- String(方法上添加@ResponseBody注解,那么String值就会直接作为响应返回)
- void
- HttpEntity<?>或- ResponseEntity<?>
- Callable<?>/- DeferredResult<?>
4.3 接收请求参数的方式
4.3.1 请求参数名称与处理方法形参名称相同
直接通过处理方法的形参接收,建议即使名称一样,也添加@RequestParam注解来确定参数名称,可以明确一些错误原因。
4.3.2 请求参数名称与处理方法形参名称不同
添加@RequestParam(“name”)注解绑定请求参数名与形参
4.3.3 通过Bean实体类接收参数
传递的参数名称与Bean中的属性名一致,如果有不一致的,则属性值为null
4.3.4 通过@PathVariable接收
对RestFul风格请求的URL,需要形参前添加@PathVariable注解
4.3.5 通过@ModelAttribute接收参数
与通过Bean接收一样,但不同在于,接收的参数会添加到Model中。具体使用方式可看:Java EE框架整合开发入门到实战:Spring+Spring MVC+MyBatis(微课版)-陈恒-微信读书
五、RestFul请求风格
SpringMVC支持RESTFul风格的请求路径。
@RequestMapping(value="/h2/{a}/{b}", method=RequestMethod.GET)
public String hello2(@PathVariable Integer a, @PathVariable Integer b, Model model) {
    model.addAttribute("msg", a + b);
    return "hello";
}
- 可以在路径中通过{}包含参数,对应的同名参数需要添加@PathVariable注解。
- RestFul风格的请求路径与传统风格的请求路径就不算是同一个请求了,需要两个方法来接收请求。
- 最好添加GET的请求方式限定
六、请求转发与重定向
在SpringMVC框架中,可以通过return一个路径的方式,默认是通过请求转发的方式将数据传递到视图。也可以通过显示的标识确定方式,即在路径前添加"forward:"(转发)或"redirect:"(重定向)。
需要注意:
- 添加后的路径是不再通过视图解析器了,即配置的前后缀是不起作用的
- 其转发和重定向的路径都是在本web应用目录下跳转 
  - 项目路径:/demo3;请求地址:/hello/h1,/hello/h2
- “forward:hello”: /demo3/hello/hello
- “forward:/hello”: /demo3/hello
- “redirect:hello”: /demo3/hello/hello
- “redirect:/hello”: /demo3/hello
 
- 可以在请求的路径上添加?后的参数,SpringMVC会自动拼接要传递的参数
- 重定向传递参数有两种方式, 
  - 使用GET的URL地址传参,
- 使用类RedirectAttributes通过Session传参,RedirectAttributes在Session中添加参数,在接收后就删除参数。 
    - 使用的方法是redirectAttributes.addFlashAttributie(“prama”,value)
- 如果传递到一个Controller中,那么需要@RequestPrama(value = “prama”)String prama方式接收参数
- 方法redirectAttributes.addAttributie(“prama”,value)依旧是GET方式传参
 
- 使用的方法是
 
七、乱码问题
7.1 JSP和Servlet
SpringMVC中自带了一个乱码问题的过滤器。在web.xml中配置即可。
需要注意,过滤器路径最好设置为/*,否则可能不起作用。
<filter>
    <filter-name>encoding</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>encoding</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
![[为什么CharacterEncodingFilter对响应不起作用]]
但对于添加了@ResponseBody注解直接返回字符串的响应不会起作用,因为SpringMVC是通过StringHttpMessageConverter输出的,它会添加头信息:Content-Type: text/plain;charset=ISO-8859-1,覆盖掉了过滤器对它做的字符集设置。可以在Spring的配置文件中设置:
<mvc:annotation-driven>
    <!-- 消息转换器 -->
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="UTF-8"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
或者在处理方法上设置字符集@RequestMapping(value="/u3", produces = "text/plain;charset=UTF-8")。需要注意,如果请求发来的请求头中的Accept字段没有produces中的对应的协议,会出现406错误。
八、SpringMVC对JSON的支持
SpringMVC默认支持Jackson框架来解析JSON串。只需要添加Jackson的包就能使用。可以直接返回一个对象,SpringMVC会自动转化为JSON串。
使用时必须先设置编码格式,否则中文乱码。
<mvc:annotation-driven>
    <!-- 消息转换器 -->
    <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="defaultCharset" value="UTF-8"/>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>
对应的处理方法或类上必须添加@ResponseBody注解,才能直接返回字符串。或者将类的@Controller注解改为@RestController注解(这是@Controller和@ResponseBody的组合注解。
@Controller
public class UserController {
    @RequestMapping("/u1")
    @ResponseBody
    public String getUser1() {
        User user = new User("电脑", 12, "男");
        return user.toString();
    }
    @RequestMapping("/u2")
    @ResponseBody
    public String getUser2() throws JsonProcessingException {
        User user = new User("电脑", 12, "男");
        return new ObjectMapper().writeValueAsString(user);
    }
    /**
     * 解决乱码问题,添加了RequestMapping的produces属性,但请求的Accept字段中必须包含对应的协议类型
     */
    @RequestMapping(value="/u3", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getUser3() throws JsonProcessingException {
        User user = new User("电脑", 12, "男");
        return new ObjectMapper().writeValueAsString(user);
    }
    @RequestMapping("/u4")
    @ResponseBody
    public User getUser4(HttpServletRequest request, HttpServletResponse response) {
        return new User("电脑", 12, "男");
    }
}
资料
- RequestMapping中的参数consumes 与 produces_游湖虾_新浪博客









