[Learn Java for beginners] Use beanUtils to control javabean and beanutilsjavabean

Source: Internet
Author: User

[Learn Java for beginners] Use beanUtils to control javabean and beanutilsjavabean

Use BeanUtils to set/read attribute values and Auto conversion supported by default:

@ Test // use BeanUtils to set/read attribute values and automatically convert public void test1 () throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {Person p = new Person (); // use BeanUtils to set the attribute value BeanUtils. setProperty (p, "username", ""); // use BeanUtils to read the attribute value System. out. println (BeanUtils. getProperty (p, "username"); // different types can still be converted automatically. BeanUtils supports conversion of eight basic types by default. BeanUtils. setProperty (p, "age", "123"); System. out. println (p. getAge ());}

Register an existing converter to automatically convert complex types:

@ Test // register an existing converter to automatically convert complex types to public void test3 () throws IllegalAccessException, InvocationTargetException {Person p = new Person (); String birthday = "; // register the time converter ConvertUtils provided by Apache. register (new DateLocaleConverter (), Date. class); BeanUtils. setProperty (p, "birthday", birthday); System. out. println (p. getBirthday ());}

 

The existing Apache time converter cannot filter empty strings. If the string to be converted is empty, an exception is thrown. However, the real business is very complicated, apache cannot provide us with all types of conversion methods. When necessary, we can register the converters we need to complete the business needs.

 

Register your own converter completion time conversion:

@ Test // register your own converter completion time to convert public void test2 () throws IllegalAccessException, InvocationTargetException {Person p = new Person (); String birthday = "; // For the date value that can be assigned to the bean attribute, we register the date converter ConvertUtils for benUtils. register (new Converter () {@ SuppressWarnings ({"unchecked", "rawtypes"}) public Object convert (Class type, Object value) {if (value = null) {return null;} if (! (Value instanceof String) {throw new ConversionException ("only String type conversion supported");} String str = (String) value; if (str. trim (). equals ("") {return null;} SimpleDateFormat dateformate = new SimpleDateFormat ("yyyy-MM-dd"); try {return dateformate. parse (str);} catch (ParseException e) {throw new RuntimeException (e) ;}}, Date. class); BeanUtils. setProperty (p, "birthday", birthday); System. out. println (p. getBirthday ());}

 

Directly use the map object filling class:

@ Test // directly use the map object filling class public void test4 () throws Exception {HashMap <String, String> map = new HashMap <String, String> (); map. put ("username", "Li Si"); map. put ("password", "lisi"); map. put ("age", "26"); map. put ("birthday", "1990-05-05"); ConvertUtils. register (new DateLocaleConverter (), Date. class); Person p = new Person (); BeanUtils. populate (p, map); System. out. println (p. getUsername (); System. out. println (p. getPassword (); System. out. println (p. getAge (); System. out. println (p. getBirthday ());}

Related Article

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.