@RequestMapping(path = {"/savePerson"})
@ResponseBody
public String save( Person person) {
logger.info("person:{}",person);
return "success";
}
2、如果通过form表单提交post请求,后端可以使用java对象、Map、字段名称三种方式接收,需要注意的是只能通过@RequestParam对java对象和Map对象修饰,否则无法接收到参数信息(不能通过@RequestBody接收)
@RequestMapping(path = {"/save1"}, method = RequestMethod.POST)
public String save1( User user) {
logger.info("USER:{}",user.toString());
return "success";
}
@RequestMapping(path = {"/save2"}, method = RequestMethod.POST)
public String save2(@RequestParam Map user) {
logger.info("id:{}",user.get("id"));
logger.info("name:{}",user.get("name"));
logger.info("age:{}",user.get("age"));
return "success";
}
@RequestMapping(path = {"/save3"}, method = RequestMethod.POST)
public String save3( String id,String name,String age) {
logger.info("id:{}",id);
logger.info("name:{}",name);
logger.info("age:{}",age);
return "success";
}
3、Controller可以接收的参数类型
HttpServletRequest对象:请求对象
HttpServletResponse对象:响应对象
Model(ModelMap、map)对象:模型数据, 用于视图渲染,这些都可以作为视图渲染的数据,
HttpSession对象:会话,
HttpEntity
ResponseEntity
HttpMethod:请求方法
RedirectAttributes:重定向时使用的属性,在 url中拼接参数,或者返回的页面需要传递 Model。
BindingResult:校验方法参数后的错误
java对象: User user
Map对象:@RequestParam Map
具体字段名称:String name
参考资料:springmvc官网



