日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

PropertyUtils

發布時間:2024/8/1 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。