1. steps:
1. Add a jar package:
Commons-beanutils-1.8.0.jar
Commons-logging.jar
2. Use setproperty () to set attribute values for JavaBean
Bu. setproperty (S, "name", "James"); bu. setproperty (S, "Age", "13"); bu. setproperty (S, "Birthday", "2011-10-9 ");
3. Use the getproperty () method to obtain the attribute value for JavaBean
String str_name = bu.getProperty(s, "name");String str_age = bu.getProperty(s, "age");String str_date = bu.getProperty(s, "birthday");
II. The automatic conversion of beanutils framework is only applicable to common data types. For custom data types, you must register them first.
Use the convertutils static method register ().
Convertutils. register (New converter () {public object convert (class clazz, object type) {// parameter 1: Java. util. date. class (target type) // parameter 2: Java. lang. string (input parameter type) string str_birthday = (string) type; simpledateformat SDF = new simpledateformat ("yyyy-mm-dd ");
Try {return SDF. parse (str_birthday);} catch (parseexception e) {e. printstacktrace (); return NULL ;}}, java. util. Date. Class );
Or
// Register through the datelocaleconverter () Class
Convertutils. Register (New datelocaleconverter (), date. Class );
Beanutils Framework Analysis