JSP 開發(fā)之Spring BeanUtils組件使用
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屬性值無(wú)關(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();
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);
}
測(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組件的使用,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
JSP中獲取ExtJS.Ajax前臺(tái)傳遞的JSON數(shù)據(jù)實(shí)現(xiàn)過(guò)程
JSON數(shù)據(jù)接收的特定過(guò)程必須的數(shù)據(jù)包:commons-lang,commons-beanutils等等,否則JSONObject 報(bào)錯(cuò),并且不能接收,感興趣的朋友可以參考下哈2013-04-04
Jsp中request的3個(gè)基礎(chǔ)實(shí)踐
本篇文章給大家分享了Jsp內(nèi)置對(duì)象request的3個(gè)基礎(chǔ)實(shí)踐以及相關(guān)代碼分享,有需要的朋友學(xué)習(xí)下。2018-04-04
Java 項(xiàng)目生成靜態(tài)頁(yè)面的代碼
第一次做項(xiàng)目需要生成靜態(tài)頁(yè)面,網(wǎng)上很多大牛對(duì)將網(wǎng)頁(yè)生成靜態(tài)頁(yè)面有很多異議。說(shuō)一下我的看法。2009-07-07
JSP動(dòng)態(tài)生成驗(yàn)證碼存儲(chǔ)在session作用范圍內(nèi)
下面的代碼實(shí)現(xiàn)的功能:寫一個(gè)JSP頁(yè),動(dòng)態(tài)生成一個(gè)驗(yàn)證碼,存儲(chǔ)在session作用范圍內(nèi),并以圖像形式返回給客戶端顯示2014-09-09
利用JSP session對(duì)象保持住登錄狀態(tài)
這篇文章主要為大家詳細(xì)介紹了如何利用JSP session對(duì)象保持住登錄狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
JSP spring boot / cloud 使用filter防止XSS
這篇文章主要介紹了JSP spring boot / cloud 使用filter防止XSS的相關(guān)資料,需要的朋友可以參考下2017-06-06
jsp只在首次加載時(shí)調(diào)用action實(shí)現(xiàn)代碼
如何只在首次加載時(shí)調(diào)用action,實(shí)現(xiàn)很簡(jiǎn)單只需判斷l(xiāng)ist==null即可,感興趣的朋友可以參考下2013-10-10

