1、@RequestMapping注解的功能
2、@RequestMapping注解的位置
@Controller
@RequestMapping("/test")
public class Demo {
@RequestMapping("/")
public String index() {
return "index";
}
// 此时请求映射所映射的请求的请求路径为:/test/hello
@RequestMapping("/hello")
public String hello() {
return "target";
}
}
3、@RequestMapping注解的value属性
代码示例:
java代码
@Controller
// @RequestMapping("/test")
public class Demo {
@RequestMapping("/")
public String index() {
return "index";
}
// 此时请求映射所映射的请求的请求路径为:/test/hello
@RequestMapping({"/hello","target"})
public String hello() {
return "target";
}
}
index.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/hello}">测试@RequestMapping的value属性值1</a> <br/>
<a th:href="@{/target}">测试@RequestMapping的value属性值2</a><br/>
</body>
</html>
target.html代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Target</title>
</head>
<body>
<h1>Hello World</h1>
</body>
</html>
4、@RequestMapping注解的method属性
代码示例:
java代码:
@Controller
// @RequestMapping("/test")
public class Demo {
@RequestMapping("/")
public String index() {
return "index";
}
// 此时请求映射所映射的请求的请求路径为:/test/hello
@RequestMapping(
value = {"/hello", "target"},
method = {RequestMethod.GET,RequestMethod.POST}
)
public String hello() {
return "target";
}
}
index.html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/hello}">测试@RequestMapping的value属性值1</a> <br/>
<a th:href="@{/target}">测试@RequestMapping的value属性值2</a><br/>
<form th:action="@{/hello}" method="post">
<input type="submit" value="测试@RequestMapping的method属性"/>
</form>
</body>
</html>
注: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属性
@Controller
// @RequestMapping("/test")
public class Demo {
@RequestMapping("/")
public String index() {
return "index";
}
// 此时请求映射所映射的请求的请求路径为:/test/hello
@RequestMapping(
value = {"/hello", "target"},
method = {RequestMethod.GET,RequestMethod.POST},
params = {"username","!password","age=20","gender!='男'"}
)
public String hello() {
return "target";
}
}
<body>
<h1>首页</h1>
<a th:href="@{/hello}">测试@RequestMapping的 value 属性值1</a> <br/>
<a th:href="@{/target}">测试@RequestMapping的 value 属性值2</a><br/>
<form th:action="@{/hello}" method="post">
<input type="submit" value="测试@RequestMapping的 method 属性"/>
</form>
<a th:href="@{/hello?username='admin'&age=20}">测试@RequestMapping的 params 属性值1</a><br/>
<a th:href="@{/hello(username='admin',age=20,gender='女')}">测试@RequestMapping的 params 属性值2</a><br/>
</body>
6、@RequestMapping注解的headers属性
@RequestMapping(
value = {"/hello", "target"},
method = {RequestMethod.GET,RequestMethod.POST},
// params = {"username","!password","age=20","gender!='男'"},
headers = {"Referer"}
)
public String hello() {
return "target";
}
7、SpringMVC支持ant风格的路径
?:表示任意的单个字符* :表示任意的0个或多个字符** :表示任意层数的任意目录 注意:在使用**时,只能使用/**/xxx的方式
代码示例:
@RequestMapping("/hello/test1/a?a")
public String hello2() {
return "target";
}
@RequestMapping("/hello/test2/a*a")
public String hello3() {
return "target";
}
@RequestMapping("/hello/test3/**/")
public String hello4() {
return "target";
}
index.html代码:
<a th:href="@{/hello/test1/aaa}">测试@RequestMapping的 value 属性值的特殊字符?</a><br/>
<a th:href="@{/hello/test2/aaaaaaa}">测试@RequestMapping的 value 属性值的特殊字符*</a><br/>
<a th:href="@{/hello/test3/a/a/a/a/a/a/}">测试@RequestMapping的 value 属性值的特殊字符**</a><br/>
8、SpringMVC支持路径中的占位符
@RequestMapping("/test/rest/{username}/{id}")
public String testRest(@PathVariable("username") String username, @PathVariable("id") Integer id) {
System.out.println("username="+username);
System.out.println("id="+id);
return "target";
}
<a th:href="@{/test/rest/admin/111}">测试@RequestMapping的 value 属性值的 rest 占位符</a><br/>
![](https://i-blog.csdnimg.cn/blog_migrate/8dc15bb0e828d1c2fca804c8f43482b6.png)
9、总结
1、@RequestMapping注解的位置
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息2、@RequestMapping注解的value属性
@RequestMapping注解的value属性通过请求的请求地址匹配请求映射
@RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
@RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射3、@RequestMapping注解的method属性
通过请求的请求方式(get或post)匹配请求映射
@RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求 若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method 'xxx' not supported
在@RequestMapping 的基础上,结合请求方式的一些派生注解:
@GetMapping,@PostMapping,@PutMapping,@DeleteMapping 4、@RequestMapping 注解的 params 属性
作用:通过请求的请求参数匹配请求,即浏览器发送的请求参数必须满足 params 属性的设置params 可以使用四种表达式:
"param" : 表示当前所匹配请求的请求参数必须携带 param 参数
"!param" : 表示当前所匹配请求的请求参数一定不能携带 param 参数
"param = value" : 表示当前所匹配请求的请求参数必须携带 param 参数且参数值必须为value
"!param" : 表示当前所匹配请求的请求参数可以不携带 param 参数,若携带其参数值不能是 value若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,
此时页面回报错:400 - Parameter conditions "username" not met for actual request parameters
5、@RequestMapping 注解的 headers 属性
作用: 通过请求的请求头信息匹配请求,即浏览器发送的请求的请求头信息必须满足 headers 属性的设置@RequestMapping注解的headers属性是一个字符串类型的数组
可以通过四种表达式设置请求头信息和请求映射的匹配关系
"header":要求请求映射所匹配的请求必须携带header请求头信息
"!header":要求请求映射所匹配的请求必须不能携带header请求头信息
"header=value":要求请求映射所匹配的请求必须携带header请求头信息且header=value
"header!=value":要求请求映射所匹配的请求必须携带header请求头信息且header!=value若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,
此时页面显示404错误,即资源未找到6、SpringMVC 支持 ant 风格的路径
在 @RequestMapping 注解的 value 属性值中设置一些特殊的字符
? : 任意的单个字符(不包括 ? 和 / )
* :任意个数的任意字符(不包括 ? 和 / )
** : 任意层数的任意目录,注意是使用方式,**只能写在两个/中,**前后不能有任何的其他字符7、@RequestMapping 注解使用路径中的占位符
传统:/deleteUser?id=1
rest:/delete/user/1
需要在 @RequsetMapping 注解的 value 属性所设置的路径中,使用 {xxx} 的方式表示路径中的数据
在通过 @PathVariable 注解,将占位符所标识的值和控制器方法的形参进行绑定