What is the canonical way to handle errors during Ajax requests?

For regular queries, we can simply register <error-page> in web.xml . However, this does not apply to Ajax requests. By default, errors during an Ajax request will result in a small pop-up window in the browser that shows an exception.

The main example I'm struggling with is the uniform handling of ViewExpiredException . For standard queries, I redirect to a page that explains that the user is not logged in and provides a link to the login page. I would like to do the same for Ajax requests. There seem to be several ways:

  • I could write a javascript function that handles the error on the client side and redirects it to the error page. Then I would have to add this function every <f:ajax> -tag on all pages using the onerror attribute. Is there any way to tell JSF that I want this javascript function to be the default error handler for all <f:ajax> -tags?
  • I could use my own exception handler as described in this blog . I seem to be doing what I want, but I wonder if this is too much. Is there a simpler solution?

So my question is: how should this be resolved? Which of the approaches listed by me should be used? Is there any other approach that I don't know about?

+4
source share
1 answer

You can use jsf.ajax.addOnError() to set the default error handler. For instance.

 jsf.ajax.addOnError(function(data) { alert(data.responseText); }); 

See also chapter 13.3.6.2 of the JSF2 specification. You can find all the properties of the data object in table 14-4 of the JSF2 specification.

+3
source

All Articles