JSF2.0 Some facesmessages not sent to redirected page on error handling

拥有回忆 提交于 2019-12-13 02:15:43

问题


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.

来源:https://stackoverflow.com/questions/34464737/jsf2-0-some-facesmessages-not-sent-to-redirected-page-on-error-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!