1. @RequestMapping注解的功能
从注解名称上我们可以看到,@RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系。SpringMVC 接收到指定的请求,就会来找到在映射关系中对应的控制器方法来处理这个请求。
2. @RequestMapping注解的位置
@RequestMapping标识一个类:设置映射请求的请求路径的初始信息
@RequestMapping标识一个方法:设置映射请求请求路径的具体信息
package com.limi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/login")
public class LoginController {
@RequestMapping("/index")
public String hello(){
return "index";
}
}
3. @RequestMapping注解的属性
1.value属性
- @RequestMapping注解的value属性通过请求的请求地址匹配请求映射
- @RequestMapping注解的value属性是一个字符串类型的数组,表示该请求映射能够匹配多个请求地址所对应的请求
- @RequestMapping注解的value属性必须设置,至少通过请求地址匹配请求映射
package com.limi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/login")
public class LoginController {
//value参数是数组,可以填写多个路径
@RequestMapping(value = {"/index1","index2"})
public String hello(){
return "index";
}
}
2.method属性
- @RequestMapping注解的method属性通过请求的请求方式(get或post)匹配请求映射
- @RequestMapping注解的method属性是一个RequestMethod类型的数组,表示该请求映射能够匹配多种请求方式的请求
若当前请求的请求地址满足请求映射的value属性,但是请求方式不满足method属性,则浏览器报错405:Request method ‘POST’ not supported
package com.limi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/login")
public class LoginController {
//value参数是数组,可以填写多个路径
//method参数是数组,可以填写多个请求类型
@RequestMapping(
value = {"/index1","index2"},
method = {RequestMethod.POST, RequestMethod.GET}
)
public String hello(){
return "index";
}
}
注:
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部分会讲到.
3.params属性
- @RequestMapping注解的params属性通过请求的请求参数匹配请求映射
- @RequestMapping注解的params属性是一个字符串类型的数组,可以通过四种表达式设置请求参数和请求映射的匹配关系
①"param":要求请求映射所匹配的请求必须携带param请求参数
②"!param":要求请求映射所匹配的请求必须不能携带param请求参数
③"param=value":要求请求映射所匹配的请求必须携带param请求参数且param=value
④"param!=value":要求请求映射所匹配的请求必须携带param请求参数但是param!=value
index.html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world!</h1>
<!--第一种带参数的方式-->
<a th:href="@{/success(username='admin',password=123456)}">1测试params属性</a>
<!--第二种带参数的方式-->
<a th:href="@{/success?username=admin&password=66666}">2测试params属性</a>
</body>
</html>
package com.limi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LoginController {
//value参数是数组,可以填写多个路径
@RequestMapping(
value = "/index"
,method = {RequestMethod.POST, RequestMethod.GET}
)
public String hello(){
return "index";
}
@RequestMapping(
value = "/success"
,method = {RequestMethod.POST, RequestMethod.GET}
,params = {"username","password!=123456"}
)
public String success(){
return "success";
}
}
password='123456’无法访问
注:
若当前请求满足@RequestMapping注解的value和method属性,但是不满足params属性,此时页面回报错400:Parameter conditions “username, password!=123456” not met for actual request parameters: username={admin}, password={123456}
password='666666’可以访问
4.headers属性
- @RequestMapping注解的headers属性通过请求的请求头信息匹配请求映射
- @RequestMapping注解的headers属性是一个字符串类型的数组,可以通过四种表达式设置请求头信息和请求映射的匹配关系
“header”:要求请求映射所匹配的请求必须携带header请求头信息
“!header”:要求请求映射所匹配的请求必须不能携带header请求头信息
“header=value”:要求请求映射所匹配的请求必须携带header请求头信息且header=value
“header!=value”:要求请求映射所匹配的请求必须携带header请求头信息且header!=value
若当前请求满足@RequestMapping注解的value和method属性,但是不满足headers属性,此时页面显示404错误,即资源未找到
请求头Request Header
测试
//value参数是数组,可以填写多个路径
@RequestMapping(
value = "/index"
,method = {RequestMethod.POST, RequestMethod.GET}
,headers = {"Host=localhost:8081"} //要求是8081端口访问
)
public String hello(){
return "index";
}
Host!=localhost:8081, 访问失败
4. SpringMVC支持ant风格的路径
1. 问号 ? 匹配任意单个字符
@Controller
public class LoginController {
//1. 问号 ? 匹配任意单个字符
@RequestMapping("/index/?/test")
public String hello(){
return "index";
}
}
2. 星号 * 匹配任意0或多个字符
package com.limi.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class LoginController {
//1. ?匹配任意0或多个字符
@RequestMapping("/index/a*a/test")
public String hello(){
return "index";
}
}
注意:当路径是"/index/*/test"这样时, 星号只能匹配1个或1个以上的字符, 不能匹配0个字符.
3. 两个星号 ** 表示任意的一层或多层目录
@Controller
public class LoginController {
//3. 两个星号 ** 表示任意的一层或多层目录
@RequestMapping("/index/**/test")
public String hello(){
return "index";
}
}
5. SpringMVC支持路径中的占位符
- 原始方式:/deleteUser?username=admin&password=123456
- rest方式:/deleteUser/admin/123456
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>
</head>
<body>
<h1>hello world!</h1>
<!--第一种带参数的方式-->
<a th:href="@{/success(username='admin',password=123456)}">1测试</a>
<!--第二种带参数的方式,
其实第一种和第二种效果是一样的,
都是在地址栏中显示http://localhost:8080/springMVC/success?username=admin&password=123456-->
<a th:href="@{/success?username=admin&password=123456}">2测试</a>
<!--第三种带参数方式,以路径携带参数-->
<a th:href="@{/success/admin/123456}">3测试</a>
</body>
</html>
package com.limi.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.RequestParam;
@Controller
public class LoginController {
@RequestMapping("/index")
public String test(){
return "index";
}
//1. 原始方式
@RequestMapping("/success")
//若方法上的参数名称和路径上的参数名一致, 则可以不使用@RequestParam
public String hello1(@RequestParam("username") String username,@RequestParam("password") String password){
System.out.println("原始方式 username:"+username+" password:"+password);
return "success";
}
//2. rest方式
@RequestMapping("/success/{username}/{password}")
public String hello2(@PathVariable("username") String username, @PathVariable("password") String password){
System.out.println("rest方式 username:"+username+" password:"+password);
return "success";
}
}
1.原始方式
2.rest方式