Commons-beanutils is a software package in the subproject of Jakarta commons. Its main purpose is to use the reflection mechanism to process attributes of the JavaBean. We know that a JavaBean usually contains a large number of attributes. In many cases, processing of JavaBean results in a large number of get/SetCodeAccumulation increases the code length and difficulty in reading the code (what is your salary calculated based on the number of lines of code? Never let the boss see this post)
Beanutils is a commonly used tool class in this package. Here we only introduce its copyproperties () method. The method is defined as follows:
Public Static VoidCopyproperties (Java. Lang. Object DEST, java. Lang. Object orig)ThrowsJava. Lang. illegalaccessexception, java. Lang. Reflect. invocationtargetexception
If you have two JavaBeans with many identical attributes, a common situation is the Po object (Persistent Object) in struts and the corresponding actionform, such as teacher and teacherform. Generally, we construct a po object from actionform in action. The traditional method is to assign values to attributes one by one using statements similar to the following:
//Get teacherformTeacherform =(Teacherform) form;//Construct a teacher objectTeacher =NewTeacher ();//AssignmentTeacher. setname (teacherform. getname (); teacher. setage (teacherform. getage (); teacher. setgender (teacherform. getgender (); teacher. setmajor (teacherform. getmajor (); teacher. setdepartment (teacherform. getdepartment ());//Persists the teacher object to the databaseHibernatedao =; Hibernatedao. Save (teacher );
After using beanutils, the code is greatly improved, as shown below:
//Get teacherformTeacherform =(Teacherform) form;//Construct a teacher objectTeacher =NewTeacher ();//AssignmentBeanutils. copyproperties (teacher, teacherform );//Persists the teacher object to the databaseHibernatedao =; Hibernatedao. Save (teacher );
If Teacher and teacherform have attributes with different names, beanutils does not process these attributes and requiresProgramManually handled by the worker. For example, if teacher contains the modifydate attribute (the last modification date of this attribute record, which does not need to be input on the Interface) and teacherform does not have this attribute, then the copyproperties () in the above Code () add the following sentence:
Teacher. setmodifydate (NewDate ());
How about it? It's very convenient! In addition to beanutils, there is also a tool class named propertyutils. It also provides the copyproperties () method, which is similar to the beanutils method with the same name. The main difference is that the latter provides the type conversion function, that is to say, if the attributes with the same name of two JavaBean objects are of different types, they are converted within the supported data type range. The former does not support this function, but the speed is faster. Beanutils supports the following conversion types:
- Java. 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
Note that Java. util. date is not supported, and its subclass java. SQL. date is supported. Therefore, if the object contains time-type attributes and you want to convert them, you must use the java. SQL. date type. Otherwise, an argument mistype exception occurs during conversion.