Org. apache. commons. beanutils. BeanMap simple example, commonsbeanutils

Source: Internet
Author: User

Org. apache. commons. beanutils. BeanMap simple example, commonsbeanutils
1. org. apache. commons. beanutils. BeanMap; a java bean can be called through the map api,

Several supported Operation interfaces:

  • Object get (Object key)
  • Object put (Object key, Object value)
  • Void putAll (Map t)
  • Set entrySet ()
  • Collection values ()
  • Boolean containsKey (Object key)
  • ....
For example: // Concatenate the student information into a string in the format of stuname | stuage | stuclass ProtectedString getLine (Student student) {StringBuilder sb = NewStringBuilder (); BeanMap map = NewBeanMap (student); appStr (sb, map, "stuname"); appStr (sb, map, "stuage"); appStr (sb, map, "stuclass "); ReturnSb. toString ();} // Combine the information of teacher into a string in the format of tname | tage | tclass ProtectedString getLine (Teacher teacher) {StringBuilder sb = NewStringBuilder (); BeanMap map = NewBeanMap (teacher); appStr (sb, map, "tname"); appStr (sb, map, "tage"); appStr (sb, map, "tclass "); ReturnSb. toString ();} // public method for String concatenation. different objects, such as studengt and teacher, are transmitted to the method through map. Protected VoidAppStr (StringBuilder sb, Map <String, Object> map, String str) {Object value = map. get (str); sb. append (newValue ). append ("|");} note: In the above example, although you can directly use sb. append (student. getStuname ()). append ("| "). append (student. getStuage ()). append ("| "). append (student. getStuclass); concatenates strings, but uses BeanMap. I think the reason is that multiple objects (such as student and teacher) Need to concatenate strings, if you want to abstract a public method, you can use BeanMap. In this way, you do not need to upload all objects to appStr. You only need to pass a BeanMap object.

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.