广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringMVC的注解@RequestMapping属性及使用
  • 719
分享到

SpringMVC的注解@RequestMapping属性及使用

2024-04-02 19:04:59 719人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录springMVC注解@RequestMapping一、@RequestMapping 注解的位置1. 作用在方法2. 作用在类二、@RequestMapping 注解的 val

springmvc注解@RequestMapping

在之前的 hello world 示例中,用到了 @RequestMapping 注解,它的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。

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

一、@RequestMapping 注解的位置

在示例中,注解是用在了方法上,除此之外,还可以用在类上。

1. 作用在方法

@Controller
public class RequestMappinGController {
 
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping(){
        return "success";
    }
}

此时请求映射所映射的请求的请求路径为:/testRequestMapping。

2. 作用在类

@Controller
@RequestMapping("/test")
public class RequestMappingController {
 
    //此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
    @RequestMapping("/testRequestMapping")
    public String testRequestMapping(){
        return "success";
    }
}

此时请求映射所映射的请求的请求路径为:/test/testRequestMapping。

作用在类上以后会经常用到,比如有2个模块:用户和订单,那么每个模块下都会有自己的列表接口 /list。

为了更好的通过名称区分出不同模块,可以给两个类上加上注解,使其最终路径为/user/list、/order/list。

当然,你也可以不用类的注解,直接在方法的注解上做区分,比如/userList和/orderList。

总之,一个请求只能有一个控制器来处理,如果你两个不同的控制器方法,都使用/list,那么请求过来的时候就不知道该找哪个处理,会报错。

二、@RequestMapping 注解的 value 属性

value 属性通过请求的请求地址匹配请求映射,是必须设置的,否则请求地址匹配不到映射。

另外,value 属性也是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址的请求。

@Controller
public class RequestMappingController {
 
    @RequestMapping(
            value = {"/test1", "/test2"}
    )
    public String testRequestMapping(){
        return "success";
    }
}

这里不管请求地址是/test1还是/test2,都可以找到控制器 testRequestMapping(),返回 success.html

三、@RequestMapping 注解的 method 属性

method属性通过请求的请求方式,比如 get 或 post ,来匹配请求映射。

method 属性是一个 RequestMethod 类型的数组,表示该请求映射能够匹配多种请求方式的请求。

若当前请求的请求地址满足请求映射的 value 属性,但是请求方式不满足 method 属性,比如控制器是设置是 GET 请求方法,但是请求发送是 post:

@Controller
public class RequestMappingController {
 
    @RequestMapping(
            value = {"/test1"},
            method = {RequestMethod.GET}
    )
    public String testRequestMapping(){
        return "success";
    }
}

index.html 添加 post 发送:

<!DOCTYPE html>
<html lang="en" xmlns:th="Http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>Hello World</h1>
    <a th:href="@{/target}" rel="external nofollow" >访问目标页面 target.html </a>
    <br>
    <a th:href="@{/test1}" rel="external nofollow" >测试@RequestMapping的value属性-->/test1</a><br>
    <a th:href="@{/test2}" rel="external nofollow" >测试@RequestMapping的value属性-->/test2</a><br>
    <a th:href="@{/test}" rel="external nofollow" >测试@RequestMapping的method属性-->/test</a><br>
    <fORM th:action="@{/test1}" method="post">
        <input type="submit">
    </form>
</body>
</html>

点击 submit 按钮,浏览器报错405:Request method 'POST' not supported。

如果继续添加 post 方法支持,则可以正常访问:

@Controller
public class RequestMappingController {
 
    @RequestMapping(
            value = {"/test1"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    public String testRequestMapping(){
        return "success";
    }
}

@RequestMapping 结合请求方式的派生注解

对于处理指定请求方式的控制器方法,SpringMVC 中提供了 @RequestMapping 的派生注解。

  • @GetMapping:处理 get 请求的映射
  • @PostMapping:处理 post 请求的映射
  • @PutMapping:处理 put 请求的映射
  • @DeleteMapping:处理 delete 请求的映射

使用这种注解,就不需要设置 method 属性了。

@GetMapping("/test3")
    public String testGetMapping() {
        return "success";
    }

四、@RequestMapping 注解的 params 属性

最常用的还是上面说的 value 和 method 属性,其他的仅做了解。

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

param:要求请求映射所匹配的请求必须携带 param 请求参数

!param:要求请求映射所匹配的请求必须不能携带 param 请求参数

param=value:要求请求映射所匹配的请求必须携带 param 请求参数且 param=value

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

举个例子:

@RequestMapping(
            value = {"/test1"},
            method = {RequestMethod.GET, RequestMethod.POST},
            params = {"username", "passWord!=123456"}
    )
    public String testRequestMapping(){
        return "success";
    }

这里params = {"username", "password!=123456"}的意思就是,请求中必须带有参数username和password,且password参数的值必须不等于123456。

五、@RequestMapping 注解的 headers 属性

同样作个了解。

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

  • header:要求请求映射所匹配的请求必须携带header请求头信息
  • !header:要求请求映射所匹配的请求必须不能携带header请求头信息
  • header=value:要求请求映射所匹配的请求必须携带header请求头信息且header=value
  • header!=value:要求请求映射所匹配的请求必须携带header请求头信息且header!=value

若当前请求满足 @RequestMapping 注解的 value 和 method 属性,但是不满足 header 属性,此时页面显示404错误,即资源未找到。

@RequestMapping(
            value = {"/test1"},
            method = {RequestMethod.GET, RequestMethod.POST},
            params = {"username", "password!=123456"},
            headers = {"Host=localhost:8081"}  // 这里端口变成8081
    )
    public String testRequestMapping(){
        return "success";
    }

我本地端口是 8080,现在去请求页面,会报错。

感谢《尚硅谷》的学习资源。

以上就是SpringMVC的注解@RequestMapping属性及使用的详细内容,更多关于SpringMVC注解@RequestMapping的资料请关注编程网其它相关文章!

--结束END--

本文标题: SpringMVC的注解@RequestMapping属性及使用

本文链接: https://www.lsjlt.com/news/150083.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作