【问题标题】:Programmatically navigate to error page in JSF 2.0以编程方式导航到 JSF 2.0 中的错误页面
【发布时间】:2012-11-24 14:07:46
【问题描述】:

在我的应用程序中,每当发生异常时,我都愿意导航到错误页面。 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,但它没有导航到所需的错误页面。

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

【问题讨论】:

  • 这并不是在 JSF 中处理异常的正确方法。长话短说,请查看this demo page

标签: java jsf-2 exception-handling navigation faces-config


【解决方案1】:

我想建议使用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>

【讨论】:

    猜你喜欢
    • 2014-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 2011-08-14
    相关资源
    最近更新 更多