Creating FacesMessage in action method outside JSF conversion/validation mechanism? 내 이전 질문에 따라, 나는 내 관리 콩 밖에있는 비즈니스 계층에서 던져 예외를 처리하기 위해 노력하고있어.JSF 2 : 비즈니스 예외를 처리하는 좋은 방법인가요?
전략은 비즈니스 예외를 검색하여 PhaseListener의 얼굴 메시지로 변환하는 것입니다.
내가 예상대로 작동하지만, 메신저 바퀴 만 다시 만들거나 잘못된 방법으로 올바르게 처리하는지 궁금합니다.
public class BusinessExceptionHandler implements PhaseListener {
@Override
public void afterPhase(PhaseEvent phaseEvent) {
ExceptionHandler exceptionHandler = phaseEvent.getFacesContext().getExceptionHandler();
// just debugging the handled exception, nothing here
/*
for (ExceptionQueuedEvent event : exceptionHandler.getHandledExceptionQueuedEvents()) {
ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
System.out.println("handled exception : " + context.getException());
}*/
for (Iterator<ExceptionQueuedEvent> it = exceptionHandler.getUnhandledExceptionQueuedEvents().iterator();
it.hasNext();) {
ExceptionQueuedEvent event = it.next();
ExceptionQueuedEventContext eventContext = (ExceptionQueuedEventContext) event.getSource();
Throwable e = eventContext.getException();
System.out.println("unhandled exception : " + e);
// get the root cause exception
while (e.getCause() != null) {
e = e.getCause();
}
System.out.println("cause exception : " + e +
", cause exception is BE : " + (e instanceof BusinessException));
// handle BE
if (e instanceof BusinessException) {
BusinessException be = (BusinessException) e;
System.out.println("processing BE " + be);
FacesMessage message = Messages.getMessage(
"com.corejsf.errors",
be.getMessage(),
be.getParamValues()
);
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, message);
it.remove(); // remove the exception
// works fine without this block, if BE is thrown, always return to the original page
/*
NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
System.out.println("navigating to " + context.getViewRoot().getViewId());
navigationHandler.handleNavigation(context, context.getViewRoot().getViewId(), null);
*/
}
}
}
@Override
public void beforePhase(PhaseEvent phaseEvent) {
}
@Override
public PhaseId getPhaseId() {
return PhaseId.INVOKE_APPLICATION;
}
}
감사합니다 :
여기에 내 코드 샘플 코드 조각입니다!
감사합니다, 알버트 캄
안녕하세요. 아이디어를 가져 주셔서 감사합니다. 페이지 282를 사용할 수 없습니다. 그러나 일부 인터넷 검색을 한 후에 edburn의 기사를 이런 종류의 문제에 대해 발견했으며 나중에이 연습을 사용합니다. http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception- jsf2 – bertie