Use beanutils to assemble objects

Source: Internet
Author: User
Use beanutils to assemble objects

Keyword: beanutils populate object form binding

I have not used many beanutils tools, but I still have some experience to share with you ~~ All those who have used spring know that spring's MVC framework has a basecommandcontroller object. With this object, we can easily encapsulate the parameters passed from the client into a javajan object, we do not need to request. getparameter ("name"); bean. setname (name);, which simplifies a lot of work. In fact, structs also has such a function, but it uses the beanutils tool class, which I will tell you today.

The commons-beanutils component is one of the Jakarta Commons project teams. You can download it from the Jakarta official website. We are talking about the beanutils class in commons-beanutils. This class is a static method that allows you to easily manipulate each JavaBean object, including obtaining and setting attributes, here is an example.

Assume that the following JavaBean object is used:

Public testbean
{
Private string stringvalue;

Private int intvalue;

Public void setstringvalue (string value)
{
This. stringvalue = value;
}

Public String getstringvalue ()
{
Return this. stringvalue;
}

Public void setintvalue (INT value)
{
This. intvalue = value;
}

Public int getintvalue ()
{
Return this. intvalue;
}
}

Then we can use beanutils to set and obtain the value. As follows:

Testbean TB = new testbean ();
Beanutils. setproperty (TB, "stringvalue", "Hello world! ");
Beanutils. setproperty (TB, "intvalue", 123 );

With the preceding statement, we can set two attributes for the new object. Note that the first parameter is our attribute name, this is what we need to pay attention to when using this tool. This property name is XXX in the getxxx method in our testbean. For example, the property name in getstringvalue is stringvalue. When using the property name, except that the first letter in XXX does not need to be case-insensitive, the other parts must be case-sensitive, which is also the specification of JavaBean.Note that if the first word (defined by US) in the attribute name has only one letter, the attribute name must be capitalized!We can look at this example:

Add the following attribute to testbean:

......
Public void settname (string name)
{
This. Name = Name;
}

Public void gettname ()
{
Return this. Name;
}
......

When beanutils is used for Attribute assignment, if you write:

Beanutils. setproperty (TB, "tname", "hello ");

An exception is thrown, indicating that this attribute cannot be found,According to the JavaBean specification, if the attribute name contains multiple words, the first letter of each word must be capitalized.!Here, beanutils considers the attribute tname to be two words, T and name, respectively. Therefore, the first letter must be capitalized when obtaining the attribute, so its attribute should be; tname, then we should write the following statement:

Beanutils. setproperty (TB, "tname", "hello ");

This is correct. So let's look at the B/S structureProgram, How to convert the parameters of form fields (not necessarily form fields, but all parameters) into a JavaBean object. The bean above is an example, we will write the following code on the page:

......
<Form name = "form1" Action = "/getbean. jsp">
String Value: <input type = "text" name ="Stringvalue"/> <Br/>
Numeric value: <input type = "text" name ="Intvalue"/> <Br/>
</Form>
......

Then we write the following code on the server side:CodeTo obtain the parameters and assemble them into a testbean object. The Code is as follows:

......
Testbean TB = new testbean ();
Beanutils. populate (TB, request. getparametermap ());
// Use the beanutils tool class to obtain object attributes
System. Out. println (beanutils. getproperty ("stringvalue "));
System. Out. Print (beanutils. getproperty ("intvalue "));
// Directly obtain the Object Attributes
System. Out. println (Tb. getstringvalue ());
System. Out. println (Tb. getintvalue ());
......

We noticed that the names of several input boxes (marked in red) Written in the form area are the property names of our testbean object. Then, in the server code, we first use request. getparametermap () is used to obtain the submitted parameters and convert them into a map object storage (the key is the parameter name and the value is the submitted value). Then we use the beanutils populate method to assign values. After printing, we can see that the input value has been successfully set to the object. We can see that the two simple lines of code can be used to bind parameters to the object's data, if an object has more than a dozen or even dozens of parameter attributes, this method is very simple. You can write at least a dozen or even dozens of lines of code.

We can see that the beanutils tool class can be used to conveniently bind attributes of JavaBean and request parameters to attributes of JavaBean. For other functions, see its API documentation ~~~~

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.