The question is simple: Once a user is authenticated in my Spring MVC application, I'd like to show in the navigation menu the count of all unread messages, in every page.
For a specific page, I can do this inside its controller:
@RequestMapping(value = "/some/url", method = RequestMethod.GET)
public String somePage(Model model, Principal principal) {
// Count messages and add to the view
int countMessages = userService.countAllUnreadMessages(principal.getName());
model.addAttribute("countMessages", countMessages);
// ...
return "some/view";
}
The question is how to avoid doing the same call in all methods of every controller? Is there a way to pass a data parameter to the view for all the pages? (Maybe all pages, excluding the /login
form, of course)