Creating FacesMessage in action method outside JSF conversion/validation mechanism? में मेरे पिछले प्रश्न के बाद, मैं अपने प्रबंधित सेम के बाहर व्यापार परत से फेंकने वाले अपवादों को संभालने की कोशिश कर रहा हूं।जेएसएफ 2: क्या व्यापार अपवादों को संभालने के लिए यह एक अच्छा तरीका है?
रणनीति एक फेजलिस्टर में संदेशों के चेहरे पर व्यापार अपवादों को खोजना और परिवर्तित करना है।
यह मेरी अपेक्षा के अनुसार काम कर रहा है, लेकिन मुझे आश्चर्य है कि क्या मैं सिर्फ पहिया को फिर से शुरू कर रहा हूं, या गलत तरीके से इसे सही कर रहा हूं?
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;
}
}
धन्यवाद:
यहाँ मेरी कोड नमूना टुकड़ा है!
सादर, अल्बर्ट Kam
हाय फिर से। विचार के लिए धन्यवाद। पृष्ठ 282 उपलब्ध नहीं है। लेकिन कुछ googling के बाद, मुझे इस तरह के मामले के बारे में एडबर्न का लेख मिला, और बाद में इस अभ्यास का उपयोग करेगा: http://weblogs.java.net/blog/edburns/archive/2009/09/03/dealing-gracefully-viewexpiredexception- jsf2 – bertie