JSP Syntax (13)

Source: Internet
Author: User
Tags expression html form integer
js| syntax
<jsp:setProperty>

Sets the value of the property in the bean.



JSP syntax

<jsp:setproperty

Name= "Beaninstancename"

{

property= "*" |

property= "PropertyName" [param= "ParameterName"] |

Property= "PropertyName" value= "{string | <%= expression%>}"

}

/>

Example

<jsp:setproperty name= "Mybean" property= "*"/>

<jsp:setproperty name= "Mybean" property= "username"/>

<jsp:setproperty name= "Mybean" property= "username" value= "Steve"/>

Describe

<jsp:setProperty> element sets one or more property values in the bean, using the setter method given by the bean. You must use <jsp:useBean> to declare this bean before using this element. Because,<jsp:usebean> and <jsp:setProperty> are linked together, Also, the name of the bean instance that they use should match (that is, the value of name in <jsp:setProperty> should be the same as the value of ID in <jsp:useBean>)



You can use <jsp:setProperty> to set property values in a variety of ways:



Match the properties in the bean with all values entered by the user (as in the request object in the parameter store)

Matches the property specified in the bean with the specified value entered by the user

Use an expression at run time to match the Bean's properties

Each method of setting a property value has a specific syntax, which we'll explain here



Properties and their usage

Name= "Beaninstancename"

Represents the name of a bean instance that has been created in <jsp:useBean>.



property= "*"

Stores all values entered by the user in the JSP to match the properties in the bean. The name of the property in the bean must be the same as the parameter name in the Request object.



parameter values from the client to the service are typically character types that must be converted to other types in order to match in the bean, and the types of bean properties and their conversion methods are listed in the table below.



Converts a string to another type of method. Property type

Method





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)







If there is a null value in the parameter value of the request object, then the corresponding Bean property will not set any value. Similarly, if there is a property in the bean that does not have a request parameter value corresponding to it, then the property is not set.



property= "PropertyName" [param= "ParameterName"]

Use one of the parameter values in the request to specify a property value in the Bean. In this syntax, the property specifies the name of the Bean's attribute, param specifies the name of the parameter in the request.



If the bean attribute and the request parameter have different names, you must specify the property and the Param, and if they have the same name, then you just need to indicate the properties.



If the parameter value is empty (or uninitialized), the corresponding Bean property is not set.



Property= "PropertyName" value= "{string | <%= expression%>}"

Sets the Bean property with the specified value. This value can be a string, or it can be an expression. If this string, it is converted to the type of the Bean property (see the table above). If it is an expression, then its type must match the type of the property value it will set.



If the parameter value is null, then the corresponding property value is not set. In addition, you cannot use Param and value in a <jsp:setProperty>



Skills

If you use the property= "*", then the Bean's properties are not necessarily sorted by the order of the HTML form


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.