Have you looked at Spring RedirectAttributes ? I have not used it myself, but it looks like it should do what you want. RedirectAttributes is commonly used for GET / redirect / POST templates, and at least one user seems to think that passed session attributes are bad practice, but they continue to mention that this is not the case. this seems to be the best solution. Anyway, the example shown in the documentation:
@RequestMapping(value = "/accounts", method = RequestMethod.POST) public String handle(Account account, BindingResult result, RedirectAttributes redirectAttrs) { if (result.hasErrors()) { return "accounts/new"; } // Save account ... redirectAttrs.addAttribute("id", account.getId()).addFlashAttribute("message", "Account created!"); return "redirect:/accounts/{id}"; }
will add a message attribute to RedirectModel, and if your controller redirects, then any method that handles redirection can access this data as follows:
@RequestMapping(value = "/accounts", method = RequestMethod.POST) public String handleRedirect(Model model) { String message = (String) model.asMap().get("message"); return new ModelAndView(); }
Thus, adding session attributes should be the same. Another link here .
EDIT I was looking through Spring's documentation, and they also mention this @SessionAttributes annotation. From the documentation:
Level-level @SessionAttributes annotations declare session attributes used by a particular handler. This typically lists the names of model attributes or types of model attributes that should be transparently stored in the session or in some dialog store, which serve as support for the beans form between subsequent requests.
Could this be what you need?
And also a link to the flash attribute documentation .
heisbrandon
source share