<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 15, 文章 - 0, 評論 - 5, 引用 - 0
    數據加載中……

    小結 Commons BeanUtils

     

    Commons BeanUtils 的官方網址:http://commons.apache.org/beanutils/

    Commins BeanUtils是針對JavaBeans一般性操作的組件,可以用來對JavaBeans進行復制,屬性的讀取,設置,修改,還以動態構造JavaBeans對象。

    使用這個組件需要三個Jar文件
    其中兩個是 commons-logging-1.1.1下的commons-logging-1.1.1.jar 和commons-logging-api-1.1.1.jar
    剩下一個是   commons-beanutils-1.8.0-BETA   下的commons-beanutils-1.8.0-BETA.jar
    把這三個加入到項目的構件路徑下即可。

    下面為一個簡單的例子

    新建User Profile Address BeanUtilsExample 四個類


    1 User.java

    package com.v503.zhouzhou;

    public class User {
     private Long userId;
     private String username;
     private String password;
     private Profile profile;

     public Long getUserId() {
      return userId;
     }

     public  void setUserId(Long userId) {
      this.userId = userId;
     }

     public String getUsername() {
      return username;
     }

     public void setUsername(String username) {
      this.username = username;
     }

     public String getPassword() {
      return password;
     }

     public void setPassword(String password) {
      this.password = password;
     }

     public Profile getProfile() {
      return profile;
     }

     public void setProfile(Profile profile) {
      this.profile = profile;
     }

    }

    2 Profile.java

    package com.v503.zhouzhou;

    import java.util.Date;
    import java.util.Map;

     

    public class Profile {
     private Map<String, String> phone;
     private Address[] address;
     private Date birthDate;
     private String email;

     public Map<String, String> getPhone() {
      return phone;
     }

     public void setPhone(Map<String, String> phone) {
      this.phone = phone;
     }

     public Address[] getAddress() {
      return address;
     }

     public void setAddress(Address[] address) {
      this.address = address;
     }

     public Date getBirthDate() {
      return birthDate;
     }

     public void setBirthDate(Date birthDate) {
      this.birthDate = birthDate;
     }

     public String getEmail() {
      return email;
     }

     public void setEmail(String email) {
      this.email = email;
     }

    }



    3 Address.java

    package com.v503.zhouzhou;

    public class Address {
     private String postCode;
     private String country;
     private String city;
     private String addr;

     public Address() {

     }

     public Address(String postCode, String country, String city, String addr) {
      this.postCode = postCode;
      this.country = country;
      this.city = city;
      this.addr = addr;
     }

     public String getPostCode() {
      return postCode;
     }

     public void setPostCode(String postCode) {
      this.postCode = postCode;
     }

     public String getCountry() {
      return country;
     }

     public void setCountry(String country) {
      this.country = country;
     }

     public String getCity() {
      return city;
     }

     public void setCity(String city) {
      this.city = city;
     }

     public String getAddr() {
      return addr;
     }

     public void setAddr(String addr) {
      this.addr = addr;
     }

    }



    4 BeanUtilsExample.java


    package com.v503.zhouzhou;

    import java.lang.reflect.InvocationTargetException;
    import java.util.GregorianCalendar;
    import java.util.HashMap;
    import java.util.Map;

    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.PropertyUtils;


    public class BeanUtilsExamples {

     @SuppressWarnings("unused")
     private User prepareData() {
      Address[] address = { new Address("111111", "中國", "保定", "河北大學"),
        new Address("22222", "中國", "保定", "河北大學工商學院") };
      Profile profile = new Profile();
      profile.setBirthDate(new GregorianCalendar(1987, 04, 17).getTime());
      profile.setEmail("aa1987417@126.com");
      Map<String, String> phone = new HashMap<String, String>();
      phone.put("mobilephone", "1532222706");
      phone.put("home", "110");
      profile.setPhone(phone);
      profile.setAddress(address);

      User user = new User();
      user.setUserId(new Long(503));
      user.setUsername("zhouzhou");
      user.setProfile(profile);
      user.setPassword("hicc");
      return user;

     }

     public static void main(String[] args) {
      BeanUtilsExamples a = new BeanUtilsExamples();
      User user = a.prepareData();
      System.out.println("輸出對象的屬性值---------------------------------");
      try {
       System.out.println(BeanUtils.getProperty(user, "userId"));       //BeanUtils中讀取屬性的方法getProperty()
       System.out.println(BeanUtils.getProperty(user, "username"));
       System.out.println(BeanUtils.getProperty(user, "password"));
       System.out.println(BeanUtils.getProperty(user, "profile.email"));
       System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
       System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));
       System.out.println(BeanUtils.getProperty(user, "profile.phone(mobilephone)"));
       System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
       System.out.println(PropertyUtils.getProperty(user, "profile.address[1].country"));
       
       User user2 = new User();
       BeanUtils.copyProperties(user2, user); //BeanUtils中復制屬性的方法getProperty()

       System.out.println("輸出復制屬性的屬性值-------------------------------");
       System.out.println(BeanUtils.getProperty(user, "username"));
       System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
       System.out.println(BeanUtils.getProperty(user, "profile.phone(home)"));
       System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
       
       
       System.out.println("輸出復制屬性修改以后的屬性值---------------------");
       BeanUtils.setProperty(user2, "userId", new Long(8888888));   //設置屬性的方法
       PropertyUtils.setProperty(user2, "username", "周旭");
       BeanUtils.setProperty(user2, "profile.email", "549748067@qq.com");
       BeanUtils.setProperty(user2, "profile.birthDate", new GregorianCalendar(2008, 8, 1).getTime());
       BeanUtils.setProperty(user2, "profile.address[0]", new Address("6666666", "中國","紫園","保定"));
       System.out.println(BeanUtils.getProperty(user2, "userId"));
       System.out.println(BeanUtils.getProperty(user2, "username"));
       System.out.println(BeanUtils.getProperty(user2, "profile"));
       System.out.println(BeanUtils.getProperty(user2, "profile.email"));
       System.out.println(BeanUtils.getProperty(user2, "profile.birthDate"));
       System.out.println(BeanUtils.getProperty(user2, "profile.address[0].city"));

       System.out.println("與被復制屬性值的對象的比較-------------------------------");
       System.out.println(BeanUtils.getProperty(user, "userId"));
       System.out.println(BeanUtils.getProperty(user, "username"));
       System.out.println(BeanUtils.getProperty(user, "profile"));
       System.out.println(BeanUtils.getProperty(user, "profile.email"));
       System.out.println(BeanUtils.getProperty(user, "profile.birthDate"));
       System.out.println(BeanUtils.getProperty(user, "profile.address[0].city"));
      } catch (IllegalAccessException e) {

       e.printStackTrace();
      } catch (InvocationTargetException e) {

       e.printStackTrace();
      } catch (NoSuchMethodException e) {

       e.printStackTrace();
      }

     }

    }


     




     

    posted on 2008-08-01 10:38 zhouzhou@ 閱讀(594) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品国产综合久久一线| 日韩欧美一区二区三区免费观看| 吃奶摸下高潮60分钟免费视频| 亚洲w码欧洲s码免费| 午夜免费1000部| 日韩亚洲国产高清免费视频| 亚洲成在人线aⅴ免费毛片| 亚洲av片不卡无码久久| 99re热免费精品视频观看| 国产精品亚洲综合五月天| 日韩午夜免费视频| 青青草97国产精品免费观看| 国产亚洲精品影视在线产品| 中文字幕久精品免费视频| 亚洲网站在线观看| 成人免费视频77777| mm1313亚洲国产精品无码试看 | 国产精品亚洲午夜一区二区三区| 青青草a免费线观a| 国产亚洲一卡2卡3卡4卡新区| 亚洲熟伦熟女新五十路熟妇| 一区二区三区无码视频免费福利| 亚洲自偷精品视频自拍| 西西大胆无码视频免费| 国产亚洲精品美女久久久久 | 久久精品无码专区免费| 亚洲国产一区在线| 成年女人毛片免费视频| 免费无码午夜福利片| 亚洲国产a∨无码中文777| 成人黄色免费网站| 黄床大片30分钟免费看| 久久亚洲精品中文字幕无码| 在线天堂免费观看.WWW| 免费的黄色的网站| 精品日韩亚洲AV无码一区二区三区| 免费看的成人yellow视频| 中国国语毛片免费观看视频| 亚洲色图激情文学| 国产亚洲AV无码AV男人的天堂| 在人线av无码免费高潮喷水|