如何通过显示JavaScript警报来处理ViewExpiredException? [英] How to handle ViewExpiredException by showing JavaScript alert?
问题描述
我已阅读问题处理ViewExireException/ajax并显示Primefaces对话框和BalusC的答案.我想通过显示带有刷新页面信息的警报来处理ViewExpiredException
.我已经采纳了BalusC对用户RequestContext
的建议来执行JavaScript,并且我删除了JSF重定向,因为我没有使用它:
I've read the question Handle ViewExireException/ajax and display a Primefaces dialog and the answer from BalusC. I'd want to handle the ViewExpiredException
by showing the alert with information to refresh the page. I've taken the suggestion from BalusC to user RequestContext
to put JavaScript to execute, and I've removed the JSF redirection because I'm not using it:
@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
处理程序执行handle方法时,出现了NullPointerException
的问题.我添加了return子句,添加后,效果是一样的:
The problem is, I got NullPointerException
when executing the handle method from wrapped
handler. I've added the return clause, and after adding it, the effect was the same:
[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)
[30.01.13 15:45:59:140 CET] 0000002e ErrorPageWrit E An exception occurred javax.faces.FacesException: java.lang.NullPointerException at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241) at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156) at my.project.web.handler.ViewExpiredExceptionExceptionHandler.handle(ViewExpiredExceptionExceptionHandler.java:59) at org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:191) at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
因此,执行了父句柄方法,认为应该从方法返回(已记录信息字符串).
So, the parent handle method is executed, thought there should be the return from method (The info string is logged).
我正在使用 PrimeFaces 3.4 和 MyFaces 2.0.7 ,这些都是 WebSphere 7 上的.
I'm using PrimeFaces 3.4 and MyFaces 2.0.7, everything on WebSphere 7.
我不明白这里发生了什么.有可能实现我想要的目标,如果是,那我做错了什么?
I don't understand what is happening here. Is it possible to achieve what I want, and if so, what I'm doing wrong?
推荐答案
最好的方法是在客户端处理该异常.这是非常简单的几行代码,并且完全透明:
The best way was to handle that exception on client side. It's very simple few-liner and it's completly transparent:
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
更改,这就是我在编程中所喜欢的.
No 2 new classes on server, no faces-config.xml
changes, this is what I love in programming.
这篇关于如何通过显示JavaScript警报来处理ViewExpiredException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!