Sets a property value or values in a Bean.
<jsp:setProperty name="
beanInstanceName"
{ property= "*" |
property="
propertyName"
[ param="
parameterName" ]| property="
propertyName" value="{
string| <%=
expression%> }" } />
<jsp:setProperty name="mybean" property="*" /> <jsp:setProperty name="mybean" property="username" /> <jsp:setProperty name="mybean" property="username" value="Steve" />
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 to declare the Bean 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:
request
object) to matching properties in the Bean
request
object to a matching property or a property of a different name in the Bean
String
or the result of an expression
Each method of setting property values has its own syntax, as described in the next section.
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 shown in TABLE 1-1.
How <jsp:setProperty> Converts Strings to Other Values
You can also use <jsp:setProperty>
to set the value of an indexed property in a Bean. The indexed property must have one of the
types shown in TABLE 1-1, and the request value assigned to it must be an array of the same type. The array elements are converted using the conversion methods
shown in TABLE 1-1.
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 shown above in TABLE 1-1. 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.