<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'}' | <%=expression%>}" } />
XML Syntax
<jsp:setProperty name="beanInstanceName"
{
property="*" |
property="propertyName" [ param="parameterName" ] |
property="propertyName" value="{stringLiteral |
'${' Expression '}' | %= 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:
- By passing all of the values the user enters (stored as parameters in the
requestobject) to matching properties in the bean - By passing a specific value the user enters to a specific property in the bean
- By setting a bean property to a value you specify as either a
Stringor an expression that is evaluated at runtime
Each method of setting property values has its own syntax, as described in the next section.
Attributes and Usage
name="beanInstanceName"- The name of an instance of a bean that has already been created or located with a
jsp:useBeanelement. The value ofnamemust match the value ofidinjsp:useBean. Thejsp:useBeanelement must appear beforejsp:setPropertyin the JSP page.
- The name of an instance of a bean that has already been created or located with a
property="*"- Stores all of the values of request parameters in bean properties. The names of the bean properties must match the names of the request parameters. A bean property is usually defined by a variable declaration with matching getter and setter methods (for more information, see http://java.sun.com/javase/technologies/desktop/javabeans/docs/).
- Stores all of the values of request parameters in bean properties. The names of the bean properties must match the names of the request parameters. A bean property is usually defined by a variable declaration with matching getter and setter methods (for more information, see http://java.sun.com/javase/technologies/desktop/javabeans/docs/).
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 2.
- You can also use
jsp:setPropertyto 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 2. 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.
- 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. In the syntax,
propertyspecifies the name of the bean property andparamspecifies the name of the request parameter by which data is being sent from the client to the server.- If the bean property and the request parameter have different names, you must specify both
propertyandparam. If they have the same name, you can specifypropertyand omitparam.- If a parameter has an empty or null value, the corresponding bean property is not set.
- If the bean property and the request parameter have different names, you must specify both
- Sets one bean property to the value of one request parameter. In the syntax,
property="propertyName" value="{string| <%=expression%>}"- Sets one bean property to a specific value. The value can be a
Stringor an expression that is evaluated at runtime. If the value is aString, it is converted to the bean property's data type according to the conversion rules shown above in TABLE 2. If it is an expression, its value must have a data type that matches 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
paramandvalueattributes in ajsp:setPropertyelement. - If the parameter has an empty or null value, the corresponding bean property is not set. You cannot use both the
- Sets one bean property to a specific value. The value can be a
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.
