在Facelets页面中显示异常堆栈跟踪

Pau*_*nis 8 java jsf exception facelets jsf-2

我需要在我的JSF应用程序error.xhtml页面中显示异常堆栈跟踪.我知道用JSP页面做这件事有多简单.但是使用JSF 2.0我有一个问题.

在我的web.xml我已经定义了一个JSF 2.0 Facelets页面作为错误页面:

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/faces/views/error.xhtml</location>
</error-page>
Run Code Online (Sandbox Code Playgroud)

当错误发生时,我被重定向到此页面.我需要的是在此Facelets页面中显示异常的堆栈跟踪.

我试过用:

<pre>
    <h:outputText value="${exception}"/>
</pre>
Run Code Online (Sandbox Code Playgroud)

但我没有得到任何输出.我一直在网上搜索,但我找不到解决方案.如何在Facelets页面中显示异常堆栈跟踪?

编辑:

我刚尝试过:

<c:forEach var="exeption" items="${exception.stackTrace}">
    <div>${exeption}</div>
</c:forEach>

<h:dataTable value="#{exception.stackTrace}"
             var="exception">
    <h:column>
        <h:outputText value="#{exception}"/>
    </h:column>
</h:dataTable>
Run Code Online (Sandbox Code Playgroud)

JSTL无法正常工作并通过数据表进行交互也无法正常工作.我确定发生异常,我在日志文件中看到它.

Bal*_*usC 15

它作为请求属性存在,其名称由RequestDispatcher.ERROR_EXCEPTION常量指定.

#{requestScope['javax.servlet.error.exception']}
Run Code Online (Sandbox Code Playgroud)

这给了你整个Exception对象.获得它的堆栈跟踪需要更多的工作.你基本上需要创建一个自定义EL函数,它基本上是这样的:

public static String printStackTrace(Throwable exception) {
    StringWriter stringWriter = new StringWriter();
    exception.printStackTrace(new PrintWriter(stringWriter, true));
    return stringWriter.toString();
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以按如下方式使用它:

<pre>#{my:printStackTrace(requestScope['javax.servlet.error.exception'])}</pre>
Run Code Online (Sandbox Code Playgroud)

JSF实用程序库OmniFaces也提供了此功能.另请参见FullAjaxExceptionHandler展示页面.