一、功能
从注解名称上我们可以看到,@RequestMapping 注解的作用就是将浏览器的请求和处理请求的控制器方法关联起来,建立映射关系。
SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。
二、注解位置
@RequestMapping 标识一个类:设置映射请求的请求路径的初始信息。
@RequestMapping 标识一个方法:设置映射请求请求路径的具体信息。
@Controller
@RequestMapping("/test")
public class RequestMappingController {
// 此时请求映射所映射的请求的请求路径为:/test/testRequestMapping
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
return "success";
}
}
三、注解的 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>
</head>
<body>
<a th:href="@{/testRequestMapping}">测试 value 属性 -- 通过 /testRequestMapping 路径</a><br>
<a th:href="@{/test}">测试 value 属性 -- 通过 /test 路径</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
// 此时请求映射所映射的两个请求路径都可以访问
@RequestMapping(value = {"/testRequestMapping", "/test"})
public String testRequestMapping(){
return "success";
}
}
四、注解的 method 属性
@RequestMapping 注解的 method 属性通过请求的请求方式(get 或 post)匹配请求映射。
@RequestMapping 注解的 method 属性是一个 RequestMethod 类型的数组,表示该请求映射能够匹配多种请求方式的请求。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<!-- 默认的提交方式为 get 请求-->
<a th:href="@{/test}">测试@RequestMapping的value属性-->/test</a><br>
<form th:action="@{/test}" method="post">
<input type="submit">
</form>
</body>
</html>
如果不写 method 属性,那么无论前端发过来的是 get 还是 post 请求,后台都可以进行处理。即不写和写两个的效果是一样的。
如果后台指定了 method 为 POST 方式而前端使用了 GET 方式,那么浏览器就会报 405 错误。
@Controller
public class RequestMappingController {
@RequestMapping(
value = {"/testRequestMapping", "/test"},
method = {RequestMethod.GET, RequestMethod.POST}
)
public String testRequestMapping(){
return "success";
}
}
4.1 派生注解
对于处理指定请求方式的控制器方法,SpringMVC 中提供了 @RequestMapping 的派生注解。
4.1.1 @GetMapping 注解
用于处理 get 请求的映射,即下面的两种写法作用是相同的,如下:
@Controller
public class RequestMappingController {
@RequestMapping(
value = {"/testRequestMapping", "/test"},
method = {RequestMethod.GET}
)
public String testRequestMapping(){
return "success";
}
@GetMapping (value = {"/testRequestMapping", "/test"})
public String testGetMapping() {
return "success";
}
}
4.1.2 @PostMapping注解
用于处理 post 请求的映射,即下面的两种写法作用是相同的,如下:
@Controller
public class RequestMappingController {
@RequestMapping(
value = {"/testRequestMapping", "/test"},
method = {RequestMethod.POST}
)
public String testRequestMapping(){
return "success";
}
@PostMapping (value = {"/testRequestMapping", "/test"})
public String testPostMapping() {
return "success";
}
}
4.1.3 @PutMapping 注解
用于处理 put 请求的映射,即下面的两种写法作用是相同的,如下:
@Controller
public class RequestMappingController {
@RequestMapping(
value = {"/testRequestMapping", "/test"},
method = {RequestMethod.PUT}
)
public String testRequestMapping(){
return "success";
}
@PutMapping(value = {"/testRequestMapping", "/test"})
public String testPutMapping() {
return "success";
}
}
4.1.4 @DeleteMapping 注解
用于处理 delete 请求的映射,即下面的两种写法作用是相同的,如下:
@Controller
public class RequestMappingController {
@RequestMapping(
value = {"/testRequestMapping", "/test"},
method = {RequestMethod.DELETE}
)
public String testRequestMapping(){
return "success";
}
@DeleteMapping(value = {"/testRequestMapping", "/test"})
public String tesTDeleteMapping() {
return "success";
}
}
4.2 常用请求方式
常用的请求方式有 get,post,put,delete。
但是目前浏览器只支持 get 和 post,若在 form 表单提交时,为 method 设置了其他请求方式的字符串(put 或 delete),则按照默认的请求方式 get 处理。
若要发送 put 和 delete 请求,则需要通过 spring 提供的过滤器 HiddenHttpMethodFilter 进行配置。
五、注解的 params 属性
@RequestMapping 注解的 params 属性通过请求的请求参数匹配请求映射。
@RequestMapping 注解的 params 属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系。
// 要求请求映射所匹配的请求必须携带 username 请求参数
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username"}
)
public String testParaMapping(){
return "success";
}
// 要求请求映射所匹配的请求必须不能携带 username 请求参数
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"!username"}
)
public String testParaMapping(){
return "success";
}
// 要求请求映射所匹配的请求必须携带 username 请求参数且 username=zhangSan
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username=zhangSan"}
)
public String testParaMapping(){
return "success";
}
// 要求请求映射所匹配的请求必须携带 username 请求参数且 username!=zhangSan
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username!=zhangSan"}
)
public String testParaMapping(){
return "success";
}
测试代码如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/test(username='admin',password=123456)}">测试 params 属性-->/test</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
// 要求请求映射所匹配的请求必须携带 username 请求参数
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,params = {"username"}
)
public String testParaMapping(){
return "success";
}
}
若当前请求满足 @RequestMapping 注解的 value 和 method 属性,但是不满足 params 属性,此时页面回报错 400。
六、注解的 headers 属性
@RequestMapping 注解的 headers 属性通过请求的请求头信息匹配请求映射。
@RequestMapping 注解的 headers 属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系。和 params 属性效果是一样的。
// 要求请求映射所匹配的请求必须携带 localhost 请求头信息
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,header= {"localhost"}
)
public String testParaMapping(){
return "success";
}
// 要求请求映射所匹配的请求必须不能携带 localhost 请求头信息
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,header= {"!localhost"}
)
public String testParaMapping(){
return "success";
}
// 要求请求映射所匹配的请求必须携带 Host 请求参数且 Host=localhost:8080
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,header = {"Host=localhost:8080"}
)
public String testParaMapping(){
return "success";
}
// 要求请求映射所匹配的请求必须携带 Host 请求参数且 Host!=localhost:8080
@RequestMapping(
value = {"/testRequestMapping", "/test"}
,method = {RequestMethod.GET, RequestMethod.POST}
,header = {"Host !=localhost:8080"}
)
public String testParaMapping(){
return "success";
}
若当前请求满足 @RequestMapping 注解的 value 和 method 属性,但是不满足 headers 属性,此时页面显示 404 错误,即资源未找到。
七、支持 ant 风格的路径
可以在 @RequestMapping 注解的 value 属性上使用模糊匹配的功能,常用的如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/abc}">测试 ? 属性</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
// ? 表示任意的单个字符
@RequestMapping("/a?c")
public String testParaMapping(){
return "success";
}
}
还可以使用 *,如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/abc}">测试 * 属性</a><br>
<a th:href="@{/abbbbbc}">测试 * 属性</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
// * 表示任意的0个或多个字符
@RequestMapping("/a*c")
public String testParaMapping(){
return "success";
}
}
还可以使用 **,如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/a/b/c/d/ac}">测试 * 属性</a><br>
<a th:href="@{/ac}">测试 * 属性</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
// ** 表示任意的一层或多层目录,在使用 ** 时,只能使用 /**/xxx 的方式
@RequestMapping("/**/ac")
public String testParaMapping(){
return "success";
}
}
八、支持路径中的占位符
8.1 原始方式
我们在前后端进行传值操作时有两种方式,第一种是使用 ? 将参数拼接到路径的后面,然后在后台代码中进行接收,如下所示:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/test?userName=zhangSan&password=123456}">测试传值使用问号方式</a><br>
<a th:href="@{/test(userName='admin',password=123456)}">测试传值使用括号方式</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
@RequestMapping("/test")
public String testParaMapping(String userName,String password){
System.out.println("userName="+userName+" ,password="+password);
return "success";
}
}
8.2 rest 方式
SpringMVC 路径中的占位符常用于 Restful 风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的 @RequestMapping 注解的 value 属性中通过占位符 {xxx} 表示传输的数据,再通过 @PathVariable 注解,将占位符所表示的数据赋值给控制器方法的形参。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/test/1/admin}">测试路径中的占位符-->/testRest</a><br>
</body>
</html>
@Controller
public class RequestMappingController {
// 将参数放在 RequestMapping 的路径后面,然后在参数列表中使用 @PathVariable 注解进行接收
@RequestMapping("/test/{id}/{username}")
public String test(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
}