A typical feature of Struts is that actionform is used to collect form data, but all the collected form data is of the string type, if we use it directly, we will face a very troublesome problem: frequent type replacement. Struts uses beanutils to free us from heavy physical labor.
Beanutils Working principle:
Let's take a simple example to learn about its basic usage. First, create an entity class student.
Package COM. tgb. struts; import Java. SQL. date; public class student {// name private string name; // age private int age; // birthday private date birth; Public String getname () {return name ;} public void setname (string name) {This. name = Name;} public int getage () {return age;} public void setage (INT age) {This. age = age;} public date getbirth () {return birth;} public void setbirth (date Birth) {This. birth = birth ;}}
Compile the test class
Package COM. tgb. struts; import Org. apache. commons. beanutils. beanutils; import JUnit. framework. testcase; public class demo2test extends testcase {public void test01 () throws exception {// 1. load class clss = Class. forname ("com. tgb. struts. student "); // 2. create the bean object student ST = (student) clss. newinstance (); // 3. assign beanutils to the object attribute through beanutils. setproperty (St, "name", "L. "); // 4. String STR = beanutils. getproperty (St," name "); system. Out. println (STR );}}Run the test class and we will find that the input result on the console is "L ." That is the result we want. This seems simple. What does beanutils help us:
The parameters required by the beanutils setproperty (object, name, value) method are
Object = loading class Object
Name = Name of the class Property
Value = assigned value;
Beanutils's getproperty (object, name) method returns the string type, so it can be output directly;
If we write how we do it ourselves, first we need to know the attributes of the student class and the type of the attributes. Then, convert the data that we want to assign a value to the corresponding attribute to a usable type and assign a value to it. With beanutils, we only need to know its attribute name. The strong conversion of data has helped us do a good job. Another common method of beanutils is copyproperties (). If you have two Java Beans with many identical attributes, a common situation is the Po object (Persistent Object) in struts and the corresponding actionform, for example, 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:
// Obtain teacherformteacherform teacherform = (teacherform) form; // construct the teacher object Teacher = new teacher (); // assign a value to teacher. 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 database hibernatedao =; hibernatedao. save (teacher );
After using beanutils, the code is greatly improved, as shown below:
// Obtain teacherformteacherform teacherform = (teacherform) form; // construct the teacher object Teacher = new teacher (); // assign the value to beanutils. copyproperties (teacher, teacherform); // persists the teacher object to the database hibernatedao =; hibernatedao. save (teacher );
If Teacher and teacherform have attributes with different names, beanutils does not process these attributes and must be manually processed by the programmer. 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(new Date());
Of course, the above is the most basic use of beanutils, but how does it work. First, let's take a look at the convert method of the integerconverter class for integer installation:
Public object convert (class type, object Value) {// first, judge whether it is null. If (value = NULL) {If (usedefault) {return (defaultvalue);} else {Throw new conversionexception ("no value specified") ;}// determines whether it is of the integer type. If it is directly returned, however, the data we obtain from the form is always of the string type. // This function is often used when values are assigned between objects. // If it is not an object type, it is converted to an object and returned. If (value instanceof integer) {return (value);} Try {return (New INTEGER (value. tostring ();} catch (exception e) {If (usedefault) {return (defaultvalue);} else {Throw new conversionexception (E );}}}
In fact, the code is easy to understand. Other types are also similar. In fact, it is a simple encapsulation of our complicated work. When writing Bean components in general, we must write the setter and getter methods, of course, if we already know bean-related attributes and methods in advance, writing beans is relatively simple, but 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.
Struts1 -- View struts implementation principles from beanutils