如何在Spring Interceptor preHandle方法中获取控制器方法名称
在我基于 spring mvc 和 spring security 的应用程序中,我使用 @Controller 注释来配置控制器。
我已经配置了 Spring Handler Interceptor 并在 preHandle()
方法中,我想获取将由拦截器调用的方法名称。
我想要在 HandlerInterceptor
的 preHandle()
方法中的控制器方法上定义自定义注释,以便我可以通过记录该特定方法的活动进行管理。
请查看我的应用程序要求和代码
@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
In my application based on spring mvc and spring security I am using @Controller
annotation to configure controller.
I have configured Spring Handler Interceptor and in preHandle()
method , I want to get method name which is going to be call by interceptor.
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.
}
I am using SPRING 3.0 in my application
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道 Handler 拦截器,但您可以尝试使用方面并为所有控制器方法创建通用拦截器。
使用方面,可以轻松访问连接点方法名称。
您可以将请求对象注入方面或使用:
从您的建议方法中检索它。
例如:
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:
To retrieve it from your advice method.
For instance: