I have a mid-size Spring application and I want to insert key/value pairs into all my ModelAndViews in a cross cutting fashion (much like AOP).
The motivation is to add all kind of data to all my pages (configuration values, build number, etc).
What is the easiest way to have this done? I prefer without AOP but I am ready to use it if necessary.
You have several options here.
HandlerInterceptorand add it to the config, and override thepostHandle()method, adding your data to the suppliedModelAndView.ViewResolverusing theattributesproperty ofUrlBasedViewResolver. This is only really useful for static string-based config that you can put into your beans file.@ModelAttribute. Any object returned by such annotated methods are added to the model automatically. This will only be done for requests handled by@Requestmapping-annotated methods in that controller class.