I have a number of controller methods spread over a number of classes. Every method takes a Model
object and all of my methods populate some shared properties into the model (control the navigation bar display mostly). Is there a way for me to plug a 'base' method into Spring? I want one method that can populate my shared properties and then go into the specific controller method (or reversed for that matter). Does anybody know how to do that?
Sounds like a HandlerInterceptor might be a good approach.
It's a bit AOP like -- you can define a class that has a PreHandle
or PostHandle
method, and configure which requests it will run on. In your case you probably want a PostHandle
, since that will give you access to the ModelAndView, so you can populate it with the shared items.
There are several extension points in Spring and Spring MVC you can take:
HandlerInterceptor
- a simple way to intercept each handler methodWebArgumentResolver
- maybe you can somehow customize resolving arguments and process them before calling the controller?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