我正在使用 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/