Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Framework MVC Base Controller Method

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?

like image 1000
Nik Avatar asked Nov 07 '11 20:11

Nik


2 Answers

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.

like image 171
Jacob Mattison Avatar answered Oct 05 '22 05:10

Jacob Mattison


There are several extension points in Spring and Spring MVC you can take:

  • HandlerInterceptor - a simple way to intercept each handler method
  • Spring AOP - before/after advice matching all your controllers
  • WebArgumentResolver - maybe you can somehow customize resolving arguments and process them before calling the controller?
like image 28
Tomasz Nurkiewicz Avatar answered Oct 05 '22 03:10

Tomasz Nurkiewicz