添加一个 ModelAttribute 填充方法怎么样?
@ModelAttribute("myObject")
public MyObject modifyBeforeValidate(
@ModelAttribute("myObject") MyObject myObject) {
//modify it here
return myObject;
}
副作用是如果我没记错的话,这个方法将在每个 @RequestMapping 方法之前被调用。
更新1:示例
@ModelAttribute("command")
public ChangeOrderCommand fillinUser(
@ModelAttribute("command") ChangeOrderCommand command,
HttpServletRequest request) {
command.setUser(securityGateway.getUserFrom(request));
return command;
}
@RequestMapping(value = "/foo/bar", method = RequestMethod.POST)
public String change(@ModelAttribute("command") ChangeOrderCommand command,
BindingResult bindingResult, Model model, Locale locale) {
}