With Spring, how can i retrieve the following Controller attributes in the view?
- Controller name
- Controller’s @RequestMapping URI
- Action method name
- Action method’s @RequestMapping URI
One approach which i have tried is by creating a subclass of HandlerInterceptorAdapter and overriding postHandle. I register my subclass as an mvc:interceptor for a list of given paths – which is clunky to maintain but was the only way to avoid my interceptor being called for ResourceHandler requests (which i don’t want). In my postHandle i can easily add the 2 name attributes, but not the URIs…
- Parsing from the HttpRequest object requires constraints on all Controller RequestMappings. I.e. i must always map /Controller/Action or equiv scheme. Quite limiting.
- Creating an ApplicationContext and querying that with the requestURI is too long-winded.
I am thinking about dropping the HandlerInterceptorAdapter and instead defining a BaseController for all my controllers to extend.
I wanted to ask before i do this, is there a better approach?
You haven’t stated why you need to do this (it sometimes helps to include your motivation, as others can suggest alternative approaches).
But I’m guessing that the Spring 3.1 features loosely termed “end point documentation” may do what you are asking… See
RequestMappingHandlerMappingin the Spring documentation which doesn’t provide a lot of detail, so this example project is the best place to see it in action: