2012-11-24 53 views
0

在我的應用程序中,我願意在出現異常時導航到錯誤頁面。 RuntimeException被處理並檢查並再次拋出。對於我在面配置定義的全局導航規則:以編程方式導航到JSF 2.0中的錯誤頁面

<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,但它沒有導航到所需的錯誤頁面。

任何指針都會對我非常有幫助。

+1

這不正是在JSF處理異常的正確方法。長話短說,檢查[此演示頁面](https://showcase-omnifaces.rhcloud.com/showcase/exceptionhandlers/FullAjaxExceptionHandler.xhtml)。 – BalusC

回答

1

我想建議您使用ExternalContext重定向error頁面。你必須配置Exceptionweb.xml

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,很容易重定向的exceptionerror/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>