Writing an application using an AJAX-enabled JavaServer Faces component library

Yutaka Yoshida
Status: In Early Access

Problem Description

This document describes the programming model of writing an application with an AJAX enabled JavaServer Faces component library. One of the programming models for using AJAX with Java EE applications is to wrap the JavaScript code in a set of JSF components and then use those ajax-enabled JSF components. Developers then use the AJAX-enabled JSF component library in the same manner as using any JSF component library. This has the benefit of hiding the JavaScript and complexities of AJAX from the developer. For example, developers could  use the Java BluePrints AJAX components library in their web applications to add in a progress bar or a mashup with Google maps. This programming model shifts the burden of work to the component developers to wrap the AJAX code in JSF components, and makes it very easy for the developer using the component library to build a web application. 

Solution

Generally, using an AJAX-enabled JSF component library is the same as using any JSF component library. The JSF component does the work to process the requests, in this case made by  XMLHttpRequests, and sends a response back to the browser client. In the case of an AJAX-enabled JSF component, the response sent back may be a fragment of JavaScript code to run in the browser, as well as some HTML or data used for presentation. Lets take a look at how to use an AJAX-enabled JSF component library in a web application.  
The typical JavaServer Faces custom component library jar looks like the one shown in Figure 1.
component structure
Figure 1 : Typical JavaServer Faces component structure

Figure 2 below shows the relationship between tld and the jsp page. In the custom component,  the web application developer must care about the tag library descriptor(*.tld) and  the following information:
  1. the <uri> to specify the tag library in the JSP page of  the web application.
  2. the tag name and associated attributes. If the javadoc of the tag library is provided, the developer doesn't need to read the xml in the tld file.
tld-jsp relationsihp
Figure 2 : Relationship between TLD and JSP

As shown in figure 1 and figure 2, using an AJAX-enabled JSF component is mostly the same as using any JSF component. One point that the developer should be aware of when developing a web application with a JSF component library is to be sure to write the correct tag library descriptor in JSP page. When multiple JavaServer Faces custom components are used in one web application, make sure each component has a different <uri> in tld and incorporate them in the JSP page correctly. Another point to consider, when the developer wants to use a backing bean and write <ui:custom value="#{bean.abc}"></ui:custom>, the tag handler class and renderer in the component have to be coded appropriately with the ValueBinding class.

Figure 3 below is a summary of important elements, especially for developing a web application with multiple JavaServer Faces custom components.
tld-jsp relationsihp
Figure 3 : Important elements in TLD and faces-config.xml

References

The following references may be useful:


© Sun Microsystems 2006. All of the material in The Java BluePrints Solutions Catalog is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.