Using RichFaces a4j:form tag

I sometimes see developers use a4j:form tag (without any attributes) on their pages. When I ask them why did you use this tag? The answer is usually: “I thought I need this tag to send Ajax requests” It turns out you don’t have to use this tag to send an Ajax request. You can use the standard h:form and simply use any component in RichFaces that fires an Ajax request. Components that fire an Ajax request in RichFaces are: a4j:support, a4j:commandButton, a4j:commandLink, a4j:poll, and a4j:jsFunction.

So, if you have this in your code:

  // controls to fire an Ajax request

it’s the same as this:

 // controls to fire an Ajax request

Why does RichFaces have this tag? First, it solves the problem of rerendering h:commandLink without having to rerender the entire form. Second, all standard (non-Ajax) controls placed inside a4j:form can fire an Ajax request when setting ajaxSubmit=true. For example:


In example above, even though we use h:commandButton, an Ajax request will be fired because we use a4j:form with ajaxSubmit=true. a4j:form comes with all the standard attributesrelated to sending an Ajax request such as eventQueue, requestDelay, etc.

A few things to keep in mind:

If you have two standard buttons inside a4j:form and would like different components to be rerendered for each button then you need to bind a4j:form reRender attribute to an EL and resolve which components to rerender in runtime. h:commandButton doesn’t have reRender attribute while a4j:form has only one reRender.

Don’t use a4j:form ajaxSubmit=”true” if it contains other Ajax command components.

1 Comment

  1. Brian says:

    Great post. Thanks for clearing some things up.

Leave a Comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.