【问题标题】:How to handle ViewExpiredException by showing JavaScript alert?如何通过显示 JavaScript 警报来处理 ViewExpiredException?
【发布时间】:2013-01-14 10:27:11
【问题描述】:

我已经阅读了Handle ViewExireException/ajax and display a Primefaces dialog 的问题和 BalusC 的回答。我想通过显示带有刷新页面信息的警报来处理ViewExpiredException。我接受了 BalusC 给用户 RequestContext 的建议以执行 JavaScript,并且我已经删除了 JSF 重定向,因为我没有使用它:

@Override
public void handle() throws FacesException {
    for (Iterator<ExceptionQueuedEvent> i = getUnhandledExceptionQueuedEvents().iterator(); i.hasNext();) {
        ExceptionQueuedEvent event = i.next();
        ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        if (t instanceof ViewExpiredException) {
            ViewExpiredException vee = (ViewExpiredException) t;
            try {
                log.info("Catched ViewExpiredException for view {}", vee.getViewId());
                RequestContext.getCurrentInstance().execute("handleViewExpired("+vee.getViewId()+")");
                return;
            } finally {
                i.remove();
            }
        } 
    }
    // At this point, the queue will not contain any ViewExpiredEvents.
    // Therefore, let the parent handle them.
    getWrapped().handle();

}

问题是,我在从wrapped 处理程序执行句柄方法时得到了NullPointerException。我加了return子句,加了之后效果还是一样的:

[30.01.13 15:45:59:140 CET] 0000002e ErrorPageWrit E 异常 发生了 javax.faces.FacesException:java.lang.NullPointerException 在 org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241) 在 org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156) 在 my.project.web.handler.ViewExpiredExceptionExceptionHandler.handle(ViewExpiredExceptionExceptionHandler.java:59) 在 org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:191) 在 org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)

所以,父句柄方法被执行,认为应该有来自方法的返回(信息字符串被记录)。

我正在使用 PrimeFaces 3.4MyFaces 2.0.7,一切都在 WebSphere 7 上。

我不明白这里发生了什么。是否有可能实现我想要的,如果可以,我做错了什么?

【问题讨论】:

  • 对于 MyFaces 的人来说,如果您告诉 MyFaces 版本会很有帮助(同时,请尝试最新版本并报告它是否仍然包含错误)。
  • 所以你说这是一个错误,这种形式的代码通常应该可以工作,而没有在处理中返回?
  • 最新版MyFaces:stackoverflow.com/questions/14230905/…,所以无法查看

标签: ajax jsf primefaces myfaces viewexpiredexception


【解决方案1】:

最好的方法是在客户端处理该异常。它非常简单,而且完全透明:

var originalPrimeFacesAjaxResponseFunction = PrimeFaces.ajax.AjaxResponse;
PrimeFaces.ajax.AjaxResponse = function(data, status, xhr) {
  var errorName = $(data.documentElement).find("error-name").text();
  if (errorName == 'javax.faces.application.ViewExpiredException') {
    alert('View has expired, redirection will follow');
    window.location.reload();
  } else {
    originalPrimeFacesAjaxResponseFunction.apply(this, arguments);
  }
};

服务器上没有 2 个新类,faces-config.xml 没有变化,这就是我喜欢编程的地方。

【讨论】:

  • 对不起,这是在 xhtml 中的 javascript 中吗?或者我把它放在哪里?我刚刚在给我 ViewExpiredException 的表单上方的 javascript 中进行了尝试,但它不起作用。我使用 Websphere 和 Richfaces。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2011-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多