You can use the Spring @ControllerAdvice annotation for the new controller class as follows:
@ControllerAdvice public class GlobalControllerAdvice { @ModelAttribute("user") public List<Exercice> populateUser() { User user = ; return user; } }
The "populateUser" method will be executed for each request, and since it has the @ModelAttribute annotation, the result of the method (user) will be placed in the model for each request.
The user will be available in your jsp using $ {user}, as this name was assigned by @ModelAttribute (example: @ModelAttribute ("fooBar") → $ {fooBar})
You can pass some arguments to the @ControllerAdvice annotation to indicate which controllers are recommended by this global controller. For example:
@ControllerAdvice(assignableTypes=FooController.class,BarController.class}) or @ControllerAdvice(basePackages="foo.bar.web.admin","foo.bar.web.management"}))
wesker317
source share