Java Bean property values are dynamically set __java

Source: Internet
Author: User
Concept

Use property expressions to set property values for Java beans.

Property expression formats are similar to Java programming expressions, using "[n]" to represent an array reference, "." Represents an object reference.

For example:

"Name", which represents the Name property of the Java bean.

"Parent.name", which represents the Name property of the parent property of the Java bean.

"Parent.parent.name", which represents the Name property of the parent property of the Java Bean's Parent property.

"Children[2].name", which represents the Name property of the 2nd element of the Java Bean's children property.

When a property expression is used to navigate to the specified property, the input value is converted to the value of the property to complete the assignment, depending on the type of the property.

Use

Org.zjulhs.el.Property the static method SetProperty implement the assignment to the property:

public static void SetProperty (Object bean, String name, string[] values);

Parameters:

Bean

Java Bean Objects

Name

Property expressions for Java beans

Values

The value of the property specified by name, the array of types such as t[], values correspond to t[] One by one, other property types use Values[0], and if the property is null, it is automatically initialized to the corresponding type of object.

Example

Refer to the main method of Org.zjulhs.el.Person, which can be run directly.

Extended

Implement Org.zjulhs.el.IConvert interface, can extend the conversion method of custom object, reference Org.zjulhs.el.Property static attribute converts.

Application

Can be used to automatically map the form data to Java Bean property values, such as the product list of form orders to the Java Bean arraylist<product> Products automatic mapping, Eliminating the hassle of traditional request.getparameter methods and data type conversions, the following code enables automatic mapping of most form values:

Object bean = Formbean; The object that holds the data for the form

Enumeration<string> iter = (enumeration<string>) request.getparameternames ();

while (Iter.hasmoreelements ())

{

String key = Iter.nextelement ();

String[] values = req.getparametervalues (key);

Property.setproperty (bean, key, values);

}
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.