国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

JSP 開發之Spring BeanUtils組件使用

2024-09-05 00:18:54
字體:
來源:轉載
供稿:網友

JavaBean在程序中的操作非常頻繁,但總有一些開源組件易于使用和擴展,為眾多開發人員提供了便利,下面一起來了解JSP 開發之Spring BeanUtils組件使用的知識。

JSP 開發之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;  }}

測試(所有測試只與源javabean屬性值有關,與目標javabean屬性值無關)

當源javabean屬性均有值時的目標javabean屬性復制情況

@Testpublic 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);}

輸出結果如下

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]

當源javabean非Date類型的屬性值為null時目標javabean屬性的復制情況

@Testpublic 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);}

輸出結果如下

注意:目標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]

當源javabean中Date類型的屬性值為null時目標javabean中屬性值的復制情況

@Testpublic 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);}

輸出結果如下

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

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

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

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

/** *  * @Title: getNullPropertyNames  * @Description: 獲取一個對象中屬性值為null的屬性名字符串數組 * @param source * @return */public static String[] getNullPropertyNames (Object source) {  final BeanWrapper src = new BeanWrapperImpl(source);  java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();  Set<String> emptyNames = new HashSet<String>();  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);}

測試

@Testpublic 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);  }}

輸出結果如下

People [name=null, age=100, birth=null]People [name=newName, age=100, birth=Wed Jul 19 23:31:05 CST 2017]namebirth我們看完了以上介紹的JSP 開發之Spring BeanUtils組件使用,可以關注錯新技術頻道小編之后推出的文章哦,希望大家要繼續加油。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 达拉特旗| 玉树县| 崇仁县| 阿尔山市| 宜兴市| 荣成市| 庄浪县| 马龙县| 民丰县| 石泉县| 安宁市| 聂荣县| 夏津县| 辛集市| 周至县| 黄冈市| 鄂伦春自治旗| 仲巴县| 辽宁省| 吉安市| 安图县| 浦县| 唐河县| 绥芬河市| 囊谦县| 大方县| 广西| 木兰县| 绥棱县| 根河市| 高台县| 托里县| 高密市| 定远县| 德惠市| 炎陵县| 井冈山市| 大冶市| 旌德县| 兴和县| 尼玛县|