我跟着下面的文章來處理ViewExpiredException -由於ViewExpiredException導致重定向後保存面孔消息的最佳方式是什麼?
Dealing Gracefully with ViewExpiredException in JSF2
它做什麼是應該做的。但是,如果由於ViewExpiredException導致用戶被重定向到該視圖,我希望在視圖中顯示一些FacesMessage。所以,我使用NH.handlenaviage()重定向之前添加了一個FacesMessage。但它不起作用,原因是FacesMessage只存活一個請求處理週期。
起初,我的解決方案是將消息保存在會話中,並在下一個週期的恢復查看階段之前檢索它。
即使它的工作,我開始尋找一些標準溶液和我碰到了下面的文章的第一件事 -
Persist and pass FacesMessages over multiple page redirects
但沒有奏效。我認爲這是因爲PhaseListeners在ExceptionHandlers之前執行。所以,在這種情況下,它是無用的。如果異常處理代碼在偵聽器的afterPhase代碼之前執行,我認爲它會很有用。
然後,我遇到了下面的話題 -
Preserving FacesMessage after redirect for presentation through in JSF
而下面就是我現在所擁有的 -
@Override
public void handle() throws FacesException {
Iterator<ExceptionQueuedEvent> i = this.getUnhandledExceptionQueuedEvents().iterator();
while (i.hasNext()) {
ExceptionQueuedEvent event = i.next();
ExceptionQueuedEventContext eventContext = (ExceptionQueuedEventContext) event.getSource();
Throwable throwable = eventContext.getException();
if (throwable instanceof ViewExpiredException) {
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getFlash().setKeepMessages(true);
facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Your session expired!", null));
NavigationHandler nv = facesContext.getApplication().getNavigationHandler();
try {
facesContext.getViewRoot().setViewId("/login");
nv.handleNavigation(facesContext, null, "/login?faces-redirect=true");
facesContext.renderResponse();
} finally {
i.remove();
}
}
}
this.getWrapped().handle();
}
花了照顧的問題,但只有在意見同一個目錄。
而下面就是我在閃光範圍一番研究後,得到了 -
FacesMessage not displayed on page in subdirectory
任何人可以幫助我在這一個?我正在尋找一些處理這個問題的JSF(如果不是標準,那麼很好)。
謝謝。