Facelets自定义错误页面 - 自定义/包装异常消息/堆栈跟踪 [英] Facelets custom error page - customize / wrap the exception message / stack trace
问题描述
我已经知道如何使用我自己的
web.xml
HTTP错误的错误页面(例如404,500,...) - 这不是问题。 但是为了调试原因,我需要查看StackTrace的错误。如何访问默认显示的消息并将其嵌入错误页面?
具体异常实例可用一个请求属性,其名称由 RequestDispatcher#ERROR_EXCEPTION
,其值为 javax.servlet.error.exception
。
因此,这将给你例外:
#{requestScope ['javax.servlet.error.exception' ]}
但是,在视图中没有打印其堆栈跟踪的标准功能。您需要自制一个EL功能,像 JSF实用程序库OmniFaces 已经具有#{of:printStackTrace()}的风格
。您可以在 OmniFaces FullAjaxExceptionHandler
展示中看到它页面:
< ui:composition ... xmlns:of =http://omnifaces.org /功能>
...
< li>堆栈跟踪:< pre>< code>#{of:printStackTrace(requestScope ['javax.servlet.error.exception'])}< / code> ;< /预>< /锂>
其功能实现如下所示:
/ **
*打印给定异常的堆栈跟踪。
* @param exception打印堆栈跟踪的异常。
* @return打印的堆栈跟踪。
* /
public static String printStackTrace(Throwable exception){
if(exception == null){
return null;
}
StringWriter stringWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stringWriter,true));
return stringWriter.toString();
}
另请参见:
I am using JBoss AS7.
I already know how to use my own web.xml
error pages for HTTP Errors (e.g. 404, 500, ...) - thats not a problem.
But for debugging reasons I need to view the error StackTrace. How can I access the message that is shown by default and embed it in error page?
The concrete exception instance is available as a request attribute with the name as keyed by RequestDispatcher#ERROR_EXCEPTION
which has a value of javax.servlet.error.exception
.
Thus, this will give you the exception:
#{requestScope['javax.servlet.error.exception']}
However, there's no standard facility to print its stack trace in the view. You'd need to homebrew an EL function, something like as JSF utility library OmniFaces already has in flavor of #{of:printStackTrace()}
. You can see it in action in the 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>
whereby the function implementation look like this:
/**
* 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();
}
See also:
- What is the correct way to deal with JSF 2.0 exceptions for AJAXified components?
- What is the good approach to forward the exception from servlets to a jsp page?
这篇关于Facelets自定义错误页面 - 自定义/包装异常消息/堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!