2011-12-05 89 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

相關問題