Setting JavaBeans Component Properties
There are two ways to set JavaBeans component properties in a JSP page: with the jsp:setProperty element or with a scriptlet
<% beanName.setPropName(value); %>The syntax of the jsp:setProperty element depends on the source of the property value. Table 12-1 summarizes the various ways to set a property of a JavaBeans component using the jsp:setProperty element.
Table 12-1 Setting JavaBeans Component Properties 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 matches bean property <jsp:setProperty name="beanName"
property="propName"/>
<jsp:setProperty name="beanName"
property="*"/>Expression <jsp:setProperty name="beanName"
property="propName"
value="<%= expression %>"/>1. beanName must be the same as that specified for the id attribute in a useBean element.
2. There must be a setPropName method in the JavaBeans component.
3. paramName must be a request parameter name.A property set from a constant string or request parameter must have a type listed in Table 12-2. Since both a constant and request parameter 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.
Table 12-2 Valid Value Assignments 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 in java.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 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) You would use a runtime expression to set the value of a property whose type is a compound Java programming language type. Recall from the section Expressions that a JSP expression is used to insert the value of a scripting language expression, converted into a String, into the stream returned to the client. When used within a setProperty element, an expression simply returns its value; no automatic conversion is performed. As a consequence, the type returned from an expression must match or be castable to the type of the property.
The Duke's Bookstore app demonstrates how to use the setProperty element and a scriptlet to set the current book for the database helper bean. For example, bookstore3/bookdetails.jsp uses the form
<jsp:setProperty name="bookDB" property="bookId"/>whereas bookstore2/bookdetails.jsp uses the form
<% bookDB.setBookId(bookId); %>The following fragments from the page bookstore3/showcart.jsp illustrate how to initialize a currency bean with a Locale object and amount determined by evaluating request-time expressions. Because the first initialization is nested in a useBean element, it is only executed when the bean is created.
<jsp:useBean id="currency" class="util.Currency" scope="session"> <jsp:setProperty name="currency" property="locale" value="<%= request.getLocale() %>"/> </jsp:useBean> <jsp:setProperty name="currency" property="amount" value="<%=cart.getTotal()%>"/>