During my RichFaces session at JBoss World 2009, I showed three small examples of using Ajax with RichFaces 3.3, JSF 2, and RichFaces 4. I thougth it would be a good idea to show you the difference or more correct the similarities between the three. I will be blogging more about RichFaces 4 and JSF 2 so this is just a quick introduction.
I will show a small “Echo” application. There is one input field and as as you type, the input is echoed on the next line. On another line, the length of the string entered is counted. It looks like this:

RichFaces 3.3
<h:form> <h:panelGrid columns="2"> <h:outputText value="Text:" /> <h:inputText value="#{echoBean.text}" > <a4j:support event="onkeyup" action="#{echoBean.countAction}" reRender="text, count"/> </h:inputText> <h:outputText value="Echo:" /> <h:outputText id="text" value="#{echoBean.text}" /> <h:outputText value="Count:" /> <h:outputText id="count" value="#{echoBean.count}" /> </h:panelGrid> </h:form>
Managed bean:
public class EchoBean { private String text; // getter and setter private Integer count; // getter and setter public void countAction() { count = text.length(); } ... }
Bean is registered in JSF configuration file (not shown).
JSF 2
<h:form> <h:panelGrid columns="2"> <h:outputText value="Text:" /> <h:inputText value="#{echoBean.text}" > <f:ajax event="keyup" execute="@form" render="text count" listener="#{echoBean.countListener}"/> </h:inputText> <h:outputText value="Echo:" /> <h:outputText id="text" value="#{echoBean.text}" /> <h:outputText value="Count:" /> <h:outputText id="count" value="#{echoBean.count}" /> </h:panelGrid> </h:form>
Managed bean looks slightly different as instead of an action (see example above) we use a special Ajax listener:
@ManagedBean(name="echoBean") @RequestScoped public class EchoBean { private String text; private Integer count; public void countListener (AjaxBehaviorEvent event) { count = text.length(); } }
RichFaces 4.0
<h:form> <h:panelGrid columns="2"> <h:outputText value="Text:" /> <h:inputText value="#{echoBean.text}" > <a4j:ajax event="keyup" render="text,count" listener="#{echoBean.countListener}"/> </h:inputText> <h:outputText value="Echo:" /> <h:outputText id="text" value="#{echoBean.text}" /> <h:outputText value="Count:" /> <h:outputText id="count" value="#{echoBean.count}" /> </h:panelGrid> </h:form>
Managed bean is same as in JSF 2.