jsp:setProperty

The jsp:setProperty action sets the value of one or more properties in a bean. The bean must be defined, using jsp:useBean, before using this action.

The following example sets a value for properties for a bean named user:

<jsp:setProperty name="user" property="user" param="username" />

The following example sets a property using an expression:

<jsp:setProperty name="results" property="row" value="<%= i+1 %>" />

Simple and indexed properties can be set using setProperty. When you assign values to indexed properties, the value must be an array.

The syntax for jsp:setProperty is shown below:

<jsp:setProperty name="beanName" prop_expr />

where prop_expr has one of the following forms:

property="*" |
property="propertyName"|
property="propertyName" param="parameterName"|
property="propertyName" value="propertyValue"

propertyValue must be either a string literal or an expression.

The jsp:setProperty element has the following attributes:

name

The name of a bean as defined by a jsp:useBean action or some other element. The bean instance must contain the property that you want to set. The jsp:useBean element must appear before the jsp:setProperty action in the same file.

property

The name of the bean property that you want to set.

If you set propertyName to *, then jsp:setProperty iterates over the current request parameters to match parameter names and value type(s) to bean property names and types. Each matched property is set to the value of the matching parameter. If a parameter has a value of an empty string (""), the corresponding property is not modified.

Each of the first three forms of prop_expr assign a value represented as a string to the bean property. However, if the bean property has a data type other than string, JRun performs a type conversion. The following table shows how this conversion works.

String Type Conversions in jsp:setProperty  
Property Type Conversion
boolean or Boolean As defined by java.lang.Boolean.valueOf(String)
byte or Byte As defined by java.lang.Byte.valueOf(String)
char or Character As defined by java.lang.Character.valueOf(String)
double or Double As defined by java.lang.Double.valueOf(String)
int or Integer As defined by java.lang.Integer.valueOf(String)
float or Float As defined by java.lang.Float.valueOf(String)
long or Long As defined by java.lang.Long.valueOf(String)

The fourth form of prop_expr shown previously assigns an object to the bean property. In this case, JRun automatically converts the object to the destination data type of the bean property.

param

The name of the request parameter whose value you want to give to a bean property.

An action may not have both a param and a value.

If you omit param, the request parameter name is assumed to be the same as the bean property name.

If param is not set in the request object, or if it has the value of the empty string (""), the jsp:setProperty element has no effect.

value

The value to assign to the property.

An action may not have both a param and a value.