【问题标题】:how to get controller method name in Spring Interceptor preHandle method如何在 Spring Interceptor preHandle 方法中获取控制器方法名称
【发布时间】:2011-12-05 11:54:29
【问题描述】:

在我基于 spring mvc 和 spring security 的应用程序中,我使用@Controller 注解来配置控制器。

我已经配置了 Spring Handler Interceptor 并且在preHandle() 方法中,我想获取将被拦截器调用的方法名称。

我想在HandlerInterceptorpreHandle() 方法中的控制器方法上定义自定义注释,以便我可以通过记录该特定方法的活动来进行管理。

请看一下我的申请要求和代码

@Controller
public class ConsoleUserManagementController{
@RequestMapping(value = CONSOLE_NAMESPACE + "/account/changePassword.do", method = RequestMethod.GET)
@doLog(true)
public ModelAndView showChangePasswordPage() {
    String returnView = USERMANAGEMENT_NAMESPACE + "/account/ChangePassword";
    ModelAndView mavChangePassword = new ModelAndView(returnView);
    LogUtils.logInfo("Getting Change Password service prerequisit attributes");
    mavChangePassword.getModelMap().put("passwordModel", new PasswordModel());
    return mavChangePassword;
}
}

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
   // here I want the controller method name(i.e showChangePasswordPage() 
   // for /account/changePassword.do url ) to be called and that method annotation 
   // (i.e doLog() ) so that by viewing annotation , I can manage whether for that 
   // particular controller method, whether to enable logging or not.
}

我在我的应用程序中使用 SPRING 3.0

【问题讨论】:

  • 您是否检查过以下方式?

标签: spring-mvc annotations interceptor


【解决方案1】:

不了解 Handler 拦截器,但您可以尝试使用 Aspects 并为所有控制器方法创建通用拦截器。

使用方面,很容易访问您的连接点方法名称。

你可以在你的方面注入请求对象或使用:

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

从您的建议方法中检索它。

例如:

@Around("execution (* com.yourpackages.controllers.*.*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
public Object doSomething(ProceedingJoinPoint pjp){
 pjp.getSignature().getDeclaringType().getName();
}

【讨论】:

  • 感谢 Bob,但如果没有帮助,我想先尝试 Handler Interceptor 。那么我可以决定实现Aspect,
  • 是的 Bob,我终于用 Aspect 替换了 Handler Interceptor
  • @Vinicius Carvalho - 如果我真的不想使用 Aspect,你得到解决方案了吗?
猜你喜欢
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 2020-01-04
  • 2012-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多