views:

52

answers:

1

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.

+3  A: 

You have several options here.

  1. Write a custom HandlerInterceptor and add it to the config, and override the postHandle() method, adding your data to the supplied ModelAndView.
  2. If your data is static, then you might be able to add it to the ViewResolver using the attributes property of UrlBasedViewResolver. This is only really useful for static string-based config that you can put into your beans file.
  3. Annotated a method in your controller(s) with @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.
skaffman
+1 great, the HandlerInterceptor solution is what I was looking for!
flybywire