Beanutils Use Cases

Source: Internet
Author: User
1. beanutils framework/tool (developed by Apache open-source organization)
(1) The beanutils framework can complete all functions of internal provinces and optimize
(2) The beanutils framework can automatically convert the string <-> basic types (that is, the conversion of the eight basic types)
(3) beanutils framework custom converter:
Convertutils. Register (Conversion rule, class of the target object)
(4) registering a custom converter with the beanutils framework must be placed before the bu. setproperty () code.
(5) use the beanutils built-in string-> date converter:

Convertutils. Register (New datelocaleconverter (), java. util. Date. Class );

(6) Two jar packages to be used:
Commons-beanutils-1.8.0.jar and commons-logging.jar

Copy the two packages to myeclipse or eclipse and place the cursor over the jar package, right-click and choose "build path" --> "add to build path" to use these two jar packages in the Code (and the code will only prompt)

2. Code exercises:


Student code (student. Java ):


Package CN. WWH. www. java. beanutils;


Import java. util. date;


/**
* Function of the class: Special Note: The birthday is written, while the birthday is still used in beanutils. At this time, the program cannot pass,
* If the value of D in getbirthday and setbirthday is changed, the system runs successfully. As mentioned earlier, the framework is related to setxxx and getxxx, but not to private date birthay;
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 11:49:19
*/
Public class student {


Private string name;
Private int age;
Private date birthday;

Public student (){
System. Out. println ("constructor ");
}
/**
* @ Return the name
*/
Public String getname (){
Return name;
}
/**
* @ Param name the name to set
*/
Public void setname (string name ){
This. Name = Name;
}
/**
* @ Return the age
*/
Public int getage (){
Return age;
}
/**
* @ Param age the age to set
*/
Public void setage (INT age ){
This. Age = age;
}
/**
* @ Return the birthday
*/
Public date getbirthday (){
Return birthday;
}
/**
* @ Param birthday the birthday to set
*/
Public void setbirthday (date birthday ){
This. Birthday = birthday;
}

}

Beanutils test code (demo1.java ):


Package CN. WWH. www. java. beanutils;


Import java. Lang. Reflect. invocationtargetexception;
Import java. Text. parseexception;
Import java. Text. simpledateformat;
Import java. util. date;
Import org. Apache. commons. beanutils. beanutils;
Import org. Apache. commons. beanutils. convertutils;
Import org. Apache. commons. beanutils. converter;
Import org. Apache. commons. beanutils. locale. converters. datelocaleconverter;
Import org. JUnit. test;


/**
* Function of the class: Because beanutil can only convert eight basic types. If you want to convert other types,
* You need to define the conversion method. For example, you want to convert the date type.
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 11:49:07
*/
Public class demo {


// Customize a type converter (format the output of string ----> date)
@ Test
Public void testdemo1 () throws exception {
Student = new student ();
Beanutils bu = new beanutils ();

// Register
Convertutils. Register (New converter (){
@ Override
Public object convert (class calzz, object type ){
/**
* Parameter 1: Class, java. uitl. date; (target type) parameter 2: input parameter type, java. Lang. String;
*/
String strbirthday = (string) type;
Simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");


Try {
Return SDF. parse (strbirthday );
} Catch (parseexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
Return NULL;
}
}
}, Date. Class );


Bu. setproperty (student, "name", "");
Bu. setproperty (student, "Age", "22 ");
Bu. setproperty (student, "Birthday", "2014-07-22 ");


// Retrieve data
String name = Bu. getproperty (student, "name ");
String age = Bu. getproperty (student, "Age ");
String Birthday = Bu. getproperty (student, "Birthday ");


System. Out. println ("name:" + name + "\ Nage:" + age + "\ nbirthday :"
+ New date (birthday). tolocalestring ());


/*
* Output result: Constructor
* Name: ye Binzhou
* Age: 22
* Birthday: 14:00:00 (I have not modified the time, it is related to the source code call in it)
*/


}
@ Test
Public void testdemo2 () throws exception {
Student = new student ();
Beanutils bu = new beanutils ();

Convertutils. Register (New datelocaleconverter (), java. util. Date. Class );



Bu. setproperty (student, "name", "");
Bu. setproperty (student, "Age", "22 ");
Bu. setproperty (student, "Birthday", "2014-07-22 ");


// Retrieve data
String name = Bu. getproperty (student, "name ");
String age = Bu. getproperty (student, "Age ");
String Birthday = Bu. getproperty (student, "Birthday ");


System. Out. println ("name:" + name + "\ Nage:" + age + "\ nbirthday :"
+ New date (birthday). tolocalestring ());


}
}

Code test:




Beanutils Use Cases

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.