The J2EETM Tutorial
Home
TOC
PREV TOC NEXT Search
Feedback

JavaBeans Component Design Conventions

JavaBeans component design conventions govern the public attributes of the class, which are also known as its properties, and the public methods that give access to the properties.

A JavaBeans component property can be:

There is no requirement that a property be implemented by an instance variable; the property must simply be accessible using public methods that conform to certain conventions:

In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.

The Duke's Bookstore application JSP pages enter.jsp, bookdetails.jsp, catalog.jsp, showcart.jsp use the database.BookDB and database.BookDetails JavaBeans components. BookDB provides a JavaBeans component front end to the enterprise bean BookDBEJB. Both beans are used extensively by bean-oriented custom tags (see Tags That Define Scripting Variables). The JSP pages showcart.jsp and cashier.jsp use cart.ShoppingCart to represent a user's shopping cart.

The JSP pages catalog.jsp, showcart.jsp, and cashier.jsp use the util.Currency JavaBeans component to format currency in a locale-sensitive manner. The bean has two writable properties, locale and amount, and one readable property, format. The format property does not correspond to any instance variable, but returns a function of the locale and amount properties.

public class Currency {
	private Locale locale;
	private double amount;
	public Currency() {
		locale = null;
		amount = 0.0;
	}
	public void setLocale(Locale l) {
		locale = l;
	}
	public void setAmount(double a) {
		amount = a;
	}
	public String getFormat() {
		NumberFormat nf =
			NumberFormat.getCurrencyInstance(locale);
		return nf.format(amount);
	}
} 
Home
TOC
PREV TOC NEXT Search
Feedback