SpringMVC ModelAttribute注解

一、方法定义上使用 @ModelAttribute 注解,Spring MVC 在调用目标处理方法前,先逐个调用在方法级上标注了@ModelAttribute 的方法。

 1)通过主键或唯一键获取用户对象,形成更新请求数据,其它字段保持不变。 

//方法级	
@ModelAttribute
public void getTerminal(@RequestParam(value="posId",required=false)String posId,ModelMap map){
		if(posId != null && !"".equals(posId.trim())){
			Terminal terminal = terminalService.getById(posId);
			map.put("terminalBean", terminal);
		}
 }

2)编辑对象,从隐含对象中获取隐含的模型数据中获取对象,再将请求参数绑定到对象中,再传入入参。

public ResultInfo update(@ModelAttribute(value="terminalBean")Terminal terminal){
}

二、运行过程:
1. 执行 @ModelAttribute 注解修饰的方法: 从数据库中取出对象, 把对象放入到了 Map 中. 键为: terminalBean
2. SpringMVC 从 Map 中取出 Terminal 对象, 并把表单的请求参数赋给该 Terminal 对象的对应属性.
3. SpringMVC 把上述对象传入目标方法的参数.

原文地址:https://www.cnblogs.com/walkwithmonth/p/10741552.html