2011-12-05 2 views
3

스프링 mvc 및 스프링 보안을 기반으로하는 응용 프로그램에서 컨트롤러를 구성하는 데 @Controller 주석을 사용하고 있습니다.Spring 인터셉터 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. 
} 

내가

+0

당신은 그것을 다음과 같은 방법을 수행 체크 했습니까? –

답변

2

은 핸들러 인터셉터에 대해 알고하지 마십시오 내 응용 프로그램에서 SPRING 3.0을 사용하고있다, 그러나 당신하세요 Aspects를 사용하고 모든 컨트롤러 메소드에 대해 일반적인 인터셉터를 만들려고 시도 할 수 있습니다.

측면을 사용하면 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, 마침내 Aspect로 Handler Interceptor를 대체했습니다. – Ketan

관련 문제