This article outlines the steps involved in creating an application using JavaServer Faces. It is excerpted from chapter two of the book JavaServer Faces, written by by Hans Bergsten (O'Reilly, 2004; ISBN: 0596005393).
JSF Development Process Overview - Developing an Application with a JSF-Based User Interface (Page 2 of 4 )
To get an idea of how JSF simplifies development and maintenance of complex web-based user interfaces, let’s implement a simple newsletter subscription application. The application contains a form where the user enters an email address and selects the newsletters of interest from a list, plus a button for submitting the form. Figure 2-1 shows this user interface.
Figure 2-1.Newsletter subscription form
When the user submits the form, the email address and subscription list is saved in a database. Other parts of the application use this information to send the newsletters, but we’ll focus on this single page here.
JSF-based application development involves a number of different activities. It helps to define the different roles that the developers play, and then discuss the application aspects each role is responsible for. One person can, of course, take on more than one role. First, you need an implementation of the JSF framework itself. This is the responsibility of the JSF implementor, a role that’s usually performed by web container vendors. Another role, the tool provider, is responsible for developing tools to support JSF application development. Some web container vendors take on this role as well, but vendors specializing in development tools, such as Macromedia, are also likely candidates. Remember that JSF is a specification, not a product, so you have a choice of many competing implementations.
Most development projects use an existing JSF framework and tools, so let’s focus on the remaining roles: the application developer, the component writer, and the page author. Implementing the newsletter subscription example shows you what parts each role is responsible for and gives you a glimpse of how JSF works.
Developing the Application Backend
The application developer is primarily responsible for developing the backend part of the application; in other words, the classes that handle business logic and data.
For the newsletter subscription form, the application developer may create a class calledSubscriberto hold the subscriber information:
package com.mycompany.newsservice.models; public class Subscriber{ private String emailAddr; private String[] subscriptionIds; public String getEmailAddr() { return emailAddr; } public void setEmailAddr(String emailAddr) { this.emailAddr = emailAddr; } public String[] getSubscriptionIds() { return subscriptionIds; } public void setSubscriptionIds(String[] subscriptionIds) { this.subscriptionIds = subscriptionIds; } }
TheSubscriberclass adheres to the JavaBeans method naming conventions—“properties” are defined by methods for getting their values, namedgetplus the name of the property, and methods for setting their values, namedsetplus the property name. As you’ll see shortly, this makes it easy to use properties of this class as JSF UI component models.
When a subscription is registered or updated, the information must be saved somewhere, likely in a database. The application developer may decide to develop a separate class responsible for this task or put this logic in theSubscriberclass. To keep the example simple, we’ll add a method that represents this behavior to the Subscriberclass. Also, instead of saving the information in a database, we just write it toSystem.out:
Subscriber class. Also, instead of saving the information in a database, we just write it to System.out:
public void save(){ StringBuffer subscriptions = new StringBuffer(); if (subscriptionIds != null) { for (int i = 0; i < subscriptionIds.length; i++) { subscriptions.append(subscriptionIds[i]).append(" "); } } System.out.println("Subscriber Email Address: " + emailAddress + "nSubscriptions: " + subscriptions); }
This is all the backend code we need for this simple application. Note, however, that none of these application classes refer to JSF classes; the same classes could be used with any type of user interface.