2011-12-05 93 views
3

在我的基于spring mvc和spring security的应用程序中,我使用了@Controller注释来配置控制器。如何在Spring Interceptor preHandle方法中获取控制器方法名称

我已经配置春处理器拦截preHandle()方法,我想这将是由拦截器调用方法名。

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

请看看我的应用需求和代码

@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在我的应用

+0

你检查做以下的方法是什么? –

回答

2

不知道有关处理程序拦截,但你可以尝试使用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(); 
} 
+0

感谢Bob,但是我想尝试第一个Handler Interceptor,如果它没有帮助的话。那么我可以决定实施看点, – Ketan

+0

是鲍勃,最后我已经用Aspect替换了Handler Interceptor – Ketan

相关问题