Monday, April 27, 2009

h:messages for a4j requests

Well, it's simple: validation errors are not displayed on the page when they apear while processing a Richfaces AJAX request.

To correct this you have to decorate the h:messages tag like this:

<a4j:outputPanel ajaxRendered="true">
<h:messages/>
</a4j:outputPanel>

Why doesn't it work otherwise?
  • h:messages component is not inserted in the JSF component tree the first time the page is loaded (with an empty form);
  • Richfaces AJAX calls can not work with components which are not already in the JSF component tree.

Why does the decorated version work?
  • a4j:outputPanel inserts in the component tree even non-present child components.
  • ajaxRendered attribute sets the outputPanel (with all its child components) to be rendered with the new values after each AJAX request processing.