全局异常处理,不应该离开。在页面上显示错误

时间:2014-01-21 20:41:00

标签: ajax jsf-2 exception-handling

我在jsf 2.0中实现了全局异常处理,遵循以下代码示例: http://www.openlogic.com/wazi/bid/259014/How-to-add-exception-handling-to-JSF-applications 我不希望用户导航到错误页面。我希望用户在同一页面上显示一条消息。 所以代码现在看起来像这样:

public class CustomExceptionHandler extends ExceptionHandlerWrapper
{
    private ExceptionHandler wrapped;

    public CustomExceptionHandler(ExceptionHandler wrapped){
        this.wrapped = wrapped;

    }

    @Override
    public ExceptionHandler getWrapped(){
        return wrapped;
    }

    @Override
    public void handle() throws FacesException{
        Iterator iterator = getUnhandledExceptionQueuedEvents().iterator();

        while (iterator.hasNext()){
            ExceptionQueuedEvent event = (ExceptionQueuedEvent)iterator.next();
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext)event.getSource();
            Throwable throwable = context.getException();
            FacesContext fc = FacesContext.getCurrentInstance();

            final ExternalContext externalContext = fc.getExternalContext();
            final Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
            final ConfigurableNavigationHandler nav = (ConfigurableNavigationHandler) fc.getApplication().getNavigationHandler();
            //here you do what ever you want with exception
            try {
            fc.getExternalContext().setResponseStatus(500);
            fc.addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "There is an error.", ""));
            }
            finally {
                //remove it from queue
                iterator.remove();
            }
        }
        getWrapped().handle();
    }
}

但是使用此代码,页面既不会导航,也不会显示消息。 如果我删除这行代码:

fc.getExternalContext().setResponseStatus(500);

然后用户转到下一页,该消息显示在该页面上。

知道如何在同一页面上显示错误消息而不是导航用户吗?

由于

0 个答案:

没有答案