Sunday, March 2, 2008

< jsp:setProperty>

Sets a property value or values in a Bean.


Examples

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

Description

The < jsp:setProperty> tag sets the value of one or more properties in a JavaBean component,

using the Bean's set methods. You must use a < jsp:useBean> tag before you use

< jsp:setProperty>. The value of name in < jsp:setProperty> must match the value of id in

< jsp:useBean>.


With < jsp:setProperty>, you can set property values in several ways:

* By passing all of the values in the user's request (stored as parameters in the

request object) to matching properties in the Bean
* By passing a specific value in the request object to a matching property or a property

of a different name in the Bean
* By explicitly setting a Bean property to a value specified as a String or the result

of an expression




Attributes and Usage

* name="beanInstanceName"

The name of an instance of a Bean that has already been created or located with a

< jsp:useBean> tag. The value of name must match the value of id in < jsp:useBean>. The

< jsp:useBean> tag must appear before < jsp:setProperty> in the same JSP file.

* property="*"

Stores all of the values in the request object parameters (called request parameters) in

matching Bean properties. The property names in the Bean must match the request parameters.

The parameter names usually come from the elements of an HTML form, and the values come from

the data the user enters.

The values of the request parameters are always of type String. The String values are

converted to other data types so they can be stored in Bean properties. The allowed Bean

property types and their conversion methods are as follows...



boolean or Boolean--java.lang.Boolean.valueOf(String)

double or Double--java.lang.Double.valueOf(String)

integer or Integer--java.lang.Integer.valueOf(String)



**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.



# property="propertyName" [ param="parameterName" ]

Sets one Bean property to the value of one request parameter. The request parameter can have

a different name than the Bean property, and if so, you must specify param. If the Bean

property and request parameter have the same name, you can omit param.

If the parameter has an empty or null value, the corresponding Bean property is not set.

You cannot use both the param and value attributes in a < jsp:setProperty> tag.
# property="propertyName" value="{ string | < %= expression %> }"

Sets one Bean property to a specific value. The value can be a String or an Expression. If

you use a String, it is converted to the Bean property's data type, according to the

conversion rules. If you use an expression, the data type of the

value of the expression must match the data type of the Bean property.

If the parameter has an empty or null value, the corresponding Bean property is not set.

You cannot use both the param and value attributes in a < jsp:setProperty> tag.


**The following snippet says "set the numberOfItems property to whatever the value of the

numItems request parameter is, if there is such a request parameter. Otherwise don't do

anything."

< jsp:setProperty name="orderBean"
property="numberOfItems"
param="numItems" />

If you omit both value and param, it is the same as if you supplied a param name that

matches the property name.

Click here for details from Sun

No comments: