I have a menu that is data driven(cached) and it is a global component. I want to be able to inject the menu items for every request since every page is going to be using it. What is the best place to put it? I'm using annotation based Spring3. Best solution I can think of is using a OncePerRequestFilter and adding it there or sub-classing the Controller, but not sure how to do that with @Controller annotation.
One of the most important Spring MVC annotations is the @ModelAttribute annotation. @ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view.
Method level ModelAttribute annotation cannot be mapped directly with any request. Let's take a look at the following example for better understanding. We have 2 different methods in the above example.
I can think of two easy options:
Each @Controller
class exposes the data as a method annotated with @ModelAttribute
, e.g.
@ModelAttribute
public MyData getMyData() {
...
}
That's not really nice if you have multiple controllers, though. Also, this has the annoying side-effect of encoding the myData
on to the URL for every redirect
I suggest instead that implement a HandlerInterceptor
, and expose the data to every request that way. You can't use any annotation-lovin, but it's better separated from your business logic this way. This is similar to your OncePerRequestFilter
idea, but a but more Spring-y.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With