SpringMVC接收参数主要通过以下几种方式:
- 处理request uri 部分(这里指uri template中variable,不含queryString部分)的注解: @PathVariable;
- 处理request header部分的注解: @RequestHeader, @CookieValue;
- 处理request body部分的注解:@RequestParam, @RequestBody;
- 处理attribute类型是注解: @SessionAttributes, @ModelAttribute;
@PostMapping("/add")
public void add(Dept dept){
System.out.println(dept);
}
@RequestBody:参数通过json的方式,请求体
- 将页面传递过来的JSON对象转换成Java对象
- @RequestBody注解常用来处理Content-Type是application/json, application/xml等,不是application/x-www-form-urlencoded编码的内容;
- @RequestBody 注解用于读取 Request 请求的 body 部分数据,使用系统默认配置的 HttppMessageConverter 进行解析,然后把相应的数据绑定到 Controller 方法的参数上。
- @RequestBody不能接收表单提交的数据.
@PostMapping("/add")
public void add(@RequestBody Dept dept){
System.out.println(dept);
}
@RequestParam:接收多个指定的参数
常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况;因为使用request.getParameter()方式获取参数,所以可以处理get 方式中queryString的值,也可以处理post方式中 body data的值
@RequestParam用来处理Content-Type为 application/x-www-form-urlencoded编码的内容,提交方式GET、POST
@GetMapping("/login")
public void login(@RequestParam String name, @RequestParam String password, @RequestParm("code") String verifyCode){
System.out.println(name+" "+ password);
}
@PathVariable
@PathVariable 注解可以将 URL 中动态参数绑定到控制器处理方法的入参中 。 @PathVariable注解只有一个value属性, 类型为String,表示绑定的名称,如果缺省则默认绑定 同名参数。
@GetMapping("/fun1/{id}")
public void fun1(@PathVariable Integer id){
System.out.println(id);
}
@GetMapping("/fun2/{name}")
public void fun2(@PathVariable("name") String username){
System.out.println(id);
}
@RequestHeader
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。
示例:
- 页面代码:
自定义Header
t1
t2
t3
t4
function fun1() {
$.ajax({
url: "demo/fun1",
type: "GET"
})
}
function fun2() {
$.ajax({
type: "GET",
url: "demo/fun2",
beforeSend: function (request) { //使用beforeSend
request.setRequestHeader('content-Type', 'application/x-www-form-urlencoded');
request.setRequestHeader('token', 'fajsdlvnczewoiqr325vcnxv');
request.setRequestHeader("p", "XIXIXI");
}
})
}
//使用beforeSend方法设置请求头
function fun3() {
$.ajax({
type: "GET",
url: "demo/fun3",
beforeSend: function (request) { //使用beforeSend
request.setRequestHeader('content-Type', 'application/x-www-form-urlencoded');
request.setRequestHeader('token', 'fajsdlvnczewoiqr325vcnxv');
request.setRequestHeader("p", "XIXIXI");
}
})
}
//将header信息放在请求头中
function fun4() {
$.ajax({
type:"GET",
url:"demo/fun4",
headers: { //请求头
p:"hahaha",
token: "zxvdfsfgerwtwerhthd543hfdsg"
}
})
}
- 控制器代码:
@Controller
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/fun1")
public void fun1() {
System.out.println(33);
}
@GetMapping("/fun2")
public void fun2(HttpServletRequest request) {
String token = request.getHeader("token");
System.out.println(token);
String p = request.getHeader("p");
System.out.println(p);
}
@GetMapping("/fun3")
public void fun2(@RequestHeader("token") String token, @RequestHeader("p") String p) {
System.out.println(token);
System.out.println(p);
}
@GetMapping("/fun4")
public void fun3(@RequestHeader("token") String token,
@RequestHeader("p") String p) {
System.out.println(token);
System.out.println(p);
}
}
@CookieValue
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上。 @CookieValue 注解用于将请求的 Cookie 信息映射到处理的方法参数上
- 页面代码
自定义Header
t
t1
t2
t3
t4
function fun() {
let exp = new Date();
//页面中的cookie会自动传递到后台控制器
document.cookie= "cid=1234asdf1234"+exp.getTime();
$.ajax({
url: "demo/fun",
type: "GET"
})
}
- 控制器代码
@Controller
@RequestMapping("/demo")
public class DemoController {
@GetMapping("/fun")
public void fun(@CookieValue("cid") String cid){
System.out.println(cid);
}
}
@ModelAttribute
@ModelAttribute 注解主要是将请求参数绑定到 Model 对象上。 @ModelAttribute 注解只有 一个 value 属性,类型为 String, 表示的属性名称。 @ModelAttribute 注解用在方法或者方法的参数上:
-
用于方法上时:通常用来在@RequestMapping之前,为请求绑定需要从后台查询的model; 当 Controller 类中有任意一个方法被 @ModelAttribute 注解标记,页面请求只要进入这个控制器, 不管请求那个方法,均会先执行被 @ModelAttribute 标记的方法, 所以可以用@ModelAttribute注解的方法做一些初始化操作。当 同一个 Controller 类中有多个方法被@ModelAttribute 注解标记,所有被@ModelAttribute 标记的方法均会被执行,按先后顺序执行,然后再进入请求的方法
-
用于参数上时:用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于: A) @SessionAttributes 启用的attribute 对象上; B) @ModelAttribute 用于方法上时指定的model对象; C) 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。
Spring 框架会将 Model 传递给前端。 Model 的生命周期只存在于 HTTP 请求的处理过程中,请求处理完成后, Model 就销毁了。如果想让 参数在多个请求间共享,那么需要用到@SessionAttributes 注解。@SessionAttributes 注解只能声 明在类上,不能声明在方法上
示例:
@ModelAttribute
public User addToken(@RequestParam String token) {
return userService.findToken(token);
}
说明:上面方式实际的效果就是在调用@RequestMapping的方法之前,为request对象的model里put(“user”, User);
示例:
@RequestMapping(value="/user/{id}/edit", method = RequestMethod.POST)
public String bindUser(@ModelAttribute User user) {
}
说明:首先查询 @SessionAttributes有无绑定的User对象,若没有则查询@ModelAttribute方法层面上是否绑定了User对象,若没有则将URI template中的值按对应的名称绑定到User对象的各属性上。
@SessionAttributes:@SessionAttributes 注解让参数在多个请求间共享。@SessionAttributes 注解只能声 明在类上,不能声明在方法上。
@SessionAttributes注解有value、types两个属性,可以通过名字和类型指定要使用的attribute 对象; 示例:
@Controller
@RequestMapping("/user/edit")
@SessionAttributes("user")
public class EditUser {
// ...
}