Jakarta-Common-BeanUtils使用笔记
时间:2011-01-28 csdn博客 沈斌
下载地址:http://commons.apache.org/beanutils/
例子如下:
新建Bean:
package demo.bean;
import java.util.ArrayList;
import java.util.HashMap;
public class Company ...{
private String name;
private HashMap<String, String> address = new HashMap<String, String>();
private String[] otherInfo;
private ArrayList<String> product;
private ArrayList<String> employee;
private HashMap<String, String> telephone;
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public HashMap<String, String> getAddress() ...{
return address;
}
public void setAddress(HashMap<String, String> address) ...{
this.address = address;
}
public String[] getOtherInfo() ...{
return otherInfo;
}
public void setOtherInfo(String[] otherInfo) ...{
this.otherInfo = otherInfo;
}
public ArrayList<String> getProduct() ...{
return product;
}
public void setProduct(ArrayList<String> product) ...{
this.product = product;
}
public ArrayList<String> getEmployee() ...{
return employee;
}
public void setEmployee(ArrayList<String> employee) ...{
this.employee = employee;
}
public HashMap<String, String> getTelephone() ...{
return telephone;
}
public void setTelephone(HashMap<String, String> telephone) ...{
this.telephone = telephone;
}
}
Jakarta-Common-BeanUtils使用笔记(2)
时间:2011-01-28 csdn博客 沈斌
A.BeanUtils可以直接get和set一个属性的值。
package demo;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import org.apache.commons.beanutils.BeanUtils;
import demo.bean.Company;
public class BeanUtilsDemo ...{
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException ...{
Company c = new Company();
c.setName("Lucy");
System.out.println(BeanUtils.getProperty(c, "name"));
HashMap<String, String> am = new HashMap<String, String>();
am.put("1","234-222-1222211");
am.put("2","021-086-1232323");
BeanUtils.setProperty(c,"telephone",am);
System.out.println(c.getTelephone().get("1"));
}
}
B.copyProperty方法可以直接进行Bean之间的clone。
package demo;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
import demo.bean.Company;
public class BeanUtilsDemo ...{
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException ...{
Company c = new Company();
|