2012-11-24 54 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>