Use a HandlerInterceptor
, which is built for this purpose - you can specify a piece of logic to execute before or after the execution of your controllers (or a list of controllers - it is up to you to map an interceptor to the controllers it should apply to) by implementing a preHandle()
and/or postHandle()
method. The latter receives the ModelAndView
as an input, allowing you to add model attributes to it.
This way the logic to add some data to the model exists in just one place, can be selectively configured to apply to all or some pages, etc.