Contents | Prev | Next


<jsp:setProperty>

Sets a property value or values in a bean.

JSP Syntax

<jsp:setProperty name="beanInstanceName"	
{ 	
   property="*" |	
   property="propertyName" [ param="parameterName" ] |	
   property="propertyName" value="{stringLiteral| <%= expression %>}"	
}	
/>

XML Syntax

<jsp:setProperty name="beanInstanceName"	
{ 	
   property="*" |	
   property="propertyName" [ param="parameterName" ] |	
   property="propertyName" value="{stringLiteral | %= expression %}" 	
}	
/>

Examples

<jsp:setProperty name="mybean" property="*" />	
<jsp:setProperty name="mybean" property="username" />	
<jsp:setProperty name="mybean" property="username" value="Steve" />

Description

The <jsp:setProperty> element sets the value of one or more properties in a bean, using the bean's setter methods. You must declare the bean with <jsp:useBean> before you set a property value with <jsp:setProperty>. Because <jsp:useBean> and <jsp:setProperty> work together, the bean instance names they use must match (that is, the value of name in <jsp:setProperty> and the value of id in <jsp:useBean> must be the same).

You can use <jsp:setProperty> to set property values in several ways:

Each method of setting property values has its own syntax, as described in the next section.

Attributes and Usage

The values of the request parameters sent from the client to the server are always of type String. The String values are converted to other data types when stored in bean properties. If a property has a PropertyEditor class as indicated in the JavaBeans specification, the setAsText(String) method is used. A conversion failure arises if the method throws an IllegalArgumentException. The allowed bean property types and their conversion methods are shown in TABLE 1.

TABLE 1       How <jsp:setProperty> Converts Strings to Other Values

Property Type

String Is Converted Using

Bean Property

Use setAsText(stringLiteral)

boolean or Boolean

java.lang.Boolean.valueOf(String)

byte or Byte

java.lang.Byte.valueOf(String)

char or Character

java.lang.String.charAt(0)

double or Double

java.lang.Double.valueOf(String)

integer or Integer

java.lang.Integer.valueOf(String)

float or Float

java.lang.Float.valueOf(String)

long or Long

java.lang.Long.valueOf(String)

short or Short

java.lang.Short.valueOf(String)

Object

new String(string-literal)

You can also use <jsp:setProperty> to set the value of an indexed property in a bean. The indexed property must be an array of one of the data types shown in TABLE 1. The array elements are converted using the conversion methods shown in the table.

If a request parameter has an empty or null value, the corresponding bean property is not set. Likewise, if the bean has a property that does not have a matching request parameter, the property value is not set.

See Also

Tip

When you use property="*", the bean properties are not necessarily set in the order in which they appear in the HTML form or the bean. If the order in which the properties are set is important to how your bean works, use the syntax form property="propertyName" [ param="parameterName" ]. Better yet, rewrite your bean so that the order of setting properties is not important.



Contents | Prev | Next

Copyright © 2002, Sun Microsystems, Inc. All rights reserved.