Display exception stack trace into Facelets page
Asked Answered
G

1

8

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.

Geniculate answered 17/8, 2012 at 14:31 Comment(7)
Did you try to iterate over the Stack Trace elements? Like using a for with itens are ${exception.stackTrace}?Exaltation
No, I did not try that. I will try it now and see if it works. Thanks.Geniculate
You are welcome! I will post an answer if it works ;)Exaltation
Unfortunately this is not working, I have edited the question.Geniculate
I pretty rusty with JSF, but maybe <h:outputText/> only accepts Strings (I really don't remember). Try something like #{exception.message} to see if something is generated.Exaltation
No, I have also tried this, no success :(Geniculate
Do you perhaps need to pull it from the request scope as in ${requestScope.exception.message}?Lackey
M
16

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.

Mending answered 17/8, 2012 at 14:58 Comment(1)
Thank you, it worked perfectly. Now I can see the exception, I will write el function get the full stack trace. Thanks again :)Geniculate

© 2022 - 2024 — McMap. All rights reserved.