Spring MVC

发布时间 2023-10-23 20:35:04作者: yanggdgg

一、Spring MVC子父容器问题

因为Spring MVC属于Spring的子框架,所以Spring MVC中可以使用Spring框架的全部内容。

Spring 官方为Spring MVC专门定义了一个容器,这个容器里面放Spring MVC中全部Bean,且这个容器属于Spring容器的子容器。

有这样的一个规定:Spring MVC子容器可以调用Spring 父容器的全部内容。但是Spring父容器不能调用Spring MVC子容器内容。

 二、Spring MVC环境搭建

Spring MVC 在平时随意可以当成一个独立框架看待,但其本质只是Spring Framework中的spring-webmvc.jar文件,这个jar文件依赖了spring web模块和Spring框架核心功能的5个依赖。所以在只使用Spring MVC框架时需要导入spring-webmvc依赖即可。

引入依赖

    <dependencies>
        <!-- 依赖了Spring框架核心功能的5个依赖以及Spring整合Web的依赖spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.16</version>
        </dependency>
    </dependencies>

创建springmvc配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!-- 扫描控制器类,千万不要把service等扫描进来,也千万不要在Spring配置文件扫描控制器类所在包 -->
    <context:component-scan base-package="com.bjsxt.controller"></context:component-scan>
    <!-- 让Spring MVC的注解生效 不要引错xsd-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>
springmvc.xml

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>
            <!-- 参数名称必须叫做:contextConfigLocation。单词和大小写错误都导致配置文件无法正确加载 -->
            <param-name>contextConfigLocation</param-name>
            <!-- springmvc.xml 名称自定义,只要和后面创建的文件名称对应就可以了。 -->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <!-- Tomcat启动立即加载Servlet,而不是等到访问Servlet才去实例化DispatcherServlet -->
        <!-- 配置上的效果:Tomcat启动立即加载Spring MVC框架的配置文件-->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- /表示除了.jsp结尾的uri,其他的uri都会触发DispatcherServlet。此处不要写成 /* -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
web.xml

三、@RequestMapping注解

@RequestMapping注解可以写在控制器类上,也可以写在控制单元方法上。

1. 所有属性

 2. value、path 属性

value和path都是定义映射的路径。

单个路径:

@RequestMapping(path="/first")

多个路径:

@RequestMapping(path={"/first4","first5"})

当只有value属性时,可以省略value 但path不能省略。

3. name 属性

name:给控制单元定义一个名称。可以理解name是控制单元的注释。

@RequestMapping(value = "/testName",name = "测试下name属性")

4. method 属性

method属性类型是RequestMethod[],RequestMethod是枚举类型,支持HTTP协议中绝大多数请求类型。

 当设置了method属性后,表示只有指定类型请求方式才能访问这个控制单元方法,其他的请求方式访问时,响应会出现405状态码。

// 请求方式只能是DELETE和POST类型。
@RequestMapping(value = "/testMethod",method = {RequestMethod.DELETE,RequestMethod.POST})

4.1 简写

Spring MVC 框架针对不同请求方式提供了5个专门请求方式的注解

​    @PostMapping("/first") 

​        等效于 @RequestMapping(value = "/first",method = RequestMethod.POST)

​    @GetMapping("/first")

​        等效于 @RequestMapping(value = "/first",method = RequestMethod.GET)

​    @DeleteMapping("/first")

​        等效于 @RequestMapping(value = "/first",method = RequestMethod.DELETE)

​    @PutMapping("/first")

​        等效于 @RequestMapping(value = "/first",method = RequestMethod.PUT)

​    @PatchMapping("/first")

​        等效于 @RequestMapping(value = "/first",method = RequestMethod.PATCH)

5. params 属性

params属性类型是String[],表示请求中必须包含指定名称的请求参数。

@RequestMapping(value="/testParam",params = {"name"})

表示请求中必须包含参数名为name的参数

如果请求中没有包含指定类型参数,响应会出现400状态码。并且明确提示在实际的请求参数中没有明确设置name参数。

6. headers 属性

headers属性类型是String[],表示请求头中必须包含指定的请求头参数。

@RequestMapping(value="/testHeaders",headers = "Accept")

如果请求头中没有指定的请求头参数,浏览器会报404。

7. consumes 属性

consumers表示处理请求内容(Content-Type)的类型,平时多不设置,由Spring MVC自动判断。

8. produces 属性

produces类型是String[],作用是设置@ResponseBody注解的响应内容类型。且仅当请求头中Accept中包含的值才生效。

@RequestMapping(value="/testProduces",produces = "text/html;charset=utf-8")
@ResponseBody
public String testProduces() throws IOException {
    return "这是中文";
}

注意:

  produces只对@ResponseBody注解生效,对于代码中响应输出流对象PrintWriter是无效的。

四、映射路径

1. 多级路径

可以写多级路径,但要注意返回值加 ‘/ ’ 与不加的区别

加了/ :/代表项目根目录,也就是webapp目录的根目录,就会从项目的根目录找。

不加:相当于./ 即当前目录。  

2. Ant 风格

在Spring MVC中支持Ant风格的映射路径写法。所谓的Ant风格就是支持三种特殊的符号。

符号解释
? 匹配任意单字符
* 匹配0或者任意数量的字符
** 匹配0或者更多数量的目录

解释说明:

使用Ant的特殊符号时,表示模糊匹配。可能出现客户端发送过来的URL能匹配上多个映射路径,这时的优先级为:

固定值 > ? > *> /**