Beanutils Framework Analysis

Source: Internet
Author: User

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

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.