10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong>Beans Components<br />

166<br />

<strong>Java</strong>Beans Component Design Conventions<br />

<strong>Java</strong>Beans component design conventions govern the properties of the class and govern the<br />

public methods that give access to the properties.<br />

A <strong>Java</strong>Beans component property can be:<br />

■ Read/write, read-only, or write-only<br />

■ Simple, which means it contains a single value, or indexed, which means it represents an<br />

array of values<br />

A property does not have to be implemented by an instance variable. It must simply be<br />

accessible using public methods that conform to the following conventions:<br />

■ For each readable property, the bean must have a method of the form:<br />

PropertyClass getProperty() { ... }<br />

■ For each writable property, the bean must have a method of the form:<br />

setProperty(PropertyClass pc) { ... }<br />

In addition to the property methods, a <strong>Java</strong>Beans component must define a constructor that<br />

takes no parameters.<br />

<strong>The</strong> Duke’s Bookstore application JSP pages bookstore.jsp, bookdetails.jsp, catalog.jsp,<br />

and showcart.jsp, all located at<br />

tut-install/javaeetutorial5/examples/web/bookstore2/web, use the<br />

tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/Book<br />

<strong>Java</strong>Beans component.<br />

BookDB provides a <strong>Java</strong>Beans component front end to the access object BookDBAO. <strong>The</strong> JSP pages<br />

showcart.jsp and cashier.jsp access the bean<br />

tut-install/javaeetutorial5/examples/web/bookstore/src/com/sun/bookstore/cart/ShoppingCart.ja<br />

which represents a user’s shopping cart.<br />

<strong>The</strong> BookDB bean has two writable properties, bookId and database, and three readable<br />

properties: bookDetails, numberOfBooks, and books. <strong>The</strong>se latter properties do not correspond<br />

to any instance variables but rather are a function of the bookId and database properties.<br />

package database;<br />

public class BookDB {<br />

private String bookId = "0";<br />

private BookDBAO database = null;<br />

public BookDB () {<br />

}<br />

public void setBookId(String bookId) {<br />

this.bookId = bookId;<br />

}<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!