自定义 Facelets 错误页面

发布于 2024-10-24 07:43:19 字数 2909 浏览 9 评论 0原文

我有一个 Facelets(JSF 1.2 (myfaces)) Web 应用程序,我想自定义我的错误页面 - 当应用程序成熟时,这似乎是很自然的事情。 在这个过程中我真的很困惑。

我发现了以下内容:

  • 我还没有找到自定义 Facelets 错误页面的方法。我还没找到模板在哪里。我已经找到了重写 ViewHandler 的解决方案,该 ViewHandler 会执行 sendRedirect()。我认为这应该可以在不编写代码的情况下完成,尤其是 ViewHandler。

  • 我找到了一种方法来关闭 Facelets 的错误处理并使用 myFaces 的错误处理:

代码:

<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>org.apache.myfaces.ERROR_TEMPLATE_RESOURCE</param-name>
    <param-value>/pages/public/errorPage.jsf</param-value>
</context-param>

不幸的是,我似乎无法使 myFaces 找到 JSF 页面。我需要使用 jsf 页面,因为我想使用网站的布局,该布局分散在几个模板中。 来源: http://wiki.apache.org/myfaces/Handling_Server_Errors

  • 我尝试了战斧解决方案:

web.xml:

<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLER</param-name>
    <param-value>org.apache.myfaces.tomahawk.util.ErrorRedirectJSFPageHandler</param-value>
</context-param>

faces-config.xml:

<navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
        <from-outcome>java.lang.Throwable</from-outcome>
        <to-view-id>/pages/public/errorPage.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

来源:http://wiki.apache.org/myfaces/Handling_Server_Errors

  • 我发现我可以通过 web.xml 中的 使用容器的错误处理。我成功转发到jsf错误页面。这里的问题是我无法显示异常 - 我不知道如何。

更新:我发现了如何使用 ManagedBean:

public class ErrorDisplayBean {
    public String getStackTrace() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestMap = context.getExternalContext().getRequestMap();
        Throwable ex = (Throwable) requestMap.get("javax.servlet.error.exception");
        ...
    }
}

请参阅 http://wiki。 apache.org/myfaces/Handling_Server_Errors 了解其余代码。

我想要完成的任务:我想使用 Facelets 的错误处理机制,而无需编写代码,并且能够在 jsf 页面上显示异常。如果这是不可能的,我想再次使用 myFaces 的错误处理并在异常中显示。我认为其中之一应该是可能的。

I have a Facelets(JSF 1.2 (myfaces)) web app and I want to customize my error page - which would seem to be a natural thing to do when an application matures.
I got really confused in the process.

I found the following:

  • I haven't found a way to customize Facelets' error page. I haven't found where the template is. I have found solutions with overriding the ViewHandler that would do sendRedirect(). I think this should be accomplishable without writing code, especially the ViewHandler.

  • I have found a way to switch off Facelets' error handling and using myFaces' one:

code:

<context-param>
    <param-name>facelets.DEVELOPMENT</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>org.apache.myfaces.ERROR_TEMPLATE_RESOURCE</param-name>
    <param-value>/pages/public/errorPage.jsf</param-value>
</context-param>

Unfortunately I cannot seem to make myFaces find a JSF page. I need to use a jsf page because I want to use the site's layout which is fragmented over a few templates.
Source: http://wiki.apache.org/myfaces/Handling_Server_Errors

  • I tried a tomahawk solution:

web.xml:

<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLING</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.ERROR_HANDLER</param-name>
    <param-value>org.apache.myfaces.tomahawk.util.ErrorRedirectJSFPageHandler</param-value>
</context-param>

faces-config.xml:

<navigation-rule>
    <from-view-id>*</from-view-id>
    <navigation-case>
        <from-outcome>java.lang.Throwable</from-outcome>
        <to-view-id>/pages/public/errorPage.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

Source: http://wiki.apache.org/myfaces/Handling_Server_Errors

  • I found that I can use the container's error-handling via <error-page> in web.xml. I successfully forwarded to a jsf error page. Here the problem is that I cannot display the Exception - I don't know how.

Update: I found out how - with a ManagedBean:

public class ErrorDisplayBean {
    public String getStackTrace() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map requestMap = context.getExternalContext().getRequestMap();
        Throwable ex = (Throwable) requestMap.get("javax.servlet.error.exception");
        ...
    }
}

see http://wiki.apache.org/myfaces/Handling_Server_Errors for the rest of the code.

What I want to accomplish: I want to use Facelets' error-handling mechanism without writing code and be able to display the Exception on a jsf page. If that is not possible, I'd like to use myFaces' error-handling again with displayin the Exception. I think one of them should be possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身以后 2024-10-31 07:43:19

我认为您仍然可以通过 web.xml 进行配置:

 <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/error.xhtml</location>
 </error-page>

它也可以用于错误代码..

I think you can still make of configuration through your web.xml:

 <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/error.xhtml</location>
 </error-page>

Which can be used for error codes too..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文