I have a controller like this:
@Controller
public class HomeController {
@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(@RequestParam("user") User user, ModelMap model){
SaveUserToDatabase(user);
return "index";
}
@ModelAttribute("user")
String getUser() {
return LoadCurrentUserFromDataBase();
}
}
In short, my views would render user
in almost every actions in HomeController
,
but I don't want to code:
model.addAttribute("user", LoadCurrentUserFromDataBase())
in every actions, instead I'm seeking a way like @ModelAttribute
to expose user
to all my views.
However, according to the docs, @ModelAttribute
methods in a controller are invoked before @RequestMapping
methods, within the same controller.
As to my code, getUser
is called before update
, but i'd like to get the updated user.
Is there a way to expose the user
attribute after actions without explicitly call model.addAttribute
in every actions?