Controller中的数据处理

Controller中的数据处理

1、处理提交的数据

1.提交的域名和处理的方法名参数名一致

提交数据 : http://localhost:8080/hello?name=hehehe

@RequestMapping("/hello")
public String hello(String name){
	System.out.println(name);
	return "hello";
}

后台输出hehehe

2.提交的域名和处理的方法名参数名不一致
提交数据 : http://localhost:8080/hello?username=hehehe

//@RequestParam("username") : username提交的域的名称 .
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
	System.out.println(name);
	return "hello";
}

后台输出hehehe

3.如果提交的是一个对象

  1. 实体类

  2. 提交数据 : http://localhost:8080/mvc04/user?name=hehehe&id=1&age=15

@RequestMapping("/user")
public String user(User user){
	System.out.println(user);
	return "hello";
}

后台输出 : User { id=1, name=‘hehehe’, age=15 }
说明:如果使用对象的话,前端传递的参数名和对象名必须一致,否则就是null。


版权声明:本文为Al_0922原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。