JSP/页面中的Spring MVC Controller类名 [英] Spring MVC Controller classname from JSP / page

查看:75
本文介绍了JSP/页面中的Spring MVC Controller类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,还有一个我需要尽快解决的问题...在解决这些问题的一些替代方法的过程中,我拔掉了头发.

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.

看,我需要从JSP页面显示当前服务的Spring Controller.名称不必在JSP本身中进行解析,我实际上是在使用另一个类(一种标签库)在每页的底部显示此信息.

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.

是否有一种方法可以从外部类中获取控制器名称? (当我在外面说时,我的意思是来自控制器本身以外的其他类.) 也许是从请求以某种方式? (或者一些Spring Security请求属性?). 也许是拦截器?

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.

任何建议都可以申请!

推荐答案

您可以实现

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

  • Object handler-可以强制转换为HandlerMethod,其中包含有关已处理请求的Controller方法的信息
  • ModelAndView-您需要在其中添加有关处理方法的新信息
  • 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

完整的方法签名org.springframework.web.servlet.HandlerInterceptor#postHandle:

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

*可以直接扩展HandlerInterceptorAdapter,而不是直接实现HandlerInterceptor,这是一种方便的抽象类,它以空主体实现HandlerInterceptor的所有方法,因此只需要重写所需的方法.

* 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.

*请勿混淆,有两个HandlerInterceptor类,一个用于Servlet,一个用于Portlet(org.springframework.web.portlet.HandlerInterceptor).将HandlerInterceptor用于Servlet oorg.springframework.web.servlet.HandlerInterceptor

* 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 !

这篇关于JSP/页面中的Spring MVC Controller类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