当前位置:网站首页>三、@RequestMapping注解
三、@RequestMapping注解
2022-07-26 04:27:00 【时间邮递员】
文章目录
1、@RequestMapping注解的功能
从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。
SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。
2、@RequestMapping注解的位置
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息
@Controller
@RequestMapping("/test")
public class RequestMappingController {
//此时请求映射所映射的请求的请求路径为:/test/testRequestMapping 才能访问到 success
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
return "success";
}
}
3、@RequestMapping注解的value属性
@RequestMapping注解的value属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射
<a th:href="@{/testRequestMapping}">测试@RequestMapping的value属性-->/testRequestMapping</a><br>
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a><br>
@RequestMapping(
value = {
"/testRequestMapping", "/test"}
)
public String testRequestMapping(){
return "success";
}
4、@RequestMapping注解的method属性
@RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method ‘POST’ not supported
<form th:action="@{/test}" method="post">
<input type="submit">
</form>
@RequestMapping(
value = {
"/testRequestMapping", "/test"},
method = {
RequestMethod.GET, RequestMethod.POST}
)
public String testRequestMapping(){
return "success";
}
注:
1、对于处理指定请求方式的控制器方法,SpringMVC中提供了@RequestMapping的派生注解
处理get请求的映射–>@GetMapping 处理post请求的映射–>@PostMapping
处理put请求的映射–>@PutMapping 处理delete请求的映射–>@DeleteMapping2、常用的请求方式有get,post,put,delete
但是目前浏览器只支持get和post,若在form表单提交时,为method设置了其他请求方式的字符串(put或delete),则按照默认的请求方式get处理
若要发送put和delete请求,则需要通过spring提供的过滤器HiddenHttpMethodFilter,在RESTful部分会讲到
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')}">测试@RequestMapping的params属性-->/test</a><br>
@RequestMapping(value = {
"/testRequestMapping","/test"},
method = {
RequestMethod.GET, RequestMethod.POST},
params ={
"username"} )
public String testRequestMapping(){
return "success";
}
注: 若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时页面回报错400
6、@RequestMapping注解的headers属性(了解)
@RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射
@RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系
“header”:要求请求映射所匹配的请求必须携带header请求头信息
“!header”:要求请求映射所匹配的请求必须不能携带header请求头信息
“header=value”:要求请求映射所匹配的请求必须携带header请求头信息且header=value
“header!=value”:要求请求映射所匹配的请求必须携带header请求头信息且header!=value
@RequestMapping(value = {
"/testRequestMapping","/test"},
method = {
RequestMethod.GET, RequestMethod.POST},
params ={
"username"},
headers={
"Host=localhost:8080"})
public String testRequestMapping(){
return "success";
}
若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面显示404错误,即资源未找到
7、SpringMVC支持ant风格的路径
?:表示任意的单个字符
*:表示任意的0个或多个字符
**:表示任意的一层或多层目录
注意:在使用时,只能使用/**/xxx的方式
8、SpringMVC支持路径中的占位符(重点)
原始方式:/deleteUser?id=1
rest方式:/deleteUser/1
SpringMVC路径中的占位符常用于RESTful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,再通过**@PathVariable注解**,将占位符所表示的数据赋值给控制器方法的形参
<a th:href="@{/testRest/1/admin}">测试路径中的占位符-->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
边栏推荐
- 软模拟光栅化渲染器
- Functions of anonymous functions
- 【UOJ 429】串串划分(Runs)(容斥)+ 有关 Lyndon Tree 及其应用的小小记录
- 机器学习之信用卡欺诈检测
- Huawei executives talk about the 35 year old crisis. How can programmers overcome the worry of age?
- Chapter 3 how to use sourcetree to submit code
- Use of anonymous functions
- 理性认知教育机器人寓教于乐的辅助作用
- Low cost, fast and efficient construction of digital collection app and H5 system, professional development of scallop technology is more assured!
- FFmpeg 视频添加水印
猜你喜欢

Credit card fraud detection based on machine learning

Life related - ten years of career experience (turn)

Design and implementation of smart campus applet based on cloud development

makefile知识再整理(超详细)

数据仓库

How engineers treat open source -- the heartfelt words of an old engineer

1. Mx6u system migration-6-uboot graphical configuration
![[C language foundation] 13 preprocessor](/img/4c/ab25d88e9a0cf29bde6e33a2b14225.jpg)
[C language foundation] 13 preprocessor

Acwing brush questions

Analyzing the curriculum design evaluation system of steam Education
随机推荐
HelloWorld case analysis
Tutorial on using the one click upgrade function of the rtsp/onvif protocol video platform easynvr service
Which websites can I visit to check the latest medical literature?
1. Mx6u system migration-6-uboot graphical configuration
Analyzing the curriculum design evaluation system of steam Education
Rotate array minimum number
ROS2的launch有何不同?
生活相关——一个华科研究生导师的肺腑之言(主要适用于理工科)
egg-ts-sequelize-CLI
理性认知教育机器人寓教于乐的辅助作用
YAPI安装
ASP. Net core actionfilter filter details
Steam科学教育赋予课堂教学的创造力
Web Test Method Encyclopedia
【学习笔记】AGC041
远坂凛壁纸
Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release
Graph theory: topological sorting
The difference between positive samples, negative samples, simple samples and difficult samples in deep learning (simple and easy to understand)
I.MX6U-ALPHA开发板(主频和时钟配置实验)