jsf - Facelets 自定义错误页面 - 自定义/包装异常消息/堆栈跟踪

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

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

最佳答案

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

因此,这会给你一个异常(exception):

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

但是,没有标准工具可以在 View 中打印其堆栈跟踪。您需要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();
}

另请参阅:

关于jsf - Facelets 自定义错误页面 - 自定义/包装异常消息/堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20149251/

相关文章:

jsf - 定义 selectManyCheckbox 的列

css - 滚动时修复了 JSF 中的 PanelGrid(primefaces)

ajax - uiComponent树内存使用情况

c# - 在 .NET 中,为什么不能在抛出 BadImageFormatException 的方法中捕获它?

css - 更改面板组件的 primefaces 标题样式

jsf - 如何使用 <f :ajax> to set updated value in managed bean when value of <h:inputText> is changed

java - 在 Jar 文件中获取 xml 文件

jsf-2 - ui 内的复选框 :repeat not refreshed by Ajax

eclipse - 如何通过 Eclipse IDE 在 XHTML 中使用 Facelets 开发应用程序?

java - 线程等待用户单击某些内容