spring-mvc系列:详解@RequestMapping注解(value、method、params、header等)
一、@RequestMapping注解的功能
从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。
SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。
二、@RequestMapping注解的位置
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
@RequestMapping("/test")
public class RequestMappingController {
//此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
@RequestMapping("/testRequestMapping")
public String testRequestMapping() {
return "success";
}
}
三、@RequestMapping注解的value属性
@RequestMapping注解的value属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
</body>
</html>
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(value = {"test", "testRequestMapping"})
public String testRequestMapping() {
return "success";
}
}
四、@RequestMapping注解的method属性
@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求,若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错
405:Request method 'POST' not supported
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
<form th:action="@{/test}" method="post">
<input type="submit" value="提交">
</form>
</body>
</html>
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(value = {"test", "testRequestMapping"},
method = RequestMethod.GET)
public String testRequestMapping() {
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
五、@RequestMapping注解的params属性
@RequestMapping注解的params属性通过请求的请求参数匹配请求映射
@RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系
"param":要求请求映射所匹配的请求必须携带param请求参数
"!param":要求请求映射所匹配的请求必须不能携带param请求参数
"param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value
"param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
<form th:action="@{/test}" method="post">
<input type="submit" value="提交">
</form>
<a th:href="@{/test(username='admin',password=123456)}">测试@RequestMapping的params属性-->/test</a><br>
</body>
</html>
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping(value = {"test", "testRequestMapping"},
method = RequestMethod.GET,
params = {"username", "password!=123456"})
public String testRequestMapping() {
return "success";
}
}
注:
若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时页面回报错400:
Parameter conditions "username, password!=123456" not met for actual
request parameters: username={admin}, password=
六、@RequestMapping注解的header属性
@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分格的路径
?:表示任意的单个字符
@RequestMapping(value = "/a?a/testAnt")
*:表示任意的0个或多个字符
@RequestMapping("/a*a/testAnt")
**:表示任意的一层或多层目录
@RequestMapping("/**/testAnt")
注意:在使用**时,只能使用/**/xxx的方式
八、SpringMVC支持路径中的占位符
原始方式:/deleteUser?id=1
rest方式:/deleteUser/1
SpringMVC路径中的占位符常用于RESTful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,在通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
a{
display: block;
}
</style>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a>
<form th:action="@{/test}" method="post">
<input type="submit" value="提交">
</form>
<a th:href="@{/test(username='admin',password=123456)}">测试@RequestMapping的params属性-->/test</a><br>
<a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testRest</a><br>
</body>
</html>
package com.mcode.api.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* ClassName: RequestMappingController
* Package: com.mcode.api.controller
* Description:
*
* @Author: robin
* @Create: 2023/8/3 - 10:32 PM
* @Version: v1.0
*/
@Controller
//@RequestMapping("/test")
public class RequestMappingController {
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username) {
System.out.println("id:" + id + ",username:" + username);
return "success";
}
}
spring-mvc系列:详解@RequestMapping注解(value、method、params、header等)的更多相关文章
- 0054 Spring MVC的@Controller和@RequestMapping注解
@Controller注解 该注解用来指示一个类是一个控制器,在Spring的配置xml文件中开启注解扫描 <context:conponent-scan base-package=" ...
- Spring MVC配置详解(3)
一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...
- Spring MVC异常处理详解
Spring MVC中异常处理的类体系结构 下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要 ...
- spring MVC配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring mvc 配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring MVC异常处理详解(转)
下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要实现HandlerExceptionReso ...
- spring MVC配置详解(转)
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring MVC异常处理详解 ExceptionHandler good
@ControllerAdvice(basePackageClasses = AcmeController.class) public class AcmeControllerAdvice exten ...
- Spring MVC @ModelAttribute详解
被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,因此对于一个controller映射多个URL的用法来说,要谨慎使用. 我们编写控制器代码时,会将保存方法独立 ...
- spring mvc DispatcherServlet详解之前传---FrameworkServlet
做项目时碰到Controller不能使用aop进行拦截,从网上搜索得知:使用spring mvc 启动了两个context:applicationContext 和WebapplicationCont ...
随机推荐
- 2023-05-02:如果一个正整数每一个数位都是 互不相同 的,我们称它是 特殊整数 。 给你一个正整数 n ,请你返回区间 [1, n] 之间特殊整数的数目。 输入:n = 20。 输出:19。
2023-05-02:如果一个正整数每一个数位都是 互不相同 的,我们称它是 特殊整数 . 给你一个正整数 n ,请你返回区间 [1, n] 之间特殊整数的数目. 输入:n = 20. 输出:19. ...
- Prism Sample 13-IActiveAwareCommands
本例和12的唯一区别,仅仅是在ViewModel中增加了一个IActiveAware,这决定了只有在Acitve状态的视图中才会执行自己ViewModel中的命令.
- 【Dotnet 工具箱】DotNetCorePlugins- 动态加载和卸载 .NET 程序插件
你好,这里是 Dotnet 工具箱,定期分享 Dotnet 有趣,实用的工具和组件,希望对您有用! 1. DotNetCorePlugins- 动态加载和卸载 .NET 程序插件 DotNetCore ...
- 一站式统一返回值封装、异常处理、异常错误码解决方案—最强的Sping Boot接口优雅响应处理器
作者:京东物流 覃玉杰 1. 简介 Graceful Response是一个Spring Boot体系下的优雅响应处理器,提供一站式统一返回值封装.异常处理.异常错误码等功能. 使用Graceful ...
- 2021-09-27:Pow(x, n)。实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,x**n)。力扣50。
2021-09-27:Pow(x, n).实现 pow(x, n) ,即计算 x 的 n 次幂函数(即,x**n).力扣50. 福大大 答案2021-09-27: 遍历n的二进制位. 时间复杂度:O( ...
- 深入理解 python 虚拟机:破解核心魔法——反序列化 pyc 文件
深入理解 python 虚拟机:破解核心魔法--反序列化 pyc 文件 在前面的文章当中我们详细的对于 pyc 文件的结构进行了分析,pyc 文件主要有下面的四个部分组成:魔术. Bite Filed ...
- gitlab-runner 中的 Docker-in-Docker
笔者个人理解:gitlab-runner 安装后就是一个监听状态的 runner,而通过 gitlab-runner register 注册的"实例"其实只是预定义的配置节,当消息 ...
- 【GiraKoo】常用编码的对比(ASCII,GB2312,GBK,GB18030,UCS,Unicode)
常用编码的对比(ASCII,GB2312,GBK,GB18030,UCS,Unicode) 在程序开发中,文字编码一直扮演着人畜无害,却背后捅一刀的角色. 可能在源代码文件中,注释莫名其妙地变成了乱码 ...
- 我在 vscode 插件里接入了 ChatGPT,解决了代码变量命名的难题
lowcode 插件 已经迭代了差不多3年.作为我的生产力工具,平常一些不需要动脑的搬砖活基本上都是用 lowcode 去完成,比如管理脚手架,生成 CURD 页面,根据接口文档生成 TS 类型,生成 ...
- 2023-06-17:说一说redis中渐进式rehash?
2023-06-17:说一说redis中渐进式rehash? 答案2023-06-17: 在Redis中,如果哈希表的数组一直保持不变,就会增加哈希冲突的可能性,从而降低检索效率.为了解决这个问题,R ...