JSF:无法捕获ViewExpiredException

Ing*_*her 8 java jsf jsf-2 viewexpiredexception

我正在Glassfish v3上开发一个JSF 2.0应用程序,我正在尝试处理ViewExpiredException.但无论我做什么,我总是得到一个Glassfish错误报告,而不是我自己的错误页面.

为了模拟VEE的发生,我将以下函数插入到我的支持bean中,它会激活VEE.我通过commandLink从我的JSF页面触发此函数.代码:

@Named
public class PersonHome {
  (...)
  public void throwVEE() {
    throw new ViewExpiredException();
  }
}
Run Code Online (Sandbox Code Playgroud)

起初我只是通过向我的web.xml添加错误页面来尝试它:

<error-page>
  <exception-type>javax.faces.application.ViewExpiredException</exception-type>
  <location>/error.xhtml</location>
</error-page>  
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我没有被重定向到错误,但我显示了Glassfish错误,它显示了HTTP状态500页面,其中包含以下内容:

description:The server encountered an internal error () that prevented it from fulfilling this request.
exception: javax.servlet.ServletException: javax.faces.application.ViewExpiredException
root cause: javax.faces.el.EvaluationException:javax.faces.application.ViewExpiredException
root cause:javax.faces.application.ViewExpiredException
Run Code Online (Sandbox Code Playgroud)

我尝试的下一件事是编写ExceptionHandlerFactory和一个CustomExceptionHandler,如JavaServerFaces 2.0 - 完整参考中所述.所以我将以下标记插入faces-config.xml:

<factory>
  <exception-handler-factory>
    exceptions.ExceptionHandlerFactory
  </exception-handler-factory>
</factory>
Run Code Online (Sandbox Code Playgroud)

并添加了这些类:工厂:

package exceptions;

import javax.faces.context.ExceptionHandler;

public class ExceptionHandlerFactory extends javax.faces.context.ExceptionHandlerFactory {

    private javax.faces.context.ExceptionHandlerFactory parent;

    public ExceptionHandlerFactory(javax.faces.context.ExceptionHandlerFactory parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getExceptionHandler() {
        ExceptionHandler result = parent.getExceptionHandler();
        result = new CustomExceptionHandler(result);
        return result;
    }

}
Run Code Online (Sandbox Code Playgroud)

自定义异常处理程序:

package exceptions;

import java.util.Iterator;

import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerWrapper;
import javax.faces.context.FacesContext;
import javax.faces.event.ExceptionQueuedEvent;
import javax.faces.event.ExceptionQueuedEventContext;

class CustomExceptionHandler extends ExceptionHandlerWrapper {

    private ExceptionHandler parent;

    public CustomExceptionHandler(ExceptionHandler parent) {
        this.parent = parent;
    }

    @Override
    public ExceptionHandler getWrapped() {
        return this.parent;
    }

    @Override
    public void handle() throws FacesException {
        for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
            ExceptionQueuedEvent event = i.next();
            System.out.println("Iterating over ExceptionQueuedEvents. Current:" + event.toString());
            ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
            Throwable t = context.getException();
            if (t instanceof ViewExpiredException) {
                ViewExpiredException vee = (ViewExpiredException) t;
                FacesContext fc = FacesContext.getCurrentInstance();

                NavigationHandler nav =
                        fc.getApplication().getNavigationHandler();
                try {
                    // Push some useful stuff to the flash scope for
                    // use in the page
                    fc.getExternalContext().getFlash().put("expiredViewId", vee.getViewId());

                    nav.handleNavigation(fc, null, "/login?faces-redirect=true");
                    fc.renderResponse();

                } finally {
                    i.remove();
                }
            }
        }
        // At this point, the queue will not contain any ViewExpiredEvents.
        // Therefore, let the parent handle them.
        getWrapped().handle();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我仍然没有重定向到我的错误页面 - 我得到的是与上面相同的HTTP 500错误.我做错了什么,在我的实现中可能缺少哪些异常未正确处理?任何帮助高度赞赏!

编辑

好吧,老实说.事实上,我的代码实际上是用Scala编写的,但这是一个很长的故事.我一直认为这是一个Java问题.在这种情况下,真正的错误是我自己的愚蠢.在我的(Scala)代码中,在CustomExceptionHandler中,我忘了添加带有"i.remove();"的行.所以ViewExpiredException在处理完之后仍留在UnhandledExceptionsQueue中,并且"冒泡".当它冒泡时,它就变成了ServletException.

我真的很抱歉让你们两个都迷惑!

Bal*_*usC 15

这个测试用例是假的.在ViewExpiredException通常恢复视图(因为它缺少在会议)期间,未呈现响应,也没有实例化的bean时只被扔出.在您的情况下,在实例化bean期间抛出此异常,并且此异常已包装在a中ServletException.

真正 ViewExpiredException当您在HTTP会话过期发送一个HTTP POST请求到服务器通常只抛出.因此,基本上有两种方法可靠地再现它:

  1. h:form在Web浏览器中打开一个带有POST表单(默认情况下已经是POST)的JSF页面,关闭服务器并清理其工作目录(这一点很重要,因为大多数服务器会在关机时将打开的会话序列化为磁盘并在启动时将其反序列化),重启服务器并提交已打开的表单.A ViewExpiredException将被抛出.

  2. <session-timeout>in 设置web.xml1分钟并在使用POST表单打开JSF页面后1分钟内提交表单.这也会抛出ViewExpiredException.

  • 因为抛出了"ServletException". (3认同)