The <jsp:setProperty> tag 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:
For more information, see the JavaServer Pages (JSP) v1.2 Syntax Reference on the Sun Microsystems® web site.
<jsp:setProperty name="beanInstanceName"
{
property="*" |
property="propertyName" [
param="parameterName" ] |
property="propertyName" value="{stringLiteral|
<%= expression %>}"
}
/>
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 JSP page.
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. 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" ]. Or rewrite your bean so that the order of setting properties is not important.
property="propertyName" [ param="parameterName" ]
Sets one bean property to the value of one request parameter. In the syntax, 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. If the bean property and the request parameter have different names, you must specify both property and param. If they have the same name, you can specify property and omit param. If a parameter has an empty or null value, the corresponding bean property is not set.
property="propertyName" value="{ String | <%= expression %>}"
Sets one bean property to a specific value. The value can be a 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. 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.
<jsp:setProperty name="mybean" property="*" /> <jsp:setProperty name="mybean" property="username" /> <jsp:setProperty name="mybean" property="username" value="Steve" />