2.2 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 called Subscriber to 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;
}
}
The Subscriber class adheres to the JavaBeans
method naming
conventions—"properties" are
defined by methods for getting their values, named
get plus the name of the property, and methods for
setting their values, named set plus 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 the Subscriber class. To keep the
example simple, we'll add a method that represents
this behavior to the 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.
|