Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display exception stack trace into Facelets page

I need to display exception stack trace into my JSF application error.xhtml page. I know how simple is to do it with JSP page. But with JSF 2.0 I have a problem.

In my web.xml I have defined a JSF 2.0 Facelets page as error page:

<error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/faces/views/error.xhtml</location>
</error-page>

When the error occurs the I get redirected to this page. What I need is to display the stack trace of exception in this Facelets page.

I have tried to use:

<pre>
    <h:outputText value="${exception}"/>
</pre>

But I don't get any output. I have been searching the internet but I did not find a solution. How can I display the exception stack trace in the Facelets page?

EDIT:

I have just tried:

<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>

JSTL not working and interating through datatable also not working. I am sure that exception occurs, I see it in my log files.

like image 899
Paulius Matulionis Avatar asked Aug 17 '12 14:08

Paulius Matulionis


1 Answers

It's present as a request attribute with the name as specified by the RequestDispatcher.ERROR_EXCEPTION constant.

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

This gives you the whole Exception object. Getting its stacktrace requires a bit more work. You basically need to create a custom EL function which does basically something like this:

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

so that you can use it as follows:

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

The JSF utility library OmniFaces offers this as well. See also the FullAjaxExceptionHandler showcase page.

like image 183
BalusC Avatar answered Oct 07 '22 22:10

BalusC