0

在我的应用程序中,每当发生异常时,我都愿意导航到错误页面。被RuntimeException处理和检查并再次抛出。为此,我在 Faces 配置中定义了一个全局导航规则:

<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强行抛出一个,但它没有导航到所需的错误页面。

任何指针都会对我很有帮助。

4

1 回答 1

1

我想建议error使用重定向页面ExternalContext。你必须配置你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,很容易重定向error/warnning基于exception.

页面.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>
于 2012-11-24T14:42:56.993 回答