在我基于 spring mvc 和 spring security 的应用程序中,我使用 @Controller
注释来配置 Controller 。
我已经配置了 Spring Handler Interceptor 并且在 preHandle()
方法中,我想获取将被拦截器调用的方法名称。
我想在 HandlerInterceptor
的 preHandle()
方法中的 Controller 方法上定义自定义注解,以便我可以通过记录该特定方法的事件进行管理。
请看我的申请要求和代码
@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
最佳答案
不知道 Handler 拦截器,但您可以尝试使用 Aspects 并为所有 Controller 方法创建通用拦截器。
使用方面,可以很容易地访问您的连接点方法名称。
您可以在方面内注入(inject)请求对象或使用:
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();
}
关于spring-mvc - 如何在 Spring Interceptor preHandle 方法中获取 Controller 方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8384903/