Commons BeanUtils
[Java] 纯文本查看 复制代码 package com.jeestudy.bean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
public class PropertyUtilsTest {
public static void main(String[] args) {
Person person = new Person();
try {
// simple property
PropertyUtils.setSimpleProperty(person, "name", "李四");
PropertyUtils.setSimpleProperty(person, "age", 22);
PropertyUtils.setSimpleProperty(person, "stature", 173.5f);
PropertyUtils.setSimpleProperty(person, "sex", new Boolean(false));
// index property
// List
List<String> list = new ArrayList<String>();
list.add("0");
list.add("1");
String listValue2 = "2";
PropertyUtils.setSimpleProperty(person, "list", list);
// 修改list的值
PropertyUtils.setIndexedProperty(person, "list[1]", listValue2);
// 数组
String[] friends = { "张三", "李四", "王五" };
person.setFriends(friends);
PropertyUtils.setIndexedProperty(person, "friends[2]", "张李五");
// Map
Map<String, String> map = new HashMap<String, String>();
map.put("k1", "v1");
map.put("k2", "v2");
map.put("k3", "v3");
person.setMap(map);
PropertyUtils.setMappedProperty(person, "map", "k1", "v11");
PropertyUtils.setMappedProperty(person, "map(k3)", "v33");
// nest property
Address address = new Address();
address.setEmail("www.jeestudy.com");
List<String> telephoneList = new ArrayList<String>();
telephoneList.add("123");
telephoneList.add("456");
address.setTelephone(telephoneList);
person.setAddress(address);
PropertyUtils.setNestedProperty(person, "address.telephone[1]", "010-123");
PropertyUtils.setNestedProperty(person, "address.email", "www@jeestudy.com");
System.out.println(PropertyUtils.getSimpleProperty(person, "name"));
System.out.println(PropertyUtils.getSimpleProperty(person, "age"));
System.out.println(PropertyUtils.getSimpleProperty(person, "stature"));
System.out.println(PropertyUtils.getSimpleProperty(person, "sex"));
System.out.println(PropertyUtils.getSimpleProperty(person, "list"));
// list
System.err.println(PropertyUtils.getIndexedProperty(person, "list[0]"));
System.err.println(PropertyUtils.getIndexedProperty(person, "list", 1));
// 数组
System.out.println(PropertyUtils.getIndexedProperty(person, "friends[0]"));
System.out.println(PropertyUtils.getIndexedProperty(person, "friends", 1));
System.out.println(PropertyUtils.getIndexedProperty(person, "friends[2]"));
// Map
System.err.println(PropertyUtils.getMappedProperty(person, "map(k1)"));
System.err.println(PropertyUtils.getMappedProperty(person, "map", "k2"));
System.err.println(PropertyUtils.getMappedProperty(person, "map(k3)"));
// nest--嵌套输出
System.out.println(PropertyUtils.getNestedProperty(person, "address.email"));
System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[0]"));
System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[1]"));
// 也可以使用如下方法获取值
System.out.println(PropertyUtils.getProperty(person, "address.telephone[1]"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
[Java] 纯文本查看 复制代码 package com.jeestudy.bean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Person {
private String name;
private int age;
private float stature;
private boolean sex;
private List<String> list = new ArrayList<String>();
private String[] friends;
private Map<String, String> map = new HashMap<String, String>();
private Address address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getStature() {
return stature;
}
public void setStature(float stature) {
this.stature = stature;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public String[] getFriends() {
return friends;
}
public void setFriends(String[] friends) {
this.friends = friends;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
[Java] 纯文本查看 复制代码 package com.jeestudy.bean;
import java.util.List;
public class Address {
private String email;
private List<String> telephone;
public Address() {
}
public Address(String email, List<String> telephone) {
this.email = email;
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public List<String> getTelephone() {
return telephone;
}
public void setTelephone(List<String> telephone) {
this.telephone = telephone;
}
}
|