JavaBeans are Java objects developed using specific syntax rules that allow a reusable component architecture. That is, JavaBeans are reusable application components used as either GUI components or, as in our case, JSP helper classes. For more details about JavaBeans and the 1.0 Specification, refer to http://java.sun.com/products/javabeans/. JavaBeans generally provide support for
Introspection— Introspection enables runtime determination of supported events, methods, and properties, so that tools built to the specification can determine how the bean works. Low-level reflection is used to query methods and deduce (using conventional names and type signatures) what properties, events, and public methods are supported. If the developer provides a BeanInfo class, that class is used to determine the bean's behavior. A bean Introspector class is furnished to provide a standardized methodology of introspecting different beans.
Customization— Customization, which is applicable to GUI beans, enables developers to customize a bean's appearance and behavior so that application builders built to specification can modify it. Customization is implemented in two ways: GUI property sheets and Customizer classes. GUI beans can export a set of properties. These properties can be used to create a GUI property sheet, which lists the properties and provides an editor for each property. This property editor can then be used to update bean properties. Customizer classes are AWT components that provide the GUI behavior required to control target beans.
Events— Events are applicable to GUI beans. They're constructs that convey state change information that can be used to connect beans. Events are conveyed from source Objects to target listener Objects via method invocations originating from the target listener. Each type of event notification is defined by a unique method. Those methods are grouped within EventListener interfaces. Event state objects are used to encapsulate the state associated with a particular event. Custom adapter classes may be used in situations where listeners cannot implement a desired interface.
Persistence— Saving and restoring object state is also used to support customization in application builders to allow persistence of object state. JavaBeans must support the Java object serialization APIs, which include externalization. If a JavaBean doesn't implement the hidden state attribute, third-party tools may be used to restore the state of a serialized JavaBean; otherwise, Java object serialization must be used to save and restore the JavaBean's state.
Properties— Also known as bean attributes, properties are used to support customization and developer implementation. Properties are the only feature required for non-GUI JavaBean implementations. Further discussion of properties follows.
For use within JSP pages, JavaBeans merely must define properties and implement the accessor methods (getter/setter) associated with those properties. A JavaBean must also implement at least a no-parameter (default) constructor. Properties are discrete attributes of the bean. Attributes may be built-in Java data types or user-defined data types. An attribute must be declared private, and public accessor methods must be defined for each attribute. These methods are of the form setAttributeName(attribute_data_type) and getAttributeName(). As presented in Chapter 15, “Writing WebLogic Server JavaServer Pages,” to implement JavaBeans within a JSP page, you use the useBean, getProperty, and/or setProperty actions. For example, using the ItemHolderBean implemented for GAMS to create an item with the following attributes, we can use the following code:
<jsp:useBean id="item" class="com.gams.javaBeans.item.ItemHolderBean" scope="session"/> <jsp:setProperty name="item" property="itemId" value="3004"/> <jsp:setProperty name="item" property="title" value="Oriental Rug"/> <jsp:setProperty name="item" property="currentBid" value="875.00"/> <jsp:setProperty name="item" property="endDateTime" value="102,10,31"/>
To dereference bean attributes, use can use the following code:
<jsp:getProperty name="item" property="itemId" value="3004"/> or <%=item.getItemId()%>
For information about using JavaBeans within JSP pages, refer to the discussion of the useBean action in Chapter 15. Listing 16.1 presents a partial code listing for the ItemHolderBean.java JavaBean. As you can see, the bean definition for a JSP helper class is pretty straightforward. The bean attributes are defined in lines 8–21. In line 23, the requisite default constructor is defined.
Two constructors (one a 4 parameter and the other a 14 parameter; used to construct ItemBeanHolder Java objects) are defined in lines 26–51. The remainder of the code (lines 54–192) defines attribute getter and setter methods. Listing 16.2 displays a simple JSP, viewItemHolderBean.jsp, which uses the ItemHolderBean.
Note the declaration of the JavaBean item in line 9. Lines 10–12 implement <jsp:setProperty> actions, which set attributes on the item bean. Lines 14 and 15 dereference the item bean attributes itemId and title using Java standard method call syntax. Note line 17 where the <jsp:getProperty> action is used to dereference attribute currentBid. A browser display of viewItemHolderBean.jsp is provided in Figure 16.3.