2010-11-23 2 views
3

폼 검증 예외가 @ExceptionHandler (유연해야 함)와 SimpleMappingExceptionResolver에 의해 일반적인 시스템 예외에 의해 처리되는 웹 어플리케이션을 개발했습니다. 이메일 통지 등).Spring Exception Handling - ExceptionResolver와 @ExceptionHandler를 동시에 사용하는 방법

둘 다 사용하는 경우 @ExceptionHandler에 매핑되는 예외는 @ExceptionHandler에 전달되지 않지만 ExceptionResolver는 defaultError로 움 직입니다.

함께 사용하려면 어떻게해야할까요? DispatcherPortlet에에서


@ActionMapping(params = "action=addDocOrder") 
    public void addDocOrder(@ModelAttribute("order") CstOrderBeanImpl orderBean, BindingResult result, ActionRequest actionRequest, ActionResponse response) 
throws PortalException, SystemException, ValidatorException { 
     logger.info("Adding Form Order"); 

     Calendar cal = TimeUtils.getEuDeadLine(orderBean); 
     orderBean.setDeadLine(cal.getTime()); 
     ValidatorException ve = validateService.validate(orderBean, result, actionRequest, validator); 
     if(ve != null) 
       throw ve; 

@ExceptionHandler(ValidatorException.class) 
public String handleFormException(ValidatorException ex, ActionRequest actionRequest) { 
    logger.error(ex.getMessage()); 
    //TODO make conditions 

    return "mainOrderForm"; 
    } 
는 인터셉터를 확인, 그러나 아무도 그것을 기본 ...

을하지 그래서 난 그냥 지적, 존재하지

catch (Exception ex) { 
      // Trigger after-completion for thrown exception. 
      triggerAfterActionCompletion(mappedHandler, interceptorIndex, processedRequest, response, ex); 

있다 , 그게 포틀릿 환경, 일반적으로 적은 "전망"... 매개 변수에 의해 주도 ..

답변

2

아마도 ExceptionResolver의 임시 해결 방법으로 원하는 처리기 메서드를 동적으로 호출 할 수 있습니다. 최대한 빨리 귀하의 경우 기본들 (AnnotationMethodHandlerExceptionResolver의 인스턴스를 중지 DispatcherServlet이 응용 프로그램 컨텍스트에 적어도 하나 개의 예외 리졸버를 넣어 그것은 정확히 우아한 솔루션,하지만 작동합니다 것

public ModelAndView resolveException(RenderRequest req, RenderResponse res, Object handler,Exception exc) { 
    if(exc instanceof ValidatorException) { 
     try { 
      java.lang.reflect.Method m = handler.getClass().getMethod("someMethod", (Class<?>)null /* method parameters? */); 
      m.invoke(handler,new Object[]{(ValidatorException)exc,req/*, res - maybe?*/}); 
     } catch(Exception e) { 
      // Handle exception 
     } 
    } 

    // Send email of the error etc.. 
} 

..

+0

안녕하세요 heikkim, 이것은 정확히 내가 한 일입니다. 처리기에서 메서드를 호출하지 않았지만 방금 확인했습니다. instanceof ValidatorException) 내가 필요한 것을했습니다. 하지만 당신이 제안한 것이 더 합리적입니다. 논리가 핸들러에있을 때 예외가 아니라면 더 투명합니다. – lisak

0

포틀릿 환경에 익숙하지 않지만 아마도 AnnotationMethodHandlerExceptionResolver을 선언해야 할 수도 있습니다.

+0

안녕하세요, AnnotationMethodHandlerExceptionResolver가 기본적으로 컨텍스트에 있습니다. – lisak

2

을).

하면 앱 컨텍스트에 AnnotationMethodHandlerExceptionResolver아마도 SimpleMappingExceptionResolver를 모두 넣고도 자신의 순서를 부과해야합니다.

자세한 내용은 my post을 참조하십시오.

+0

고마워요! :-) – Kimble