BBS水木清华站∶精华区

发信人: agents (智能代理), 信区: Java        
标  题: JSP语法(13) 
发信站: BBS 水木清华站 (Tue Jan  2 14:54:37 2001) 
 
【<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="{ string | <%= 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> 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: 
· By passing all of the values in the user's request (stored as parameters  
   in the request object) to matching properties in the Bean 
· By passing a specific value in the request object to a matching property  
   or a property of a different name in the Bean 
· By explicitly setting a Bean property to a value specified as a String or 
   the result of an expression 
   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: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 
   Property -------Type String Is Converted Using 
   boolean or Boolean-------- java.lang.Boolean.valueOf(String) 
   byte or Byte---------- java.lang.Byte.valueOf(String) 
   char or Character--------- java.lang.Character.valueOf(String) 
   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) 
    
   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. 
 
-- 
 
※ 修改:·agents 於 Jan  2 16:55:45 修改本文·[FROM:   166.111.54.22] 
※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.54.22] 

BBS水木清华站∶精华区