org.apache.commons.beanutils
org.apache.commons.beanutils.converters
org.apache.commons.beanutils.locale
org.apache.commons.beanutils.locale.converters
public class Company
{
private String name;
private HashMap address = new HashMap();
private String[] otherInfo;
private ArrayList product;
private ArrayList employee;
private HashMap telephone;
public Company(){}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAddress(String type)
{
return address.get(type).toString();
}
public void setAddress(String type, String address)
{
this.address.put(type,address);
}
public String[] getOtherInfo()
{
return otherInfo;
}
public void setOtherInfo(String[] otherInfo)
{
this.otherInfo = otherInfo;
}
public ArrayList getProduct()
{
return product;
}
public void setProduct(ArrayList product)
{
this.product = product;
}
public ArrayList getEmployee()
{
return employee;
}
public void setEmployee(ArrayList employee)
{
this.employee = employee;
}
public HashMap getTelephone()
{
return telephone;
}
public void setTelephone(HashMap telephone)
{
this.telephone = telephone;
}
}
System.out.println(BeanUtils.getProperty(c, "name"));
System.out.println(BeanUtils.getProperty(c, "address (A2)"));对于Indexed,则为“属性名[索引值]”,注意这里对于ArrayList和数组都可以用一样的方式进行操作。
HashMap am = new HashMap();
am.put("1","234-222-1222211");
am.put("2","021-086-1232323");
BeanUtils.setProperty(c,"telephone",am);
System.out.println(BeanUtils.getProperty(c, "telephone (2)"));
System.out.println(BeanUtils.getProperty(c, "otherInfo[2]"));当然这3种类也可以组合使用啦!
BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");
System.out.println(BeanUtils.getProperty(c, "product[1]"));
System.out.println(BeanUtils.getProperty(c, "employee[1].name"));
Company c2 = new Company();但是这种copy都是浅拷贝,复制后的2个Bean的同一个属性可能拥有同一个对象的ref,这个在使用时要小心,特别是对于属性为自定义类的情况。
BeanUtils.copyProperties(c2, c);
public void populate(java.lang.Object bean, java.util.Map properties) throws java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
LazyDynaMap dynaBean1 = new LazyDynaMap();
dynaBean1.set("foo", "bar"); // simple
dynaBean1.set("customer", "title", "Mr"); // mapped
dynaBean1.set("address", 0, "address1"); // indexed
System.out.println(dynaBean1.get("address",0));
Map myMap = dynaBean1.getMap(); // retrieve the Map
System.out.println(myMap.toString());
通过设置这两个属性,可以防止意外修改DynaBean的property。在设计架构时,你可以在后台从数据表或xml文件自动产生DynaBean,在传到控制层和表示层之前设置上述属性使其Bean结构不允许修改,如此就不可能无意中修改Bean包含的属性……这样既可以享用它的便利,有可以防止由此引入的错误可能,设计者实在深得偷懒的精髓啊!!!!!//取的字段的信息
dynaBean1.setReturnNull(true);//设为ture。若Bean中没有此字段,返回null
//默认为false。若Bean中没有此字段,自动增加一个:)
System.out.println(dynaBean1.get("aaa"));//此时返回null
Restricted——指定是否允许改变这个bean的property。
//MutableDynaClass.setRestricted设为true后,字段不可再增删和修改.
//默认为false,允许增删和修改
dynaBean1.setRestricted(true);
dynaBean1.set("test","error");//这里会出错!
Company c = init();
Company2 c2 = new Company2();
BeanUtils.copyProperties(c2,c);
// PropertyUtils.copyProperties(c2,c); 这句会报错!!
System.out.println(c2.getOtherInfo());
//test data type convert
//ArrayList a1 = BeanUtils.getProperty(c,"product");//BeanUtils返回的是String
System.out.println("--" + BeanUtils.getProperty(c,"product"));//取出后直接被转为String
ArrayList a = (ArrayList)PropertyUtils.getProperty(c,"product");//PropertyUtils返回的是Object
System.out.println("--" + a.get(1));