• 【SpringMVC】@RequestMapping注解


    @RequestMapping注解的功能

    @RequestMapping的作用:就是将请求(request)处理请求的控制器方法(控制层)关联起来,建立一个映射关系,SpringMVC接收到指定的请求,就会找到在映射关系中对应的控制器方法(控制层)来处理这个请求

    1.@RequestMapping注解的位置

    @RequestMapping在类上加:设置映射请求的请求路径的初始信息

    @RequestMapping在方法上加:设置映射请求请求路径的具体信息

    1. @Controller
    2. @RequestMapping("/test")
    3. public class RequestMappingController {
    4. //此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
    5. @RequestMapping("/testRequestMapping")
    6. public String testRequestMapping(){
    7. return "success";
    8. }
    9. }

    2.@RequestMapping注解的value属性值

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

    即当前浏览器所发送请求的请求路径匹配value属性中的任何一个值,则当前请求就会被注解所标识的方法进行处理

    1. <a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性--
    2. >/testRequestMappinga><br>
    3. <a th:href="@{/test}">测试@RequestMapping的value属性-->/testa><br>
    1. @RequestMapping(
    2. value = {"/testRequestMapping", "/test"}
    3. )
    4. public String testRequestMapping(){
    5. return "success";
    6. }

    3.@RequestMapping注解的method属性

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

    请求方法包括:get、post、head、option 、put、 patch、 delete、 trance

    常用的请求方法包括:get、post、put、delete

    注意:若要发送put或delete请求,则需要通过spring提供的过滤器HiddenHttpMethodFilter

                表单和Ajax提交一般使用post,其他大部分都是get

    1. <form th:action="@{/hello}" method="post"><br>
    2. <input type="submit" value="测试@RequestMapping注解的method属性"><br>
    3. form>
    1. @RequestMapping(
    2. value = {"/testRequestMapping", "/test"},
    3. method = {RequestMethod.GET, RequestMethod.POST}
    4. )
    5. public String testRequestMapping(){
    6. return "success";
    7. }

  • 相关阅读:
    无线通信技术概览
    云计算环境下安全关键技术研究
    sqli-labs/Less-59
    核货宝:选择批发订货系统源码需要注意的三大关键点
    YOLOv6 PyTorch模型转TensorRT
    Android AMS——启动Activity(六)
    AOP概念及作用
    2014年下半年 系统架构设计师 下午论文
    Flink 支持三种时间语义
    C# 连接Linux中的redis
  • 原文地址:https://blog.csdn.net/xiaobaisimple/article/details/133252637