尝试这种方法来处理视图过期:在您的项目中创建以下 2 个类
import javax.faces.context.ExceptionHandler;
import javax.faces.context.ExceptionHandlerFactory;
public class ViewExpiredExceptionExceptionHandlerFactory extends
ExceptionHandlerFactory {
private ExceptionHandlerFactory parent;
public ViewExpiredExceptionExceptionHandlerFactory(
ExceptionHandlerFactory parent) {
this.parent = parent;
}
@Override
public ExceptionHandler getExceptionHandler() {
return new ViewExpiredExceptionExceptionHandler(
parent.getExceptionHandler());
}
}
和
public class ViewExpiredExceptionExceptionHandler extends
ExceptionHandlerWrapper {
private ExceptionHandler wrapped;
public ViewExpiredExceptionExceptionHandler(ExceptionHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ExceptionHandler getWrapped() {
return this.wrapped;
}
@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;
FacesContext facesContext = FacesContext.getCurrentInstance();
Map<String, Object> requestMap = facesContext
.getExternalContext().getRequestMap();
NavigationHandler navigationHandler = facesContext
.getApplication().getNavigationHandler();
try {
// Push some useful stuff to the request scope for use in
// the page
requestMap.put("currentViewId", vee.getViewId());
navigationHandler.handleNavigation(facesContext, null,
"/index");
facesContext.renderResponse();
} finally {
i.remove();
}
}
}
// At this point, the queue will not contain any ViewExpiredEvents.
// Therefore, let the parent handle them.
getWrapped().handle();
}
}
在 faces-config.xml 添加以下行:
<factory>
<exception-handler-factory>path.to.class.ViewExpiredExceptionExceptionHandlerFactory</exception-handler-factory>
就是这样,你可以走了。
编辑
好的,我忘记了 Omnifaces 这样做的方式,我不使用它,因为一旦您将页面设置为错误,它就会警告您为 404 和另一个我现在不记得的页面创建页面。您应该在 faces-config.xml 中添加以下内容:
<factory>
<exception-handler-factory>org.omnifaces.exceptionhandler.FullAjaxExceptionHandlerFactory</exception-handler-factory>
并且您的代码将起作用。有关更多信息,请参阅omnifaces 网站http://showcase.omnifaces.org/exceptionhandlers/FullAjaxExceptionHandler;jsessionid=sOwX0FVqcY-InUhvDWEMksfQ中的 FullAjaxExceptionHandlerFactory 展示。
不要忘记将所需的 jar 添加到您的项目中。