[英]Programmatically navigate to error page in JSF 2.0
在我的應用程序中,只要發生異常,我都願意導航到錯誤頁面。 RuntimeException
被處理和檢查,並再次引發。 為此,我在Faces config中定義了一個全局導航規則:
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>error</from-outcome>
<to-view-id>/error.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
在我的catch
塊中,我嘗試導航為:
FacesContext fctx = FacesContext.getCurrentInstance();
Application application = fctx.getApplication();
NavigationHandler navHandler = application.getNavigationHandler();
navHandler.handleNavigation(fctx,null, "error");
但這是行不通的。 我強行拋出Exception
但它沒有導航到所需的錯誤頁面。
任何指針對我都會非常有幫助。
我想建議使用ExternalContext
重定向error
頁面。 您必須在web.xml
配置您的Exception
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext extContext = context.getExternalContext();
String url = extContext.encodeActionURL(extContext.getRequestContextPath() + "/error");
extContext.redirect(url);
web.xml
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error</location>
</error-page>
如果您使用Jboss-Seam
,則很容易根據exception
重定向error/warnning
頁面。
pages.xml
<exception class="org.jboss.seam.security.NotLoggedInException">
<redirect view-id="/login.xhtml">
<message severity="warn">#{messages['org.jboss.seam.NotLoggedIn']}</message>
</redirect>
</exception>
<exception>
<redirect view-id="/error.xhtml">
<message severity="error">Unexpected error, please try again</message>
</redirect>
</exception>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.