Jakarta Commons Beanutils1. Introduction When writing Bean components, you must write the setter and getter methods. Of course, if we have known Beans related properties and methods, write beans is relatively simple, but when the components are too many times, Repeated writing is often boring and bored. But when I need to call the properties of the dynamic object, how should we set and get the object of the object? Beanutils can help us solve this problem. It requires the support of the Collectes package and Logging packets.
The latest version 1.7, document is still 1.6.1 .2. 2. Examples of BeanUtils * Script language that builds and Java object model interacts, such as bean scripting framework * Build a web layer display and similar use template language processor, such as JSP or Velocity * Build a custom label library for JSP and XSP environments, such as Jakarta Taglibs, Struts, Cocoon * Processes XML-based configuration resource files, such as Ant creation scripts, application deployment files and Tomcat XML configuration files 3.API profile The main package of Beanutils's main package is four org.apache.commons.beanutils This is the most basic package of beanutils, providing the standard implementation of class org.apache.commons.beanutils.converters Converter interface for processing getter and setter method properties, Register the Org.Apache.commons.beanutils.Loc.Convers LocaleConverter Interface Standils for Standling, Starting from LocaCaleConvertUTUTILS. .apache.commons.beanutils Introduction These four package topics only introduce this one, please refer to its documentation when using other uses, the document content is very good. 1.PropertyUtils It supports three types of property values - Simple , Indexed, Mapped let's create a simple bean public class Employee {public address getAddress (String type); public void setAddress (String type, address address); public Employee getSubordinate (int index); public void setSubordinate (int index, Employee Subordinate); public string getfirstname (); pu blic void setFirstName (String firstName); public String getLastName (); public void setLastName (String lastName);} * Simple property 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 property accessor methods 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 according to Value
Employee employee = ...; int index = ...; Employee subordinate = (Employee) PropertyUtils.getIndexedProperty (employee, "subordinate", index); // attribute value based on the index value taken property * Mapped 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) to achieve Code: Employee Employee = ...; address address = ...; propertyUtils.SetMappedProperty (Employee, "Address (Home)", address; // According to the value of the value within the array //, Employe Employee = ...; address address = ...; propertyutils.setmappedProperty (Employee, "Address", "Home", address; * NESTED Property Access Method // NESTED means included in parameters included Components PropertyUtils.getnestedProperty (Object Bean " String name) PropertyUtils.setnestedProperty (Object Bean, String Name, Object Value) Implementation: String City = (String) PropertyUtils.getnestedProperty (Employee, "Address (Home) .city") 2.BeanUtils.dynabean and beanutils.dynaclass interface This section describes that Dynabean must have a bean to implement this interface. Dynaclass must have a bean's property set to implement * Basic DynaBean and BasicDynaclass - Basic Dynamic Type Basic API: BasicDynaclass (java.lang .String name, java.lang.Class dynaBeanClass, DynaProperty [] properties) BasicDynaBean (DynaClass dynaClass) we define what basic code line: // define a dynamic set of attributes 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 }
// set dynamic class to create a 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) - using the ResultSet Dynamic JavaBean API: ResultSetDynaClass (java.sql.ResultSet resultSet) ResultSetDynaClass (java.sql.ResultSet resultSet, boolean lowerCase) Connection conn = ...; Statement stmt = conn.createStatement (); ResultSet rs = stmt. ExecuteQuery ("SELECT ACCOUNT_ID, NAME from Customers"); item Rows = (NEW RESULTSETDYNACLASS (RS)). Iterator (); whene (rows.hasnext ()) {// Use dynamic bean to 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 .C lose (); * RowSetDynaClass (Disconnected ResultSet as DynaBeans) - RowSet use of Dynamic JavaBean API: RowSetDynaClass (java.sql.ResultSet resultSet) RowSetDynaClass (java.sql.ResultSet resultSet, boolean lowerCase) Connection conn = ...; // Get connected from the buffer pool Stmt = conn.createstatement (); ResultSet RS = Stmt.executeQuery ("SELECT ..."); RowsetDynaclass RSDC = New RowSetDynaclass (RS); rs.close (); stmt.close (); ...; // Close the connection return buffer pool List rows = rsdc.getrows (); ...;