当前位置: 首页 > java >正文

PropertyUtils的使用

1、定义一个二个bean类

(1)  第一个

 

Java代码   收藏代码
  1. package com.sunrex.demo02;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class Address {  
  6.     private String email;  
  7.     private List<String> telephone;  
  8.     public String getEmail() {  
  9.         return email;  
  10.     }  
  11.     public void setEmail(String email) {  
  12.         this.email = email;  
  13.     }  
  14.     public List<String> getTelephone() {  
  15.         return telephone;  
  16.     }  
  17.     public void setTelephone(List<String> telephone) {  
  18.         this.telephone = telephone;  
  19.     }  
  20. }  

(2)第二个

Java代码   收藏代码
  1. package com.sunrex.demo02;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. public class Person {  
  9.     private String username;  
  10.     private int age;  
  11.     private float stature;//身高  
  12.     private boolean sex;//性别  
  13.     @SuppressWarnings("unchecked")  
  14.     private List list = new ArrayList();  
  15.     private String[] friendsNames;  
  16.     private Map<String, String> maps = new HashMap<String, String>();  
  17.     private Address address;  
  18.       
  19.     public String getUsername() {  
  20.         return username;  
  21.     }  
  22.     public void setUsername(String username) {  
  23.         this.username = username;  
  24.     }  
  25.     public int getAge() {  
  26.         return age;  
  27.     }  
  28.     public void setAge(int age) {  
  29.         this.age = age;  
  30.     }  
  31.     public float getStature() {  
  32.         return stature;  
  33.     }  
  34.     public void setStature(float stature) {  
  35.         this.stature = stature;  
  36.     }  
  37.     public boolean isSex() {  
  38.         return sex;  
  39.     }  
  40.     public void setSex(boolean sex) {  
  41.         this.sex = sex;  
  42.     }  
  43.     @SuppressWarnings("unchecked")  
  44.     public List getList() {  
  45.         return list;  
  46.     }  
  47.     @SuppressWarnings("unchecked")  
  48.     public void setList(List list) {  
  49.         this.list = list;  
  50.     }  
  51.     public Address getAddress() {  
  52.         return address;  
  53.     }  
  54.     public void setAddress(Address address) {  
  55.         this.address = address;  
  56.     }  
  57.     public Map<String, String> getMaps() {  
  58.         return maps;  
  59.     }  
  60.     public void setMaps(Map<String, String> maps) {  
  61.         this.maps = maps;  
  62.     }  
  63.     public String[] getFriendsNames() {  
  64.         return friendsNames;  
  65.     }  
  66.     public void setFriendsNames(String[] friendsNames) {  
  67.         this.friendsNames = friendsNames;  
  68.     }  
  69. }  
 

3、测试类

