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.如果提交的是一个对象
-
实体类
-
提交数据 : 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。