1.表单
<form action="springmvc/testModelAndAttribute" method="post">
id:<input type="hidden" name="id" value="1"><br>
username:<input type="text" name="username" value="jinhan"><br>
age:<input type="text" name="age" value="20"><br>
email:<input type="text" name="email" value="123@qq.com"><br>
<input type="submit" value="提交"><br>
</form>
2.
/*运行流程
*1.执行@ModelAttribute注解修饰的方法:从数据库中取出对象,把对象放入到了Map中,键为:user
*2.SpringMvc从Map中取出User对象,并把表单的请求参数赋给该User对象的对应属性
*3.SpringMvc把上述对象传入目标方法的参数
*/
@ModelAttribute
public void getUser(@RequestParam(value="id", required=false) Integer id,
Map<String, Object> map){
if(id!=null){
User user=new User(1, "jinhan", "123456", 20, "123@qq.com");
map.put("user", user);
System.out.println("modelAttribut:"+user);
}
}
@RequestMapping("/testModelAndAttribute")
public String testModelAndAttribute(User user){
System.out.println("修改用户:"+user);
return SUCCESS;
}