Preface

Comment

Declaration

Expression

Scriptlet

EL Expression

Directives

Attribute Directive

Include Directive

Page Directive

Tag Directive

Taglib Directive

Variable Directive

Standard Actions

<jsp:attribute>

<jsp:body>

<jsp:element>

<jsp:doBody>

<jsp:forward>

<jsp:getProperty>

<jsp:include>

<jsp:invoke>

<jsp:output>

<jsp:plugin>

<jsp:root>

<jsp:setProperty>

<jsp:text>

<jsp:useBean>

<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:

Each method of setting property values has its own syntax, as described in the next section.

Attributes and Usage

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.

TABLE 2       How jsp:setProperty Converts Strings to Other Values

Property Type

String Is Converted Using

Bean Property

Use setAsText(stringLiteral)

boolean or Boolean

java.lang.Boolean.valueOf(String)

byte or Byte

java.lang.Byte.valueOf(String)

char or Character

java.lang.String.charAt(0)

double or Double

java.lang.Double.valueOf(String)

integer or Integer

java.lang.Integer.valueOf(String)

float or Float

java.lang.Float.valueOf(String)

long or Long

java.lang.Long.valueOf(String)

short or Short

java.lang.Short.valueOf(String)

Object

new String(string-literal)

You can also use 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 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.

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.



宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有