Section 14.8. JavaBeans

   

14.8 JavaBeans

A JavaBean is a regular Java class that has its member variables (its properties) available to set methods , which update their values, and get methods, which retrieve their values. They are simple, easy-to-write repositories for information that must define a method to get and another method to set each of their elements.

JavaBeans are actually carried over from Swing, where they have a rather different meaning. In the Swing world, JavaBeans create reusable components , often for making Java IDEs . There are several truths (or at least conventions) that your JavaBeans for JSP must uphold:

  • The Java class that defines your bean should be named ClassNameBean . That is, your descriptive name should be appended with "Bean." So, a bean whose purpose is to track orders in an e-commerce site should be called OrderBean .

  • JavaBeans optionally implement the serializable interface. This is primarily for use with Enterprise JavaBeans, however ”another subject altogether.

  • A JavaBean must define a no-argument constructor. Typically, member variables are initialized in this constructor.

  • Every bean property generally consists of a member variable, a getVar() method, and a setVar() method. The methods should be named accordingly , though an exception is made for boolean variables, which can use the "is" prefix; for example, isCurrent() .

  • The naming of the variables and methods is as follows : their initial letter should be lowercase, and subsequent words in the identifier should have initial caps ”for instance, getManagerBonus() and setManagerBonus() . The method names should match their variable names . That is, using the above methods, you should have a managerBonus variable in your bean.

14.8.1 <jsp:useBean>

The <jsp:useBean> action checks for an instance of a bean in the given class and scope. If one is found, it is referenced with the id attribute. If an instance is not found, it instantiates the bean. The bean is then available for processing by referencing the id and scope.

Usage:

 <jsp:useBean id="myBean" class="com.packageName.className"> 

Table 14.6 shows the <jsp:useBean> action attributes.

Table 14.6. <jsp:useBean> Action Attributes

Attribute

Description

Notes

beanName

Bean name

(optional) Specifies name of the bean component you are using to instantiate the bean. This is the same as the name of the bean in the instantiate() method of java.beans.Beans .

class

Class name

(optional) Used to specify class of the bean you are instantiating.

  id  

Bean reference

(required) Specifies name by which you will reference the bean being instantiated .

type

Variable type

(optional) Sets the type of scripting variable to be used as a bean, defined by the id attribute ”the class that the bean gets cast to.

14.8.2 <jsp:setProperty>

The <jsp:setProperty> action sets the value of a property inside a JavaBean.

Usage:

The action performs somewhat differently depending on how it is called.

 <jsp:setProperty name="counter" property="count" param="count"> 

Table 14.7 shows the <jsp:setProperty> action attributes.

Table 14.7. <jsp:setProperty> Action Attributes

Attribute

Description

Notes

name

Bean name

(required) Name of the bean containing the property to be set.

param

Request parameter

(optional) Specifies the request parameter whose value will be set to the bean property value. Use of this attribute obviates use of the value attribute.

property

Bean property

(required) Specifies the property to be set.

value

Set value

(optional) Sets the value of the specified property. Can be any runtime expression that evaluates to a string.

14.8.3 <jsp:getProperty>

This action is used to retrieve the specified property value from a JavaBean; the output value is a string. The specified bean must first be instantiated with the <jsp:useBean> action.

Usage:

 <jsp:getProperty name="cart" property = "orderid" /> 

Table 14.8 shows the <jsp:getProperty> action attributes.

Table 14.8. <jsp:getProperty> Action Attributes

Attribute

Description

Notes

name

Name of bean

(required) Specifies name of a bean, already instantiated in any scope.

property

Property of bean

(required) Specifies the bean property to retrieve the value of.

Let's look at a quick example to demonstrate the typical usage of a bean. Below is a simple bean example that defines a counter.

14.8.4 testCounterBean.jsp

 <%-- File: testCounterBean.jsp       Purpose: show JSP using a JavaBean --%> <%@page contentType="text/html"%> <html> <head><title>session hit counter</title></head> <body> <%-- instantiate the counter bean, this is      usually at or near the top of a page --%> <jsp:useBean id="counter" scope="session" class="beans.ACounterBean" /> <%-- set the property --%>     <jsp:setProperty name= "counter" property="count" param="count"/> <%-- retrieve the property --%> The count: <jsp:getProperty name="counter" property="count" /> </body> </html> 

Below is the Java class that this JSP uses to handle the counting business. Note one thing about working with beans: You cannot simply write this bean and compile it into the same directory as the JSP. Java classes used for beans and custom tags and objects in Web applications should reside under the WEB-INF/classes directory. So, this bean is compiled into <TOMCAT_HOME>/webapps/javaforcf/WEB-INF/classes/beans . The JSP, however, is in the <TOMCAT_HOME>/webapps//javaforcf/chp14 directory. The container knows where to find your classes.

14.8.5 ACounterBean.java

 /* File: CounterBean.java   * Purpose: demo beans by creating a simple counter  */ package beans; public class ACounterBean {     int count = 0;         // no-arg constructor     public ACounterBean() {     }         // getter method     public int getCount() {         // increment on each request         count++;         return this.count;     }         // set the count for this instance     public void setCount(int count) {         this.count = count;     } } 

Closing the browser and opening it again will restart the count, because the bean operates in the session. JSP automatically handles the initiating of sessions for you.

In the following section, we will look at a somewhat more interesting example, though the same simple principles are still clearly at work. Note that an advantage of using beans is that they do not require any special deployment parameters.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net