【问题标题】:Facelets custom error page - customize / wrap the exception message / stack traceFacelets 自定义错误页面 - 自定义/包装异常消息/堆栈跟踪
【发布时间】:2013-12-07 14:13:08
【问题描述】:

我正在使用 JBoss AS7。 我已经知道如何使用我自己的 web.xml 错误页面来处理 HTTP 错误(例如 404、500、...)——这不是问题。 但出于调试原因,我需要查看错误 StackTrace。如何访问默认显示的消息并将其嵌入错误页面?

【问题讨论】:

  • 当您说“查看错误堆栈跟踪”时,您的意思是要将其打印到屏幕上吗?
  • 这正是我的意思。但不是在控制台上,而是在网页(xhtml)上。
  • 你研究过JSF的ExceptionHandlerFactory机制吗?

标签: jsf exception facelets stack-trace custom-error-pages


【解决方案1】:

具体的异常实例可用作请求属性,其名称以RequestDispatcher#ERROR_EXCEPTION 为键,其值为javax.servlet.error.exception

因此,这会给你一个例外:

#{requestScope['javax.servlet.error.exception']}

但是,没有标准工具可以在视图中打印其堆栈跟踪。你需要homebrew an EL function,就像JSF utility library OmniFaces 已经有#{of:printStackTrace()} 的味道。您可以在OmniFaces FullAjaxExceptionHandler showcase page 中看到它的实际效果:

<ui:composition ... xmlns:of="http://omnifaces.org/functions">
...
<li>Stack trace: <pre><code>#{of:printStackTrace(requestScope['javax.servlet.error.exception'])}</code></pre></li>

函数实现如下所示:

/**
 * Print the stack trace of the given exception.
 * @param exception The exception to print the stack trace for.
 * @return The printed stack trace.
 */
public static String printStackTrace(Throwable exception) {
    if (exception == null) {
        return null;
    }

    StringWriter stringWriter = new StringWriter();
    exception.printStackTrace(new PrintWriter(stringWriter, true));
    return stringWriter.toString();
}

另见:

【讨论】:

  • 到目前为止听起来不错。在尝试此操作时,我意识到我的 HTTP 500 错误自定义页面实际上根本不起作用。就像在这篇文章 (1) 中一样,只有我的 404 页面有效。是否有关于错误 500 问题的任何更新? (1)stackoverflow.com/questions/16694675/…
  • 您是否已经阅读了我的答案中链接的OmniFaces FullAjaxExceptionHandler showcase?它描述(并解决)了许多与 JSF (ajax) 请求中的异常处理有关的问题。
  • 很棒的文章 - 我现在可以使用它了。正在捕获错误 500,并且该页面包含错误消息。谢谢。
猜你喜欢
  • 2015-11-19
  • 2021-06-07
  • 1970-01-01
  • 2023-01-14
  • 1970-01-01
  • 2015-12-26
  • 2010-10-17
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多