Article
Adding Ajax to JavaServer Faces Technology With Dynamic Faces
This article shows how to use Project Dynamic Faces, included in the new Sun Web Developer Pack, to add first-class Ajax support to your JavaServer Faces technology-based application.
Beginning with an existing sample application, Virtual Trainer, from the book that the article's author wrote with Chris Schalk, JavaServer Faces: The Complete Reference, this article will show you how to add Ajax behavior to two of that application's pages. This example will illustrate two usage patterns for Project Dynamic Faces and also provide a springboard for discussing the Ajax techniques that Dynamic Faces employs.
First, this article will go through the Virtual Trainer application and call out the places where you will add Ajax features.
The application's welcome page contains two links at the upper right: Register and Login. The Sign Up Today! link at the bottom of the page points to the same page as the Register page. Click Register.
The screen capture in Figure 1 shows the non-Ajax version of the registration page. As you can see, this is a garden-variety JavaServer Faces technology-based page. Fill out the form but use
jake
for the Userid text field, and submit the form.
Once you get past the validation errors, you will see the message Userid jake already exists! Please choose another. Wouldn't it be nice if there were a button next to the Userid field that would allow the user to fire an Ajax request off to the server, sending only the user ID and checking only whether that user ID is available? With a few lines of code, you can create this functionality. Now click the Login link in the application, and use jake as both the user ID and the password. Once logged in, you will see a table of training events, as shown in Figure 2. Choose one training event by clicking the Select link at the right of the desired row.
Listing 2 shows a diff , a file comparison showing the difference between two versions of the same file, of the register.jsp file before and after the changes that this article discusses. As you can see, the author of this article added or changed only 11 lines. Listing 2: diff of Old and New register.jsp Files
Listing 3 shows this new method. This is a plain-old JavaServer Faces ActionListener method. It does not do anything specifically Ajax-related, but because you are using Dynamic Faces, it will be invoked by Ajax. On line 2 of Listing 3, you get the value of the Userid field. Note that this component is bound with a JavaServer Faces component binding, as shown on line 87 of Listing 1. For more detail on component bindings, see the book JavaServer Faces: The Complete Reference. Because you are running the full JavaServer Faces life cycle, you know that the value has been validated and converted according to any validators or converters attached to the component. Line 3 of Listing 3 uses a class specific to the Virtual Trainer application, UserRegistry , which provides a handy method, userIdAlreadyExists . You simply pass the user ID from the text field to this method and store a message in request scope, as shown on lines 4 through 14 of Listing 3. Because this application is already internationalized, you will use the ResourceBundle for the application to get the message. Note that you are passing the current locale from the ViewRoot into the ResourceBundle.getLocale() method. This ensures that the language settings sent by the browser are correctly handled with respect to the supported localizations for this application. Sun Web Developer Pack Tutorial. Project Dynamic Faces reference materials. Briefly, putting components within an Ajax zone causes them to be imbued with Ajax behavior such that clicking on the element will cause an Ajax transaction to the server, allowing just those components within the zone to update themselves by way of Ajax. The Sun Web Developer Pack Tutorial describes how to make action in one zone cause a reaction in another zone, all by way of Ajax. Source code for this bundle Original source for the virtual trainer application Sun Web Developer Pack Tutorial |