Could Spring MVC call @ModelAttribute after @RequestMapping?

java, modelattribute, spring, spring-mvc

Solution

Each time you do a `POST`, make a redirection. That way, your `POST` method will only be responsible for updating the data, and the updated data will be loaded by the target controller.

So in this case, the `update()` method would redirect to another controller which would call the `getUser()` method before its `GET` method.

Problem

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?

Original source