BeanUtils.copyProperties(Dest,Orig)的不足 与 PropertyUtils
常常须要进行bean之间的数值 拷贝,这会用到BeanUtils.copyProperties方法,可是在页面上的checkbox为空的时候传的值是null,而通过 BeanUtils.copyProperties(DestBean,OrigBean)的拷贝,会把null设置为false,可是若是需求是这样 的:DestBean传null表明不给server传值,而传false表明把server数据改成false,这样显然 BeanUtils.copyProperties(Dest,Orig)方法就不太好用了,能够本身写一个方法:public static ActionForm updateFields(ActionForm dest, ActionBean source, List fieldList) { for (int i = 0; i < fieldList.size(); i++) { String name = (String) fieldList.get(i); Object value = PropertyUtils.getProperty(source, name); PropertyUtils.setProperty(dest, name, value); Object destvalue = PropertyUtils.getProperty(dest, name); } return dest; } 便可完成两个bean之间根据field list来相互赋值 其实根本不用本身写一个方法,直接用PropertyUtils就能够实现。若是checkbox值是null,那么传过去不会是false,而是null 两个bean 之间拷贝值,最笨的方法就是a.setXXX(b.getXXX())而后想到用java反射去找到各个字段,而后一个循环就能够实现bean的拷贝。可还有更加简便的方法,用PropertyUtils。import org.apache.commons.beanutils.PropertyUtils;try{ PropertyUtils.copyProperties(b, a); } catch(Exception e){ }