Sets a property value or values in a bean.
<jsp:setProperty name="
beanInstanceName
" { property="*" | property="
propertyName
"
[ param="
parameterName
" ]| property="
propertyName
" value="{
stringLiteral
| <%=
expression
%>}" } />
<jsp:setProperty name="beanInstanceName
" { property="*" | property="propertyName
" [ param="parameterName
" ] | property="propertyName
" value="{stringLiteral
| %=expression
%}" } />
<jsp:setProperty name="mybean" property="*" /> <jsp:setProperty name="mybean" property="username" /> <jsp:setProperty name="mybean" property="username" value="Steve" />
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:
request
object) to matching properties in the bean
String
or an expression that is evaluated at runtime
Each method of setting property values has its own syntax, as described in the next section.
name="
beanInstanceName
"
<jsp:useBean>
element. The value of name
must match the value of id
in <jsp:useBean>
. The <jsp:useBean>
element must appear before <jsp:setProperty>
in the JSP page.
property="*"
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.
<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.
property="
propertyName
" [ param="
parameterName
" ]
property
specifies the name of the bean property and param
specifies the name of the request parameter by which data is being sent from the client to the server.
property
and param
. If they have the same name, you can specify property
and omit param
.
property="
propertyName
" value="{
string
| <%=
expression
%>}"
String
or an expression that is evaluated at runtime. If the value is a String
, it is converted to the bean property's data type according to the conversion rules shown above in TABLE 1. If it is an expression, its value must have a data type that matches the the data type of the value of the expression must match the data type of the bean property.
param
and value
attributes in a <jsp:setProperty>
element.
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.