5

I have a question, and a problem that I need fixed soon... I have pulled my hair out while going through some alternatives on how to solve this problem.

See, I have a need to display the current serving Spring Controller from a JSP page. The name doesn't have to be parsed in the JSP itself, I'm actually using another class (a kind of taglibrary) to display this information on the bottom of ever page.

Is there a way to get the controller name from an outside class? (When i say outside, I mean from another class than the Controller itself.) Perhaps from the request somehow? (Or some Spring Security request attribute?). Perhaps an interceptor?

I would like to avoid extending a class from the controller just to fix this issue.

Any advice is appriciated!

Robin Jonsson
  • 2,761
  • 3
  • 22
  • 42
  • possible duplicate of [How to show all controllers and mappings in a view](http://stackoverflow.com/questions/9766800/how-to-show-all-controllers-and-mappings-in-a-view) – NimChimpsky Jan 22 '14 at 08:22
  • By currently serving controller, do you mean the one which just processed the request and presented the currently loaded view. – Pratik Shelar Jan 22 '14 at 08:22
  • possible duplicate of [Get name of Spring controller and method in view](http://stackoverflow.com/questions/17169722/spring-3-web-mvc-get-name-of-controller-and-method-in-view) – andyb Jan 22 '14 at 08:23
  • @Pratik Yes, exactly. And I'd like to get that information from outside the controller itself. I don't want to edit a bunch of controllers just to get this to work, I'd like to get this working from perhaps an interceptor, or an outside class. – Robin Jonsson Jan 22 '14 at 08:35

3 Answers3

4

You can implement a HandlerInterceptor* it has a method postHandle, that has the two parameters you need:

  • Object handler - that the can be cast to HandlerMethod, contains the information about the Controller Method that has handled the request
  • ModelAndView - there you need to add the new information about the handling Method

complete Method signature org.springframework.web.servlet.HandlerInterceptor#postHandle:

void postHandle(HttpServletRequest request,
                HttpServletResponse response,
                Object handler,
                ModelAndView modelAndView)
      throws Exception;

* Instead of implementing a HandlerInterceptor directly, one can extend HandlerInterceptorAdapter - that is a convenient abstract class that implements all methods of HandlerInterceptor with an empty body, so that one only need to override the methods that are needed.

* Don't get confused, there are two HandlerInterceptor classes, one for Servlets, and one for Portlets (org.springframework.web.portlet.HandlerInterceptor). Use the HandlerInterceptor for Servlets oorg.springframework.web.servlet.HandlerInterceptor !

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • To clearify how this solved me problem. First, I cast `handler` to an `HandlerMethod`, then get the classname from `handlerMethod.getMethod().getDeclaringClass().getName()`, and set it as a request attribute, which I then can use in my custom taglib. – Robin Jonsson Jan 24 '14 at 07:20
1

The normal java way, from mwithin the controller itself probably won;t work as spring creates a proxy of the original class :

String className = this.getClass().getSimpleName();

I think you need to implement a request mapping handler

Community
  • 1
  • 1
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0

You can create a after advise interceptor which will be called after your controller is called. In this interceptor you can get hold of the pointCut method on which this advice was applied. Once you have that you can add it as a property in ModelAndView map.

Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51