婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > JSP 開發(fā)之Spring BeanUtils組件使用

JSP 開發(fā)之Spring BeanUtils組件使用

熱門標(biāo)簽:南通通訊外呼系統(tǒng)產(chǎn)品介紹 外呼系統(tǒng)使用方法 自繪地圖標(biāo)注數(shù)據(jù) 潤(rùn)滑油銷售電銷機(jī)器人 電銷機(jī)器人免培訓(xùn) 海外圖書館地圖標(biāo)注點(diǎn) 電話機(jī)器人需要使用網(wǎng)絡(luò)嗎 給地圖標(biāo)注得傭金 如何看懂地圖標(biāo)注點(diǎn)

JSP 開發(fā)之Spring BeanUtils組件使用

用于演示的javabean

import java.util.Date;

public class People {

  private String name;
  private int age;
  private Date birth;

  public People(String name, int age, Date birth) {
    super();
    this.name = name;
    this.age = age;
    this.birth = birth;
  }

  public People() {
    super();
    // TODO Auto-generated constructor stub
  }

  @Override
  public String toString() {
    return "People [name=" + name + ", age=" + age + ", birth=" + birth + "]";
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public Date getBirth() {
    return birth;
  }

  public void setBirth(Date birth) {
    this.birth = birth;
  }

}

測(cè)試(所有測(cè)試只與源javabean屬性值有關(guān),與目標(biāo)javabean屬性值無關(guān))

當(dāng)源javabean屬性均有值時(shí)的目標(biāo)javabean屬性復(fù)制情況

@Test
public void springBeanUtilsTest(){
  People oldPeople = new People("oldName",100,new Date());
  People newPeople = new People();

  //BeanUtils.copyProperties(Object source,Object target);  
  BeanUtils.copyProperties(oldPeople, newPeople);

  System.out.println(oldPeople);
  System.out.println(newPeople);
}

輸出結(jié)果如下

People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017]
People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017]

當(dāng)源javabean非Date類型的屬性值為null時(shí)目標(biāo)javabean屬性的復(fù)制情況

@Test
public void springBeanUtilsTest(){
  People oldPeople = new People(null,100,new Date());
  People newPeople = new People("newName",20,null);

  //BeanUtils.copyProperties(Object source,Object target);  
  BeanUtils.copyProperties(oldPeople, newPeople);

  System.out.println(oldPeople);
  System.out.println(newPeople);
}

輸出結(jié)果如下

注意:目標(biāo)javabean中的非null屬性值被覆蓋為null了

People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017]
People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017]

當(dāng)源javabean中Date類型的屬性值為null時(shí)目標(biāo)javabean中屬性值的復(fù)制情況

@Test
public void springBeanUtilsTest(){
  People oldPeople = new People("oldName",100,null);
  People newPeople = new People("newName",20,new Date());

  //BeanUtils.copyProperties(Object source,Object target);  
  BeanUtils.copyProperties(oldPeople, newPeople);

  System.out.println(oldPeople);
  System.out.println(newPeople);
}

輸出結(jié)果如下

People [name=oldName, age=100, birth=null]
People [name=oldName, age=100, birth=null]

BeanUtils.copyProperties(Object source,Object target);方法有一個(gè)不足的地方,就是當(dāng)source里的屬性對(duì)應(yīng)的屬性值為null時(shí),也會(huì)覆蓋掉target里相同屬性名的屬性,即使target中該屬性值已存在且不為null的屬性值,這顯然有些不合理,這是我們可以使用它的一個(gè)重載方法:

BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);

最后一個(gè)參數(shù)的含義是,復(fù)制屬性值時(shí)忽略的屬性名稱,所有我們只要找出source中屬性值為null的屬性名稱數(shù)組即可,方法如下:

/**
 * 
 * @Title: getNullPropertyNames 
 * @Description: 獲取一個(gè)對(duì)象中屬性值為null的屬性名字符串?dāng)?shù)組
 * @param source
 * @return
 */
public static String[] getNullPropertyNames (Object source) {
  final BeanWrapper src = new BeanWrapperImpl(source);
  java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

  SetString> emptyNames = new HashSetString>();
  for(java.beans.PropertyDescriptor pd : pds) {
    Object srcValue = src.getPropertyValue(pd.getName());
    if (srcValue == null) emptyNames.add(pd.getName());
  }
  String[] result = new String[emptyNames.size()];
  return emptyNames.toArray(result);
}

測(cè)試

@Test
public void copyBeanNotNull(){
  People oldPeople = new People(null, 100, null);
  People newPeople = new People("newName", 20, new Date());

  //BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);  
  BeanUtils.copyProperties(oldPeople, newPeople, getNullPropertyNames(oldPeople));

  System.out.println(oldPeople);
  System.out.println(newPeople);
  for(String key : getNullPropertyNames(oldPeople)){
    System.out.println(key);
  }
}

輸出結(jié)果如下

People [name=null, age=100, birth=null]
People [name=newName, age=100, birth=Wed Jul 19 23:31:05 CST 2017]
name
birth

以上就是JSP中Spring BeanUtils組件的使用,如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

您可能感興趣的文章:
  • 淺析Java中Apache BeanUtils和Spring BeanUtils的用法

標(biāo)簽:大連 內(nèi)江 貸款邀約 銅川 廣州 黃石 樂山 南京

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP 開發(fā)之Spring BeanUtils組件使用》,本文關(guān)鍵詞  JSP,開,發(fā)之,Spring,BeanUtils,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《JSP 開發(fā)之Spring BeanUtils組件使用》相關(guān)的同類信息!
  • 本頁收集關(guān)于JSP 開發(fā)之Spring BeanUtils組件使用的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 桓台县| 民丰县| 龙游县| 开阳县| 石狮市| 即墨市| 买车| 赣州市| 淄博市| 阜新| 旺苍县| 南阳市| 长葛市| 油尖旺区| 垦利县| 祁连县| 黔东| 牟定县| 巩留县| 开鲁县| 昂仁县| 高青县| 怀远县| 荣成市| 民勤县| 玉山县| 安顺市| 盐山县| 灵丘县| 潞西市| 长宁区| 商河县| 彝良县| 于都县| 南木林县| 喀什市| 克东县| 雷州市| 福清市| 华池县| 页游|