PropertyUtils
生活随笔
收集整理的這篇文章主要介紹了
PropertyUtils
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、定義一個二個bean類
(1)? 第一個
package com.sunrex.demo02;import java.util.List;public class Address {private String email;private List<String> 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;}}?(2)第二個
?
package com.sunrex.demo02;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;public class Person {private String username;private int age;private float stature;//身高private boolean sex;//性別@SuppressWarnings("unchecked")private List list = new ArrayList();private String[] friendsNames;private Map<String, String> maps = new HashMap<String, String>();private Address address;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}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;}@SuppressWarnings("unchecked")public List getList() {return list;}@SuppressWarnings("unchecked")public void setList(List list) {this.list = list;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public Map<String, String> getMaps() {return maps;}public void setMaps(Map<String, String> maps) {this.maps = maps;}public String[] getFriendsNames() {return friendsNames;}public void setFriendsNames(String[] friendsNames) {this.friendsNames = friendsNames;} }?
?
3、測試類
package com.sunrex.demo02;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import org.apache.commons.beanutils.PropertyUtils;public class PersonTest {@SuppressWarnings("unchecked")public static void main(String[] args) {Person person = new Person();try {//simple propertyPropertyUtils.setSimpleProperty(person, "username", "李四");PropertyUtils.setSimpleProperty(person, "age", 22);PropertyUtils.setSimpleProperty(person, "stature", 173.5f);PropertyUtils.setSimpleProperty(person, "sex", new Boolean(false));//index property//ListList list = new ArrayList();list.add("list value 0");list.add("list value 1");String listValue2 = "new list value 1";PropertyUtils.setSimpleProperty(person, "list", list);//將list設置到person之后,可以對里面的值進行修改PropertyUtils.setIndexedProperty(person, "list[1]", listValue2);//數組String[] str = {"張三", "王五", "趙錢"};person.setFriendsNames(str);PropertyUtils.setIndexedProperty(person, "friendsNames[2]", "new趙錢");//MapMap<String, String> map = new HashMap<String, String>();map.put("key1", "vlaue1");map.put("key2", "vlaue2");map.put("key3", "vlaue3");person.setMaps(map);PropertyUtils.setMappedProperty(person, "maps", "key1", "new value1");PropertyUtils.setMappedProperty(person, "maps(key2)", "maps(key2) value");//nest propertyAddress address = new Address();address.setEmail("jhlishero@163.com");List<String> telephoneList = new ArrayList<String>();telephoneList.add("12345678911");telephoneList.add("92345678911");address.setTelephone(telephoneList);person.setAddress(address);PropertyUtils.setNestedProperty(person, "address.telephone[1]", "nest 11111111");PropertyUtils.setNestedProperty(person, "address.email", "new_jhlishero@163.com");System.out.println(PropertyUtils.getSimpleProperty(person, "username"));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"));//listSystem.err.println(PropertyUtils.getIndexedProperty(person, "list[0]"));System.err.println(PropertyUtils.getIndexedProperty(person, "list", 1));//數組System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[0]"));System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames", 1));System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[2]"));//MapSystem.err.println(PropertyUtils.getMappedProperty(person, "maps(key1)"));System.err.println(PropertyUtils.getMappedProperty(person, "maps", "key2"));System.err.println(PropertyUtils.getMappedProperty(person, "maps(key3)"));//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) {// TODO Auto-generated catch blocke.printStackTrace();}} }?
?
4、輸出結果
李四
22
173.5
false
[list value 0, new list value 1]
list value 0
new list value 1
張三
王五
new趙錢
new value1
new_jhlishero@163.com
12345678911
nest 11111111
nest 11111111
maps(key2) value
vlaue3
總結
以上是生活随笔為你收集整理的PropertyUtils的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拟合函数、曲线拟合有这个网站足够了
- 下一篇: java的property_「prope