2011-12-05 8 views
3

私のアプリケーションでは、spring mvcとspring securityに基づいて、コントローラを設定するのに@Controller注釈を使用しています。SpringインターセプタpreHandleメソッドでコントローラメソッド名を取得する方法

スプリングハンドラインターセプタpreHandle()メソッドでは、インターセプタで呼び出されるメソッド名を取得したいと思います。

preHandle()メソッドHandlerInterceptorのコントローラメソッドで定義されたカスタムアノテーションを取得して、その特定のメソッドのアクティビティを記録することで管理できるようにしたいとします。

ハンドラインターセプターについて知っている、しかし、あなたはいけない、私は自分のアプリケーション

+0

チェックしましたか? –

答えて

2

でSPRING 3.0を使用しています私のアプリケーションの要件とコード

@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. 
} 

を見てください。アスペクトを使用して、すべてのコントローラメソッドに対して一般的なインターセプタを作成しようとする可能性があります。

aspectを使用すると、joinpointメソッド名に簡単にアクセスできます。

あなたはあなたの側面または使用の内側にリクエストオブジェクトを挿入することができます

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を試してみたいと思います。 Aspectを実装することができます。 – Ketan

+0

はいBob。最後に、私はHandler InterceptorをAspectに置き換えました – Ketan

関連する問題