background image

Retrieving JavaBeans Component Properties

<< Valid Bean Property Assignments | Using Custom Tags >>
<< Valid Bean Property Assignments | Using Custom Tags >>

Retrieving JavaBeans Component Properties

TABLE 5­7
Valid Property Value Assignments from String Values
(Continued)
Property Type
Conversion on String Value
float
or Float
As indicated in java.lang.Float.valueOf(String)
long
or Long
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 a BookDB bean with a database object. Because the initialization is
nested in a useBean element, 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}" />
</jsp:useBean>
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 id attribute in a useBean
element, and there must be a getPropName 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.
JavaBeans Components
Chapter 5 · JavaServer Pages Technology
171