【问题标题】:PrettyFaces 404 page without URL redirect没有 URL 重定向的 PrettyFaces 404 页面
【发布时间】:2015-10-13 00:26:40
【问题描述】:

在我们使用 PrettyFaces 2.0.12.Final 的应用中,我们在 pretty-config.xml 中设置了重定向。

<url-mapping id="foo">
    <pattern value="/foo/#{alias}" />
    <view-id value="/foo.xhtml" />
</url-mapping>

我们在 web.xml 中设置了一个自定义 404 页面。

<error-page>
    <error-code>404</error-code>
    <location>/404.xhtml</location>
</error-page>

当用户收到 404 Not Found 错误时,对于不存在的 foo“别名”,他们将被重定向到“/404.xhtml”,并且浏览器 URL 栏不会保留有问题的地址。

有没有办法在浏览器的网址栏中维护“/foo/aliasdoesnotexist”的网址,仍然显示404页面?

【问题讨论】:

    标签: jsf redirect http-status-code-404 prettyfaces


    【解决方案1】:

    处理这种情况的一种方法是处理应用程序中的异常并执行到错误页面的内部转发:

    您可以为 404 页面设置 PrettyFaces 映射:

    <url-mapping id="foo">
        <pattern value="/foo/#{alias}" />
        <view-id value="/foo.xhtml" />
    </url-mapping>
    

    然后在您的应用程序代码中:

    catch ( Exception e )
    {
        String contextName = FacesContext.getCurrentInstance().getExternalContext().getContextName();
        FacesContext.getCurrentInstance().getExternalContext().dispatch(contextName + "/404");
    }
    

    如果您想全局捕获异常,您需要创建一个 servlet 过滤器来执行此操作。有关如何创建捕获所有异常的过滤器,请参阅以下主题:

    how do I catch errors globally, log them and show user an error page in J2EE app

    希望这会有所帮助!

    ~林肯

    【讨论】:

      猜你喜欢
      • 2013-06-18
      • 1970-01-01
      • 2019-02-03
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2012-12-09
      相关资源
      最近更新 更多