什么是javabean呢?
简单来说,就是存粹的java对象,每个属性都提供对应的setXxx,getXxx方法。
下面是apache common beanutils对javabean的解释
get or set as the prefix for the property name with it‘s first character capitalizedpublic class Employee {
private String address;
private String firstName;
private String lastName;
private Employee employee;
private List<String> privilegeIdList;
private Map<String, String> theMap;
public Map<String, String> getTheMap() {
return theMap;
}
public void setTheMap(Map<String, String> theMap) {
this.theMap = theMap;
}
public List<String> getPrivilegeIdList() {
return privilegeIdList;
}
public void setPrivilegeIdList(List<String> privilegeIdList) {
this.privilegeIdList = privilegeIdList;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [address=" + address + ", firstName=" + firstName
+ ", lastName=" + lastName + ", employee=" + employee
+ ", privilegeIdList=" + privilegeIdList + ", theMap=" + theMap
+ "]";
}
}
<span style="font-family:SimSun;font-size:18px;">/*
* Licensed to Luohong Software.
*/
package embedding;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
/**
* how to use the BeanUtils to set property
* in the web, we can get the property, and then put the parameter to a map.
* at finally,put the property map to the obj by using BeanUtils.popualte()
*
* */
public class Employee {
private String address;
private String firstName;
private String lastName;
private Employee employee;
private List<String> privilegeIdList;
private Map<String, String> theMap;
public Map<String, String> getTheMap() {
return theMap;
}
public void setTheMap(Map<String, String> theMap) {
this.theMap = theMap;
}
public List<String> getPrivilegeIdList() {
return privilegeIdList;
}
public void setPrivilegeIdList(List<String> privilegeIdList) {
this.privilegeIdList = privilegeIdList;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return "Employee [address=" + address + ", firstName=" + firstName
+ ", lastName=" + lastName + ", employee=" + employee
+ ", privilegeIdList=" + privilegeIdList + ", theMap=" + theMap
+ "]";
}
public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException {
Employee employee = new Employee();
Map<String, Object> empMap = new HashMap<String, Object>();
empMap.put("address", "广东河源龙川");
empMap.put("firstName", "骆");
empMap.put("lastName", "Hong");
empMap.put("employee", new Employee()); //测试对象setXxx、getXxx
List<String> privilegeIdList = new ArrayList<String>();
privilegeIdList.add("hello");
privilegeIdList.add("hello");
privilegeIdList.add("hello");
privilegeIdList.add("hello");
empMap.put("privilegeIdList", privilegeIdList); //测试属性为List的setXxx、getXxx
Map<String, String> theMap = new HashMap<String, String>();
theMap.put("hello", "value");
theMap.put("hello12", "value");
theMap.put("hello2", "value");
theMap.put("hello3", "value");
empMap.put("theMap", theMap); //测试属性为map的setXxx、getXxx
//将map里面的内容,set进对象里面,前提是属性名与key相同
BeanUtils.populate(employee, empMap);
//复制
Employee copyEmployee = (Employee) BeanUtils.cloneBean(employee);
System.out.println(copyEmployee);//
//测试复制方法使用的是浅度复制
copyEmployee.getPrivilegeIdList().add("你好,世界");
String value = BeanUtils.getNestedProperty(copyEmployee, "theMap(hello12)");
System.out.println(value);
//获取一个bean的描述,使用map来存储
Map<String,String> map = BeanUtils.describe(employee);
System.out.println(map);
System.out.println(employee);
}
}</span><span style="font-family:SimSun;font-size:18px;">Employee [address=广东河源龙川, firstName=骆, lastName=Hong, employee=Employee [address=null, firstName=null, lastName=null, employee=null, privilegeIdList=null, theMap=null], privilegeIdList=[hello, hello, hello, hello], theMap={hello=value, hello12=value, hello2=value, hello3=value}]
value
{lastName=Hong, address=广东河源龙川, class=class embedding.Employee, employee=Employee [address=null, firstName=null, lastName=null, employee=null, privilegeIdList=null, theMap=null], firstName=骆, privilegeIdList=hello, theMap={hello=value, hello12=value, hello2=value, hello3=value}}
Employee [address=广东河源龙川, firstName=骆, lastName=Hong, employee=Employee [address=null, firstName=null, lastName=null, employee=null, privilegeIdList=null, theMap=null], privilegeIdList=[hello, hello, hello, hello, 你好,世界], theMap={hello=value, hello12=value, hello2=value, hello3=value}]
</span>原文地址:http://blog.csdn.net/u010469003/article/details/42677989