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!
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.
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.
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.
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.
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 requestModelAndView
- there you need to add the new information about the handling Methodcomplete 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
!
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
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