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属性复制情况
@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);
}
输出结果如下
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属性的复制情况
@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);
}
输出结果如下
注意:目标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中属性值的复制情况
@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);
}
输出结果如下
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);
}
测试
@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);
}
}
输出结果如下
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组件的使用,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!