-->

JSF2.0 Some facesmessages not sent to redirected p

2019-08-12 15:15发布

问题:

I'm building a JSF2.0 application on TomEE1.7.3, and I've created a custom ExceptionHander, and overrided handle() method, which looks like below:

@Override
public void handle() {
    for (Iterator<ExceptionQueuedEvent> it = getUnhandledExceptionQueuedEvents().iterator(); it.hasNext();) {
        ExceptionQueuedEventContext eventContext = it.next().getContext();
        FacesContext facesContext = eventContext.getContext();
        ExternalContext externalContext = facesContext.getExternalContext();
        Throwable throwable = eventContext.getException();
        if (throwable instanceof ViewExpiredException) {
            facesContext.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "",
                    "oops, view state error! This FacesMessage is sent to error.xhtml, just as expected."));
        } else {
            facesContext.addMessage(null, new FacesMessage(
                    FacesMessage.SEVERITY_INFO, "",
                    "oops, some OTHER error! This FacesMessage is NOT sent to error.xhtml, " + 
                    "I want full stacktrace here, string length is irrelevant to the problem ..."));
        }
        externalContext.getFlash().setKeepMessages(true);
        externalContext.redirect("error.xhtml");
        it.remove();
    }
    wrapped.handle();
}

When I catch a throwable of ViewExpiredException, I CAN get FacesMessages at @PostConstruct method of "ErrorBean"(which is attached to "error.xhtml"). But if any other type of throwable comes, I can NOT get any FacesMessages. "ErrorBean" is a backing bean class with these 2 annotations:

@javax.faces.bean.ManagedBean
@javax.enterprise.context.RequestScoped

I'm assuming that its relevant to the "Life Cycle of JSF", OR maybe because I have 2 beans attached to 1 page, but I can't figure out why this is happening.

BTW, what I really want to do is output stacktrace on the same url (without http 3xx redirect), I tried something like below:

final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler)             
        facesContext.getApplication().getNavigationHandler();
nav.performNavigation("error.xhtml");

but did not work out either (another bean for this page will show up and no faces messages), so at this moment, I decide to go with redirect.

Please help me out! Thanks.