Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSF 2 : Ajax errors not displayed

Im currently trying out the ajax feature, and im curious about how can i display the error messages occured when i do the ajax stuff ? For example, i have a primefaces button :

<p:commandButton value="Refresh" update="debugPanel messages" 
   action="#{checkbocLabBean.submit}"/>

When testing that button, nothing seems to happen, until i check the server logs, there's an exception. It turns out that i have a typo. Should be #{checkboxLabBean.submit}.

The only thing i can think of right now is to have disable the ajax, adding ajax="false" in my case, and the error will show up.

Is there any other ways to show errors right into the browser in development phase when using ajax requests ?

like image 553
Albert Gan Avatar asked Dec 24 '10 03:12

Albert Gan


1 Answers

Ajax requests happens asynchronously. They do by default not affect the entire document. Exceptions thrown during PrimeFaces' ajax requests are delegated to <p:ajaxStatus>. You can do your thing in <facet name="error">.

<p:ajaxStatus>
    <f:facet name="start">Loading...</f:facet>
    <f:facet name="success"><h:outputText value="" /></f:facet>
    <f:facet name="error">An error has occurred!</f:facet>
</p:ajaxStatus>

If you rather want to replace the current document with the error page -which is a very reasonable choice- then it's good to know that PrimeFaces uses jQuery under the covers and that you can configure jQuery as follows to do so:

<script>
    jQuery.ajaxSetup({
        error: handleXhrError
    });

    function handleXhrError(xhr) {
        document.open();
        document.write(xhr.responseText);
        document.close();
    }
</script>

See also:

  • How to safeguard webapplication on server-side errors?
  • Handling of server-side HTTP errors in jQuery's ajax requests
like image 121
BalusC Avatar answered Oct 24 '22 11:10

BalusC