JavaBeans Components - The Java EE 5 Tutorial (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
5. JavaServer Pages Technology
Using Objects within JSP Pages
Using Application-Specific Objects
Immediate and Deferred Evaluation Syntax
Deactivating Expression Evaluation
Process of Expression Evaluation
Including the Tag Library Implementation
Transferring Control to Another Web Component
Setting Properties for Groups of JSP Pages
Deactivating EL Expression Evaluation
Further Information about JavaServer Pages Technology
7. JavaServer Pages Standard Tag Library
10. JavaServer Faces Technology
11. Using JavaServer Faces Technology in JSP Pages
12. Developing with JavaServer Faces Technology
13. Creating Custom UI Components
14. Configuring JavaServer Faces Applications
15. Internationalizing and Localizing Web Applications
16. Building Web Services with JAX-WS
17. Binding between XML Schema and Java Classes
19. SOAP with Attachments API for Java
21. Getting Started with Enterprise Beans
23. A Message-Driven Bean Example
24. Introduction to the Java Persistence API
25. Persistence in the Web Tier
26. Persistence in the EJB Tier
27. The Java Persistence Query Language
28. Introduction to Security in the Java EE Platform
29. Securing Java EE Applications
31. The Java Message Service API
32. Java EE Examples Using the JMS API
36. The Coffee Break Application
37. The Duke's Bank Application
JavaBeans Components
JavaBeans components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions is a JavaBeans component.
JavaServer Pages technology directly supports using JavaBeans components with standard JSP language elements. You can easily create and initialize beans and get and set the values of their properties.
JavaBeans Component Design Conventions
JavaBeans component design conventions govern the properties of the class and govern the public methods that give access to the properties.
A JavaBeans component property can be:
- Read/write, read-only, or write-only
- Simple, which means it contains a single value, or indexed, which means it represents an array of values
A property does not have to be implemented by an instance variable. It must simply be accessible using public methods that conform to the following conventions:
- For each readable property, the bean must have a method of the form:
PropertyClass getProperty() { ... } - For each writable property, the bean must have a method of the form:
setProperty(PropertyClass pc) { ... }
In addition to the property methods, a JavaBeans component must define a constructor that takes no parameters.
The Duke’s Bookstore application JSP pages bookstore.jsp, bookdetails.jsp, catalog.jsp, and showcart.jsp, all located at tut-install/javaeetutorial5/examples/web/bookstore2/web, use the tut-install/javaeetutorial5/examples/web/bookstore2/src/java/com/sun/bookstore2/database/BookDB.java JavaBeans component.
BookDB provides a JavaBeans component front end to the access object BookDBAO. The JSP pages showcart.jsp and cashier.jsp access the bean tut-install/javaeetutorial5/examples/web/bookstore/src/com/sun/bookstore/cart/ShoppingCart.java, which represents a user’s shopping cart.
The BookDB bean has two writable properties, bookId and database, and three readable properties: bookDetails, numberOfBooks, and books. These latter properties do not correspond to any instance variables but rather are a function of the bookId and databaseproperties.
package database; public class BookDB { private String bookId = "0"; private BookDBAO database = null; public BookDB () { } public void setBookId(String bookId) { this.bookId = bookId; } public void setDatabase(BookDBAO database) { this.database = database; } public Book getBook() throws BookNotFoundException { return (Book)database.getBook(bookId); } public List getBooks() throws BooksNotFoundException { return database.getBooks(); } public void buyBooks(ShoppingCart cart) throws OrderException { database.buyBooks(cart); } public int getNumberOfBooks() throws BooksNotFoundException { return database.getNumberOfBooks(); } }
Creating and Using a JavaBeans Component
To declare that your JSP page will use a JavaBeans component, you use a jsp:useBean element. There are two forms:
<jsp:useBean id="_beanName_" class="_fully-qualified-classname_" scope="_scope_"/>
and
<jsp:useBean id="_beanName_" class="_fully-qualified-classname_" scope="_scope_"> <jsp:setProperty .../>
The second form is used when you want to include jsp:setProperty statements, described in the next section, for initializing bean properties.
The jsp:useBean element declares that the page will use a bean that is stored within and is accessible from the specified scope, which can beapplication, session, request, or page. If no such bean exists, the statement creates the bean and stores it as an attribute of the scope object (seeUsing Scope Objects). The value of the id attribute determines the name of the bean in the scope and the identifier used to reference the bean in EL expressions, other JSP elements, and scripting expressions (see Chapter 9, Scripting in JSP Pages). The value supplied for the classattribute must be a fully qualified class name. Note that beans cannot be in the unnamed package. Thus the format of the value must be package-name.class-name.
The following element creates an instance of mypkg.myLocales if none exists, stores it as an attribute of the application scope, and makes the bean available throughout the application by the identifier locales:
<jsp:useBean id="locales" scope="application" class="mypkg.MyLocales"/>
Setting JavaBeans Component Properties
The standard way to set JavaBeans component properties in a JSP page is by using the jsp:setProperty element. The syntax of the jsp:setProperty element depends on the source of the property value. Table 5-6 summarizes the various ways to set a property of a JavaBeans component using the jsp:setProperty element.
Note -
Syntax rules of attribute values used in this table:
- beanName must be the same as that specified for the id attribute in a useBean element.
- There must be a setPropName method in the JavaBeans component.
- paramName must be a request parameter name.
Table 5-6 Valid Bean Property Assignments from String Values
Value Source | Element Syntax |
---|---|
String constant | <jsp:setProperty name="_beanName_" property="_propName_" value="_string-constant_"/> |
Request parameter | <jsp:setProperty name="_beanName_" property="_propName_" param="_paramName_"/> |
Request parameter name that matches bean property | <jsp:setProperty name="_beanName_" property="_propName_"/> <jsp:setProperty name="_beanName_" property="*"/> |
Expression | <jsp:setProperty name="_beanName_" property="_propName_" value="_expression_"/> <jsp:setProperty name="_beanName_" property="_propName_" > <jsp:attribute name="value"> expression |
A property set from a constant string or request parameter must have one of the types listed in Table 5-7. Because constants and request parameters are strings, the web container automatically converts the value to the property’s type; the conversion applied is shown in the table.
String values can be used to assign values to a property that has a PropertyEditor class. When that is the case, the setAsText(String) method is used. A conversion failure arises if the method throws an IllegalArgumentException.
The value assigned to an indexed property must be an array, and the rules just described apply to the elements.
You use an expression to set the value of a property whose type is a compound Java programming language type. The type returned from an expression must match or be castable to the type of the property.
Table 5-7 Valid Property Value Assignments from String Values
Property Type | Conversion on String Value |
---|---|
Bean Property | Uses setAsText(string-literal) |
boolean or Boolean | As indicated in java.lang.Boolean.valueOf(String) |
byte or Byte | As indicated in java.lang.Byte.valueOf(String) |
char or Character | As indicated in java.lang.String.charAt(0) |
double or Double | As indicated injava.lang.Double.valueOf(String) |
int or Integer | As indicated in java.lang.Integer.valueOf(String) |
float or Float | As indicated in java.lang.Float.valueOf(String) |
long orLong | As indicated in java.lang.Long.valueOf(String) |
short or Short | As indicated in java.lang.Short.valueOf(String) |
Object | new String(string-literal) |
The Duke’s Bookstore application demonstrates how to use the setProperty element to set the current book from a request parameter in the database bean in_tut-install_/javaeetutorial5/examples/web/bookstore2/web/books/bookdetails.jsp:
<c:set var="bid" value="${param.bookId}"/> <jsp:setProperty name="bookDB" property="bookId" value="${bid}" />
The following fragment from the page tut-install/javaeetutorial5/examples/web/bookstore2/web/books/bookshowcart.jsp illustrates how to initialize aBookDB bean with a database object. Because the initialization is nested in a useBeanelement, it is executed only when the bean is created.
<jsp:useBean id="bookDB" class="database.BookDB" scope="page"> <jsp:setProperty name="bookDB" property="database" value="${bookDBAO}" />
Retrieving JavaBeans Component Properties
The main way to retrieve JavaBeans component properties is by using the unified EL expressions. Thus, to retrieve a book title, the Duke’s Bookstore application uses the following expression:
${bookDB.bookDetails.title}
Another way to retrieve component properties is to use the jsp:getProperty element. This element converts the value of the property into a String and inserts the value into the response stream:
<jsp:getProperty name="_beanName_" property="_propName_"/>
Note that beanName must be the same as that specified for the idattribute in a useBean element, and there must be a get_PropName_ method in the JavaBeans component. Although the preferred approach to getting properties is to use an EL expression, the getProperty element is available if you need to disable expression evaluation.
Copyright © 2010, Oracle and/or its affiliates. All rights reserved. Legal Notices