Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC Controller classname from JSP / page

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!

like image 608
Robin Jonsson Avatar asked Jan 22 '14 08:01

Robin Jonsson


People also ask

How can we call controller method from JSP in Spring MVC?

and [1] copy the <button/> and <script/> elements to your JSP, [2] change the URL to point to your controller and, [3] create an element <div id="demo"></div> in your JSP then, on clicking the button in your page, this <div/> should be updated to display the String returned by your controller.

What is HandlerMapping?

HandlerMapping is an interface that defines a mapping between requests and handler objects. While Spring MVC framework provides some ready-made implementations, the interface can be implemented by developers to provide customized mapping strategy.

What is the use of ViewResolver in Spring MVC?

The ViewResolver provides a mapping between view names and actual views. The View interface addresses the preparation of the request and hands the request over to one of the view technologies.

What is controller class in Spring MVC?

Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be applied to classes only. It's used to mark a class as a web request handler. It's mostly used with Spring MVC applications.


2 Answers

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 !

like image 165
Ralph Avatar answered Oct 11 '22 20:10

Ralph


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

like image 23
NimChimpsky Avatar answered Oct 11 '22 20:10

NimChimpsky