4
我使用Ed Burns suggested custom exception handler在發生特定異常時顯示錯誤頁面。當上下文不活動時,我也嘗試顯示一個錯誤頁面。 然而,這部分(線45./46)引起麻煩:JSF 2.0自定義異常處理程序 - ViewHandler未在焊接應用程序中重定向
nav.handleNavigation(fc, null, "viewExpired");
fc.renderResponse();
這裏談到的全CustomExceptionHandler(正確配置愛德伯恩斯在他的Blog建議)
package com.sun.faces;
import java.util.Iterator;
import java.util.Map;
import javax.enterprise.context.ContextNotActiveException;
import javax.faces.FacesException;
import javax.faces.application.NavigationHandler;
import javax.faces.application.ViewExpiredException;
import javax.faces.component.UIViewRoot;
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;
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 fc = FacesContext.getCurrentInstance();
Map<String, Object> requestMap = fc.getExternalContext().getRequestMap();
NavigationHandler nav =
fc.getApplication().getNavigationHandler();
try {
// Push some useful stuff to the request scope for
// use in the page
requestMap.put("currentViewId", vee.getViewId());
nav.handleNavigation(fc, null, "viewExpired");
fc.renderResponse();
} finally {
i.remove();
}
}
if (t instanceof ContextNotActiveException) {
FacesContext fc = FacesContext.getCurrentInstance();
NavigationHandler nav =
fc.getApplication().getNavigationHandler();
try {
nav.handleNavigation(fc, null, "contextNotActive");
fc.renderResponse();
} finally {
i.remove();
}
}
}
// At this point, the queue will not contain any exceptions I want to handle (for now).
// Therefore, let the parent handle them.
getWrapped().handle();
}
}
不過,我總是得到在控制檯中打印出「視圖無法呈現」。那麼,爲什麼它不呈現該錯誤頁面。我認爲它是由不活躍的上下文(由NavigationHandler使用)造成的!
任何人都可以證實此行爲?
PS:我用的焊接1.1鑽嘴魚科2.0.4
你有viewExpired.xhtml? – bertie 2011-11-08 03:29:07
你在頁面中使用JSTL嗎?我發現我有一個NPE發生,因爲我試圖讀取設置在PhaseListener中的CDI bean中的信息。然而,當頁面被加載時,CDI bean首先被實例化並拋出NPE,因爲JSTL與JSF不同步。 – 2012-12-10 13:21:22