本文介绍了如何在 Spring Interceptor preHandle 方法中获取控制器方法名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my application based on spring mvc and spring security I am using @Controller annotation to configure controller.

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

I have configured Spring Handler Interceptor and in preHandle() method , I want to get method name which is going to be call by interceptor.

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

I want to get custom annotation defined on controller method in preHandle() method of HandlerInterceptor so that I can manage by logging activity for that particular method.

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

Please have a look at my application requirement and code

@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

I am using SPRING 3.0 in my application

推荐答案

不知道 Handler 拦截器,但您可以尝试使用 Aspects 并为您的所有控制器方法创建一个通用拦截器.

Don't know about the Handler interceptor, but you could try to use Aspects and create a general interceptor for all your controller methods.

使用方面,可以很容易地访问您的连接点方法名称.

Using aspects, it would be easy to access your joinpoint method name.

您可以在切面中注入请求对象或使用:

You can inject the request object inside your aspect or use:

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

从你的通知方法中检索它.

To retrieve it from your advice method.

例如:

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

这篇关于如何在 Spring Interceptor preHandle 方法中获取控制器方法名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 21:32