Jakarta commons beanutils (propertyutils beanutils. dynabean and beanutils. dynaclass beanutils. Convert

Source: Internet
Author: User

1. Introduction
Generally, when writing Bean components, you must write the setter and getter methods. Of course, if we already know the attributes and methods of bean, bean writing is relatively simple, however, when there are too many components, repeated writing is often boring. But sometimes, when I need to call the attributes of a dynamic object, how should we set and obtain the attributes of the object? Beanutils can help us solve this problem. It requires support for the collections package and logging package. The latest version is 1.7, and the document is still 1.6.1.

2. Cases in which beanutils has been used
* Build a scripting language that interacts with the Java object model, such as bean Scripting framework.
* Build a template language processor for web layer display and similar purposes, such as JSP or Velocity
* Build a custom tag library for JSP and xsp environments, such as Jakarta taglibs, struts, and cocoon.
* Process XML-based configuration resource files, such as ant creation scripts, Application Deployment files, and Tomcat XML configuration files

3. API Overview
Beanutils's main Java API package has a total of four
Org. Apache. commons. beanutils is the most basic package of beanutils. It provides a class for processing the attributes of getter and setter methods.
Org. Apache. commons. beanutils. converters: Standard Implementation of the converter interface, registered with convertutils at startup
Org. Apache. commons. beanutils. locale: localized Processing Package of the component
Org. Apache. commons. beanutils. locale. converters: Standard implementation of the localeconverter interface. It is registered with localeconvertutils at startup.

4.org. Apache. commons. beanutils Introduction
This topic only describes these four packages. When using other packages, please refer to its documentation. The document content is well written.
 1. propertyutils
It supports three types of attribute values: simple, indexed, and mapped.
First, create a simple bean.
Public class employee {
Public Address getaddress (string type );
Public void setaddress (string type, Address );
Public Employee getsubordinate (INT index );
Public void setsubordinate (INT index, employee subordinate );
Public String getfirstname ();
Public void setfirstname (string firstname );
Public String getlastname ();
Public void setlastname (string lastname );
}

 *Simple attribute access method
Propertyutils. getsimpleproperty (Object bean, string name)
Propertyutils. setsimpleproperty (Object bean, string name, object value)
Implementation Code:
Employee Employee = ...;
String firstname = (string) propertyutils. getsimpleproperty (employee, "firstname ");
String lastname = (string) propertyutils. getsimpleproperty (employee, "lastname ");
... Manipulate the values...
Propertyutils. setsimpleproperty (employee, "firstname", firstname );
Propertyutils. setsimpleproperty (employee, "lastname", lastname );

* Indexed attribute access method
Propertyutils. getindexedproperty (Object bean, string name)
Propertyutils. getindexedproperty (Object bean, string name, int index)
Propertyutils. setindexedproperty (Object bean, string name, object value)
Propertyutils. setindexedproperty (Object bean, string name, int index, object value)
Implementation Code:
Employee Employee = ...;
Int Index = ...;
String name = "subordinate [" + index + "]";
Employee subordinate = (employee) propertyutils. getindexedproperty (employee, name); // get Attributes Based on Value

Employee Employee = ...;
Int Index = ...;
Employee subordinate = (employee) propertyutils. getindexedproperty (employee, "subordinate", index); // The attribute value obtained based on the index value

 * Mapped attribute access method
Propertyutils. getmappedproperty (Object bean, string name)
Propertyutils. getmappedproperty (Object bean, string name, string key)
Propertyutils. setmappedproperty (Object bean, string name, object value)
Propertyutils. setmappedproperty (Object bean, string name, string key, object value)
Implementation Code:
Employee Employee = ...;
Address = ...;
Propertyutils. setmappedproperty (employee, "address (home)", address); // The attribute values that follow the values in the array.
Employee Employee = ...;
Address = ...;
Propertyutils. setmappedproperty (employee, "Address", "home", address );

 * The nested attribute access method // nested indicates that the parameter contains components.
Propertyutils. getnestedproperty (Object bean, string name)
Propertyutils. setnestedproperty (Object bean, string name, object value)
Implementation Code:
String city = (string) propertyutils. getnestedproperty (employee, "address (home). City ");
2. beanutils. dynabean and beanutils. dynaclass interface Introduction
Dynabean must have a bean to implement this interface. dynaclass must have a bean attribute set.

 * Basicdynabean and basicdynaclass-basic dynamic types
Basic APIs:
Basicdynaclass (Java. Lang. string name, java. Lang. Class dynabeanclass, dynaproperty [] properties)
Basicdynabean (dynaclass)
Let's define the basic code:
// Define a dynamic property set
Dynaproperty [] props = new dynaproperty [] {
New dynaproperty ("Address", java. util. Map. Class ),
New dynaproperty ("subordinate", mypackage. Employee []. Class ),
New dynaproperty ("firstname", String. Class ),
New dynaproperty ("lastname", String. Class)
};
// Create a dynamic class to set the dynamic attribute value
Basicdynaclass dynaclass = new basicdynaclass ("employee", null, props );
Dynabean Employee = dynaclass. newinstance ();
Employee. Set ("Address", new hashmap ());
Employee. Set ("subordinate", new mypackage. Employee [0]);
Employee. Set ("firstname", "Fred ");
Employee. Set ("lastname", "Flintstone ");
* Resultsetdynaclass (wraps resultset in dynabeans)-use the resultset
Dynamic JavaBean
API:
Resultsetdynaclass (Java. SQL. resultset)
Resultsetdynaclass (Java. SQL. resultset, Boolean lowercase)
Connection conn = ...;
Statement stmt = conn. createstatement ();
Resultset rs = stmt.exe cutequery
("Select account_id, name from MERs ");
Iterator rows = (New resultsetdynaclass (RS). iterator ();
While (rows. hasnext ()){
// Use dynamic bean for output
Dynabean ROW = (dynabean) rows. Next ();
System. Out. println ("account number is" +
Row. Get ("account_id") +
"And name is" + row. Get ("name "));
}
Rs. Close ();
Stmt. Close ();
* Rowsetdynaclass (disconnected resultset as dynabeans)-use rowset dynamic
JavaBean
API:
Rowsetdynaclass (Java. SQL. resultset)
Rowsetdynaclass (Java. SQL. resultset, Boolean lowercase)
 
Connection conn =...; // gets a connection from the buffer pool
Statement stmt = conn. createstatement ();
Resultset rs = stmt.exe cutequery ("Select ...");
Rowsetdynaclass RSDC = new rowsetdynaclass (RS );
Rs. Close ();
Stmt. Close ();
...; // Closes the connection and returns the buffer pool.
List rows = RSDC. getrows ();
...; // The row to be processed

 3. beanutils. convertutils Introduction
This package is mainly used to convert the functions passed from the request.
Main functions:
Convertutils (). Convert (Java. Lang. Object value)
Convertutils (). Convert (Java. Lang. String [] values, java. Lang. Class clazz)
Convertutils (). Convert (Java. Lang. String Value, java. Lang. Class clazz)
Implementation example:
Httpservletrequest request = ...;
Mybean bean = ...;
Hashmap map = new hashmap ();
Enumeration names = request. getparameternames ();
While (names. hasmoreelements ()){
String name = (string) names. nextelement ();
Map. Put (name, request. getparametervalues (name ));
}
Beanutils. populate (bean, MAP); // use convertutils for conversion
Currently supported conversion types:
Sjava. Lang. bigdecimal
Java. Lang. biginteger
Boolean and Java. Lang. Boolean
Byte and Java. Lang. byte
Char and Java. Lang. Character
Java. Lang. Class
Double and Java. Lang. Double
Float and Java. Lang. Float
INT and Java. Lang. Integer
Long and Java. Lang. Long
Short and Java. Lang. Short
Java. Lang. String
Java. SQL. Date
Java. SQL. Time
Java. SQL. Timestamp

5. References or articles:
* Http://jakarta.apache.org/commons/beanutils/api/index.html
* Http://jakarta.apache.org/commons/beanutils/index.html
* Http://apache.linuxforum.net/dist/jakarta/commons/beanutils/binaries/commons-beanutils-1.6.1.zip
The source code used in this article is from Apache

 

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.