Java代码   收藏代码
  1. package com.sunrex.demo02;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.apache.commons.beanutils.PropertyUtils;  
  9.   
  10. public class PersonTest {  
  11.     @SuppressWarnings("unchecked")  
  12.     public static void main(String[] args) {  
  13.         Person person = new Person();  
  14.         try {  
  15.             //simple property  
  16.             PropertyUtils.setSimpleProperty(person, "username""李四");  
  17.             PropertyUtils.setSimpleProperty(person, "age"22);  
  18.             PropertyUtils.setSimpleProperty(person, "stature"173.5f);  
  19.             PropertyUtils.setSimpleProperty(person, "sex"new Boolean(false));  
  20.               
  21.             //index property  
  22.             //List  
  23.             List list = new ArrayList();  
  24.             list.add("list value 0");  
  25.             list.add("list value 1");  
  26.             String listValue2 = "new list value 1";  
  27.             PropertyUtils.setSimpleProperty(person, "list", list);  
  28.             //将list设置到person之后,可以对里面的值进行修改  
  29.             PropertyUtils.setIndexedProperty(person, "list[1]", listValue2);  
  30.   
  31.             //数组  
  32.             String[] str = {"张三""王五""赵钱"};  
  33.             person.setFriendsNames(str);  
  34.             PropertyUtils.setIndexedProperty(person, "friendsNames[2]""new赵钱");  
  35.               
  36.             //Map  
  37.             Map<String, String> map = new HashMap<String, String>();  
  38.             map.put("key1""vlaue1");  
  39.             map.put("key2""vlaue2");  
  40.             map.put("key3""vlaue3");  
  41.             person.setMaps(map);  
  42.             PropertyUtils.setMappedProperty(person, "maps""key1""new value1");  
  43.             PropertyUtils.setMappedProperty(person, "maps(key2)""maps(key2) value");  
  44.               
  45.             //nest property  
  46.             Address address = new Address();  
  47.             address.setEmail("jhlishero@163.com");  
  48.             List<String> telephoneList = new ArrayList<String>();  
  49.             telephoneList.add("12345678911");  
  50.             telephoneList.add("92345678911");  
  51.             address.setTelephone(telephoneList);  
  52.             person.setAddress(address);  
  53.             PropertyUtils.setNestedProperty(person, "address.telephone[1]""nest 11111111");  
  54.             PropertyUtils.setNestedProperty(person, "address.email""new_jhlishero@163.com");  
  55.               
  56.             System.out.println(PropertyUtils.getSimpleProperty(person, "username"));  
  57.             System.out.println(PropertyUtils.getSimpleProperty(person, "age"));  
  58.             System.out.println(PropertyUtils.getSimpleProperty(person, "stature"));  
  59.             System.out.println(PropertyUtils.getSimpleProperty(person, "sex"));  
  60.             System.out.println(PropertyUtils.getSimpleProperty(person, "list"));  
  61.             //list  
  62.             System.err.println(PropertyUtils.getIndexedProperty(person, "list[0]"));  
  63.             System.err.println(PropertyUtils.getIndexedProperty(person, "list"1));  
  64.             //数组  
  65.             System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[0]"));  
  66.             System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames"1));  
  67.             System.out.println(PropertyUtils.getIndexedProperty(person, "friendsNames[2]"));  
  68.               
  69.             //Map  
  70.             System.err.println(PropertyUtils.getMappedProperty(person, "maps(key1)"));  
  71.             System.err.println(PropertyUtils.getMappedProperty(person, "maps""key2"));  
  72.             System.err.println(PropertyUtils.getMappedProperty(person, "maps(key3)"));  
  73.               
  74.             //nest--嵌套输出   
  75.             System.out.println(PropertyUtils.getNestedProperty(person, "address.email"));  
  76.             System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[0]"));  
  77.             System.out.println(PropertyUtils.getNestedProperty(person, "address.telephone[1]"));  
  78.               
  79.             //也可以使用如下方法获取值  
  80.             System.out.println(PropertyUtils.getProperty(person, "address.telephone[1]"));  
  81.         } catch (Exception e) {  
  82.             // TODO Auto-generated catch block  
  83.             e.printStackTrace();  
  84.         }  
  85.     }  
  86. }  

 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

http://www.xdnf.cn/news/11242.html

相关文章:

  • Elasticsearch 技术分析(九):全文搜索引擎Elasticsearch,这篇文章给讲透了!
  • 粉丝答疑:如何实现高效免杀?
  • 智能聊天助手:数据分析的新兴英雄
  • 千兆光模块和万兆光模块的安装和维护指南
  • 使用U盘安装Fedora14 32bit操作系统(参考自www.osyunwei.com)
  • 如何给网页和代码做HTML加密?
  • 国家授时中心服务器IP地址
  • mega linux教程,LINUX 安装MegaRAID Storage Manager (MSM)安装使用教程
  • wordpress主题_2020年使用的15个顶级WordPress主题
  • 网络安全应急响应----4、DDoS攻击应急响应
  • 使用Teleport Pro离线下载网页所有内容
  • Ubuntu7.04使用中遇到的问题及从网上搜集的解决办法(截止2007-11-3日) 收藏
  • 协方差矩阵与相关系数矩阵
  • 联想y430完全拆机图解_y430p拆机详细步骤及如何安装mSATA接口的固态硬盘?
  • 磁盘碎片原理分析
  • 同一网络(局域网)下远程控制另一台电脑
  • [免费源码]个人引导页毛玻璃页面html源码
  • java操作JSON
  • 蠕虫病毒代码(纯属参考,请勿用于非法事件)
  • 游戏发展演变:何谓第三代网游?
  • 蜀门linux服务端架设,蜀门私服常用修改配置大全
  • 【干货】零基础30分钟让你拥有一个完整属于自己的短视频APP系统
  • 文心一言APP国区可下载!免费体验120+玩法,PPT大纲Excel公式一键生成
  • 各国搜索引擎大全
  • 使用Python截取nc文件数据保存到CSV文件
  • 忘记Gmail谷歌账号密码或者密码错误怎么办?用这种方法轻松搞定
  • 2024年Go最全PHP从基础到高级详细教程(完整版)_php高级教程(1),重要概念一网打尽
  • Linux学习记录--内核|内核模块编译
  • VMware 8安装Mac OS X 10.7 Lion
  • css3帮助文档,CSS3 最新版参考手册