BeanUtils and PropertyUtils in POJO

BeanUtils and PropertyUtils in POJO

1. my test POJO
Person.java:
package com.sillycat.easymarket.model;
import java.util.Map;
public class Person {
private Integer id;
private String personName;
private String personPassword;
private String gender;
private Integer age;
private Address address;
private Map<String,Object> others;

        ...snip getter and setter ...

public String toString() {
return "Person [id=" + id + ", personName=" + personName
+ ", personPassword=" + personPassword + ", gender=" + gender
+ ", age=" + age + ", address=" + address + ", others="
+ others + "]";
}
}

Address.java:
package com.sillycat.easymarket.model;
public class Address {
private Integer id;
private String email;
private String city;
private String country;

...snip getter and setter ...

public String toString() {
return "Address [id=" + id + ", email=" + email + ", city=" + city
+ ", country=" + country + "]";
}
}

2. My test Class
BeanFilter.java:
package com.sillycat.easymarket.filter;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import com.sillycat.easymarket.model.Address;
import com.sillycat.easymarket.model.Person;
public class BeanFilter {
public static void main(String[] args) {
Person p = new Person();
Address a = new Address();
a.setCity("Chengdu");
a.setCountry("China");
a.setEmail("[email protected]");
a.setId(Integer.valueOf(1));
p.setAddress(a);
p.setAge(Integer.valueOf(29));
p.setGender("man");
p.setPersonName("sillycat");
p.setPersonPassword("test");
p.setId(Integer.valueOf(1));
Map<String, Object> others = new HashMap<String, Object>();
others.put("other1", "good");
others.put("other2", "other requirement");
p.setOthers(others);
System.out.println(p);
System.out.println(p.getOthers());
try {
PropertyUtils.setProperty(p, "personName", null);
PropertyUtils.setProperty(p, "address.city", null);
PropertyUtils.setProperty(p, "others.other2", null);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
System.out.println(p);
System.out.println(p.getOthers());
}
}

3. The console output
Person [id=1, personName=sillycat, personPassword=test, gender=man, age=29, address=Address [id=1, [email protected], city=Chengdu, country=China], others={other1=good, other2=other requirement}]
{other1=good, other2=other requirement}
Person [id=1, personName=null, personPassword=test, gender=man, age=29, address=Address [id=1, [email protected], city=null, country=China], others={other1=good, other2=null}]
{other1=good, other2=null}



references:
http://www.4ucode.com/Study/Topic/434994

http://www.cnblogs.com/H_Razor/archive/2011/03/01/1967620.html

http://blog.csdn.net/congqian1120/archive/2008/01/15/2045339.aspx

http://www.blogjava.net/hexuzhong/archive/2005/11/26/21498.html

猜你喜欢

转载自sillycat.iteye.com/blog/1058244