• SpringMVC ---- @RequestMapping注解


    1. @RequestMapping注解的功能

    从注解名称上我们可以看到,@RequestMapping 注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

    SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。

    2. @RequestMapping注解的位置

    @RequestMapping 标识一个类:设置映射请求的请求路径的初始信息
    @RequestMapping 标识一个方法:设置映射请求请求路径的具体信息

    package com.fickler.mvc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author dell
     * @version 1.0
     */
    
    @Controller
    @RequestMapping("/hello")
    public class RequestMappingController {
    
        @RequestMapping("testRequestMapping")
        public String testRequestMapping(){
            return "success";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3. @RequestMapping注解的value属性

    @RequestMapping 注解的 value 属性通过请求的请求地址匹配请求映射
    @RequestMapping 注解的 value 属性是一个字符串类型的数组,标识该请求映射能够匹配多个请求地址所对应的请求
    @RequestMapping 注解的 value 属性必须设置,至少通过请求地址匹配请求映射

    <a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMappinga><br>
    <a th:href="@{/test}">测试@RequestMapping的value属性-->/testa>
    
    • 1
    • 2
    package com.fickler.mvc.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    /**
     * @author dell
     * @version 1.0
     */
    
    @Controller
    //@RequestMapping("/hello")
    public class RequestMappingController {
    
        @RequestMapping(value = {"testRequestMapping", "test"})
        public String testRequestMapping(){
            return "success";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    4. @RequestMapping注解method属性

    @RequestMapping 注解的 method 属性通过请求的请求方式(get 或 post)匹配请求映射
    @RequestMapping 注解的 method 属性是一个 RequestMethod 类型的数组,表示该请求映射能够匹配多种请求方式的请求

    若当前请求的请求地址满足请求映射的 value 属性,但是请求方式不满足 method 属性,则浏览器报错 405:Request method ‘POST’ not supported

    <a th:href="@{/test}">测试@RequestMapping的value属性-->/testa>
    <form th:action="@{/test}" method="post"><input type="submit">form>
    
    • 1
    • 2
    @Controller
    //@RequestMapping("/hello")
    public class RequestMappingController {
    
        @RequestMapping(value = {"testRequestMapping", "test"},method = {RequestMethod.GET, RequestMethod.POST})
        public String testRequestMapping(){
            return "success";
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    注:

    1. 对于处理请求方式的控制器方法,SpringMVC 中提供了 @RequestMapping 的派生注解
      处理 get 请求的映射 --> @GetMapping
      处理 post 请求的映射 --> @PostMapping
      处理 put 请求的映射 --> @PutMapping
      处理 delete 请求的映射 --> @DeleteMapping
    2. 常用的请求方式有 get、post、put、delete
      但是目前浏览器只支持 get 和 post,若 form 表单提交时,为 method 设置了其他请求方式的字符串(put 或 delete),则按照默认的请求方式 get 处理
      若要发送 put 和 delete 请求,则需要通过 spring 提供的过滤器 HiddenHttpMethodFilter

    5. @RequestMapping注解的params属性(了解)

    @RequestMapping 注解的 params 属性通过请求的请求参数匹配请求映射
    @RequestMapping 注解的 params 属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系

    “param”:要求请求映射所匹配的请求必须携带 param 请求参数
    “!param”:要求请求映射所匹配的请求必须不能携带 param 请求参数
    “param = value”:要求请求映射所匹配的请求必须携带 param 请求参数且 param = value
    “param != value”:要求请求映射所匹配的请求必须携带 param 请求参数但是 param != value

    <a th:href="@{/test(username = 'admin', password(排除文章风险) = '12345')}">测试@RequestMapping的param属性-->username = 'admin', password = '12345'a>
    
    • 1
        @RequestMapping(value = "test", params = {"username", "password!=13456"})
        public String testRequestMappingAndParam(){
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4

    注:
    若当前请求满足 @RequestMapping 注解的 value 和 method 属性,但是不满足 params 属性,此时页面会报错 400:Parameter conditions “username, password != 123456” not met for actual request parameters: username = {admin}, password = {123456}

    6. @RequestMapping注解的headers属性(了解)

    @RequestMapping 注解的 headers 属性通过请求的请求头信息匹配请求映射
    @RequestMapping 注解的 headers 属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系

    “header”:要求请求映射所匹配的请求必须携带 header 请求头信息
    “!header”:要求请求映射所匹配的请求必须不能携带 header 请求头信息
    “header = value”:要求请求映射所匹配的请求必须携带 header 请求头信息切 header = value
    “header != value”:要求请求映射所匹配的请求必须携带 header 请求头信息且 header != value
    若当前请求满足 @RequestMapping 注解的 value 和 method 属性,但是不满足 headers 属性,此时页面显示 404 报错,即资源为找到

    7. SpringMVC支持ant风格的路径

    ?:表示任意的单个字符
    *:表示任意的 0 或多个字符
    **:表示任意的一层或多层目录

    注意:在使用 ** 的时候,只能使用 /**/xxx 的方式

    //    @RequestMapping("/a?a/testAnt")
    //    @RequestMapping("/a*a/testAnt")
        @RequestMapping("/**/testAnt")
        public String testAnt(){
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    8. SpringMVC支持路径中的占位符(重点)

    原始方式:/deleteUser?id = 1
    rest 方式:/deleteUser/1

    SpringMVC 路径中的占位符常用于 RESTful 风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在响应的 @RequestMapping 注解的 value 属性中通过占位符 {xxx} 表示传输的数据,再通过 @PathVariable 注解,将占位符所表示的数据赋值给控制器方法的形参

    <a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testResta><br>
    
    • 1
        @RequestMapping("testRest/{id}/{username}")
        public String testRest(@PathVariable("id")Integer id, @PathVariable("username")String username){
            System.out.println("id: " + id + "\n" + "username: " + username);
            return "success";
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 相关阅读:
    hdl-graph-slam怎么保存tum格式轨迹
    Java进阶知识复习笔记(三):函数式编程
    程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
    Vue.js 响应式系统深度剖析
    HTML的特殊字符
    Gym 104025 M -Counting in Tree
    AutoSAR点亮LED:开发环境介绍
    一张图了解JVM八大原子操作
    主流开发语言和开发环境介绍
    C++ 数据结构 哈弗曼树的制作
  • 原文地址:https://blog.csdn.net/qq_52354698/article/details/126982550