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.
Share
I can think of two easy options:
Each
@Controllerclass exposes the data as a method annotated with@ModelAttribute, e.g.That’s not really nice if you have multiple controllers, though. Also, this has the annoying side-effect of encoding the
myDataon to the URL for every redirectI 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 yourOncePerRequestFilteridea, but a but more Spring-y.