簡體   English   中英

以編程方式導航到JSF 2.0中的錯誤頁面

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM