广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java@RequestMapping注解功能使用详解
  • 511
分享到

Java@RequestMapping注解功能使用详解

Java@RequestMappingJava@RequestMapping注解 2022-11-13 19:11:35 511人浏览 安东尼

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

摘要

目录一、@RequestMapping注解的功能二、@RequestMapping注解的位置三、@RequestMapping注解的value属性四、@RequestMapping注

一、@RequestMapping注解的功能

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

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

二、@RequestMapping注解的位置

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

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

@Controller
@RequestMapping("/test")
public class TestRequestMappinGController {
    //此时控制器方法所匹配的请求的请求路径为/test/hello
    @RequestMapping("/hello")
    public String hello() {
        return "success";
    }
}

三、@RequestMapping注解的value属性

@RequestMapping注解的value属性通过请求的请求地址匹配请求映射

@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址 所对应的请求

@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射

测试映射请求控制器

@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
    //此时控制器方法所匹配的请求的请求路径为/test/hello
    @RequestMapping({"/hello","/abc"})
    public String hello() {
        return "success";
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="Http://www.thymelef.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
    <h1>index.html</h1>
    <a th:href="@{/hello}" rel="external nofollow"  rel="external nofollow" >测试@RequestMapping注解所标识的位置</a>
    <a th:href="@{/abc}" rel="external nofollow"  rel="external nofollow" >测试@RequestMapping注解的value属性</a>
</body>
</html>

success.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymelef.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
  <h1>success.html</h1>
</body>
</html>

结果:

点超链接跳转到下面页面

四、@RequestMapping注解的method属性

@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射

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

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

注:

除了表单我们默认就是post请求,其他的都是get请求

@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
    //此时控制器方法所匹配的请求的请求路径为/test/hello
    @RequestMapping(
            value = {"/hello","/abc"},
            method = RequestMethod.POST)
    public String hello() {
        return "success";
    }
}
    <h1>index.html</h1>
    <a th:href="@{/hello}" rel="external nofollow"  rel="external nofollow" >测试@RequestMapping注解所标识的位置</a>
    <a th:href="@{/abc}" rel="external nofollow"  rel="external nofollow" >测试@RequestMapping注解的value属性</a>
    <fORM th:action="@{/hello}" method="post">
        <input type="submit" value="测试@RequestMapping注解的method属性">
    </form>

结果:

除了按钮可以跳转页面,其他的都报405错误-方法不允许。因为表单设置了post请求,我们的@RequestMapping注解的method属性配置的也是post请求

@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
    //此时控制器方法所匹配的请求的请求路径为/test/hello
    @RequestMapping(
            value = {"/hello","/abc"},
            method = {RequestMethod.POST,RequestMethod.GET})
    public String hello() {
        return "success";
    }
}

上面这种方式也可以匹配两种请求,不过默认的情况下也是匹配两种请求

注:

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,在 RESTful部分会讲到

五、@RequestMapping注解的params属性(了解)

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

params可以使用四种表达式:

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

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

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

路径匹配,请求方式也匹配,但是请求参数不匹配,因为要求我们的请求参数必须有username。报400错误请求-Parameter conditions "username" not met for actual request parameters:

请求参数必须有username,不能有passWord,年龄必须为20,性别必须不等于女

@Controller
//@RequestMapping("/test")
public class TestRequestMappingController {
    //此时控制器方法所匹配的请求的请求路径为/test/hello
    @RequestMapping(
            value = {"/hello","/abc"},
            method = {RequestMethod.POST,RequestMethod.GET},
            params = {"username","!password","age=20","gender!=女"}
    )
    public String hello() {
        return "success";
    }
}
    <a th:href="@{/hello?username=admin}" rel="external nofollow" >测试@RequestMapping注解的params属性</a><br>
    <a th:href="@{/hello(username='admin')}" rel="external nofollow" >测试@RequestMapping注解的params属性</a><br>

六、@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错误,即资源未找到

七、SpringMVC支持ant风格的路径

?:表示任意的单个字符

*:表示任意的0个或多个字符

**:表示任意层数的任意目录

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

八、SpringMVC支持路径中的占位符(重点)

原始方式:/deleteUser?id=1

rest方式:/user/delete/1

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

<a th:href="@{test/rest/admin/1}" rel="external nofollow" >测试@RequestMapping注解的value属性中的占位符</a>
    @RequestMapping("/test/username/{username}/{id}")
    public String testRest(@PathVariable("id") Integer id,@PathVariable("username") String username) {
        System.out.println("id:" + id + ",username:" + username);
        return "success";
    }
}

到此这篇关于Java @RequestMapping注解功能使用详解的文章就介绍到这了,更多相关Java @RequestMapping内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java@RequestMapping注解功能使用详解

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

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

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

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

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

  • 微信公众号

  • 商务合作