JSF 2.0 and AJAX example

October 10th, 2009  | Tags: ,

As you should know, in JSF 1 the AJAX wasn’t default. To use AJAX we had to use an external component, such as Richfaces (ajax4jsf), IceFaces, etc. This raises some drawback, because you cannot use both components in the same project.
However, in version 2, the AJAX API comes by default. If you’re familiar with ajax4jsf, you’ll fee comfortable to use the AJAX API in JSF 2.0.Also, the JSF team promisses that you’ll be able to use external AJAX API, such as YUI, JQuery, etc, in JSF smoothly. Check it out this topic: http://weblogs.java.net/blog/driscoll/archive/2009/08/using_the_yui_c_1.html
Let’s create a simple example using our previous example that has been written here. Please, go to the previous post and download the example.

How the example works

In the previous example we had 2 JSPs and 1 ManagedBean. The principal.xhtml had a form with one field (name). A submit button calls the managed bean and this name attribute was converted to upper case. Finally, the success.xhtml showed the result.
Now, let’s use only the principal.xhtml file. It will have the form and then will show up the result.

Changing the principal.xhtml page

The first thing to do is setup the javascript library into page. To do that, simple add the following in the head content.

<h:outputScript name="jsf.js" library="javax.faces" target="head" />

Another change is in the h:form tag. Add the prependId=”false” attribute.
Note: All fields must have the id attribute setup. As you should know, AJAX looks for those IDs to make the request.

Adding AJAX call

So far we changed the principal.xhtml file to be able to make AJAX call, but where is the call itself?
It is done through a tag called f:ajax. This tag can be inserted into components, such as h:commandButton to make an ajax call.
So, let’s create a commandButton and add the AJAX functionality for it.

        	<h:commandButton value="Ajax Button" action="#{helloWorld.ajaxCall}">
        		<f:ajax event="action" execute="@form" render="out"/>        		
        	</h:commandButton>

Some important notes: The h:commandButton is common. It has a value and an action call. It could have other events, such as: onclick, onblur, etc.
Inside the button we have the f:ajax tag. Its event attribute tells to JSF with event will be called, in our example the action event. Other events, such as onclick can be used.
The execute attribute tells with data should be sent do the server. The @form value tells that all the form should be sent.
Finally, we have the render attribute. In this render we tell to JSF with component will be updated after the call.
Now, we just need to add our h:outputText component.

<h:outputText value="#{helloWorld.name}" id="out" />

After those changes, our entire principal.xhtml looks like:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
	<h:outputScript name="jsf.js" library="javax.faces" target="head" />
</h:head>      
<h:body>
    <f:view>
        <h2><h:outputText value="This is our first JSF 2.0 example" /></h2>
        <h:form prependId="false" id="form">
        	Name: <h:inputText value="#{helloWorld.name}" id="name" /><br />
        	<h:commandButton action="#{helloWorld.doSomething}" value="Something is gonna happen" /><br />
        	<h:commandButton value="Ajax Button" action="#{helloWorld.ajaxCall}">
        		<f:ajax event="action" execute="@form" render="out"/>        		
        	</h:commandButton> 
        	<h:outputText value="#{helloWorld.name}" id="out" />        	
        </h:form>        
    </f:view>
</h:body>
</html>

Note: We kept the old button there, thus you can have both example in the same project.

Chaning the ManagedBean

If you look at the AJAX Button, you’ll see that its call an ajaxCall method. So, let’s implement it into ManagedBean.
Open the HelloWorldMBean.java class and add the following method:

	public void ajaxCall() {
		name = name.toUpperCase();
	}

Note: the method doesn’t have a return. That’s simple, huh?

Running the code

After that, you can run the project, type something and click on AJAX Button. You’ll realize that a AJAX call was performed because only the result is in upper case, not the value in the field.

Conclusion

Certainly the JSF 2.0 brings great news and native AJAX support is one of them. Who knows in the future, we can use multiples AJAX components in our single project. Now you already know how to use AJAX support, try out combine it with other AJAX frameworks, such as YUI or JQuery.

If you have any comment, question or feedback, please, let your message below.

  1. October 23rd, 2009 at 18:48
    #1

    Olá

    Muito legal seus posts sobre o JSF 2.0.
    Parabéns!

    Abraços

  2. boernie
    January 18th, 2010 at 10:57
    #2

    Thank you! Very good description of a simple but good working example!
    Helped me out a lot! :)

    greetings boernie

  3. cristi
    February 22nd, 2010 at 09:34
    #3

    Congratulations! after two days of searching the internet and trying to understand JSF 2.0 and Ajax, your example is exactly what I was looking for. Thank you so much!!!

  4. Sandro
    March 6th, 2010 at 02:46
    #4

    Congratulations! after two days of searching the internet and trying to understand JSF 2.0 and Ajax, your example is exactly what I was looking for. Thank you so much!!! [2]

  5. Edson
    March 25th, 2010 at 08:59
    #5

    Hello, very good, but dont work with richfaces?

    Thanks

    Édson Oliveira

  6. March 25th, 2010 at 09:28
    #6

    According to Richfaces’ team, the version 4 (still beta), is regarding JSF 2.0, however this topic is about the native ajax support in version 2, not an external component

  7. sue
    March 25th, 2010 at 16:09
    #7

    Your article is a good help for me, however, i keep getting message that ‘One or more resources have the target of ‘head’, but no ‘head’ component has been defined within the view.’. I am using facelets and jsf 2.0. Any idea of what i might be missing? Thanks!

  8. ravi
    July 8th, 2010 at 15:47
    #8

    Hi how to generate popup while displaying validation error. for example for inputtext required=true if i skip and press submit button i want to display h:message in popup is that possible in jsf2.0 . could u repsond this .

    Thanks inadvance.

  9. September 7th, 2010 at 01:55
    #9

    Olá

    Muito legal seus posts sobre o JSF 2.0.
    Parabéns!

    Abraços
    [2]

TOP