Helping ordinary people create extraordinary websites!
GET OUR NEWSLETTER
Your Email:
 

JSF for Nonbelievers: The JSF Application Lifecycle

By Rick Hightower
2005-05-05


A Working Example

Now that you have a basic understanding of the phases of the JSF lifecycle, I'll show you how they all work together in an example Web application. In addition to demonstrating the basic functions of the JSF lifecycle, the application utilizes common JSF GUI components like Radio List, List, Text Field, Label, Panel, and more, so you can experience first-hand some of the components discussed briefly in Part 1.

The example application also demonstrates a couple of ways to combine JSF with other Java technologies. It combines JSF and JavaScript to enable immediate event handling (in cases where validating an entire form is unwanted), and its layout is managed by Struts Tiles. Struts Tiles is not necessarily part of JSF, but tiles are commonly used to lend a consistent look and feel to all the JSF pages in an application. See Resources to learn more about Struts Tiles.

Application setup
The example Web application is essentially a simple create, read, update, and delete (CRUD) listing that manages inventory for an online CD store. It includes a form that lets the end user enter a new CD into the system and a list of radio buttons that lets him select a music category. When the user selects a category, you'll fire off some JavaScript to post the form immediately back to the server. Combining JSF with JavaScript to deal with just one component, rather than the entire page, is called immediate event handling. In this case, it lets you populate a subcategory list without validating the rest of the form.

The example application also includes a CD listing that demonstrates how to work with JSF dataTables. From the listing page, the end user can sort the CD listing by title or by artist.

Classes and methods
Figure 3 shows the classes of the example application. Of the four classes shown, we're concerned only with three: StoreManagerDelegate, CD, and StoreController.

Figure 3. Example application classes


The StoreManagerDelegate class is the application's business delegate. It represents the main interface to the model. The CD class is a data transfer object (DTO). If this were a real application, the StoreManagerDelegate class would implement all the business rules for adding, deleting, and editing CDs, and would delegate storing a CD to a persistent store with a data access object (DAO). The StoreManagerDelegate and the CD comprise the model for this MVC application.

The StoreController class is the main backing bean for this example. The StoreController class is the glue from the GUI world to the model world. It delegates a lot of its behavior to the StoreManagerDelegate. The StoreController is the controller for this MVC application.

The StoreController class demonstrates how to build a sortable CRUD listing. It has the following CRUD-related methods: editCD, addNew, addCD, and updateCD. The StoreController is also responsible for presenting the model object to the form. In this case, it presents the current CD object to the CD form using its cd property, which is of type CD.

Tutorial Pages:
» Walk Through the 6 Phases of JSF's Request Processing Lifecycle
» The JSF Lifecycle: an Overview
» Phase 1: Restore View
» Phase 2: Apply Request Values
» Phase 3: Process Validation
» Phase 4: Update Model Values
» Phase 5: Invoke application
» Phase 6: Render Response
» A Working Example
» Let's Code it
» Use Case 1: Add a New CD
» Use Case 2: Edit a CD
» Use Case 3: Sort CDs
» Immediate Event Handling
» Conclusion
» Resources


First published by IBM DeveloperWorks


 | Bookmark
Related Tutorials:
» All about JAXP, Part 1
» Make Database Queries Without the Database
» Load List Values for Improved Efficiency
» 2 Ways To Implement Session Tracking
» A Simple Way to Read an XML File in Java
» Develop Aspect-Oriented Java Applications with Eclipse and AJDT

Advertise with Us!


Tutorials Scripts Web Hosting Developer Manuals
Resources