设为首页收藏本站 JeeStudy企业开发官网①

JEE Study|JAVA EE|企业级开发学习网

 找回密码
 立即加入

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 5612|回复: 0
打印 上一主题 下一主题

PropertyUtils的使用

[复制链接]

219

主题

221

帖子

1418

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1418

最佳新人活跃会员热心会员推广达人宣传达人灌水之王突出贡献优秀版主荣誉管理论坛元老

跳转到指定楼层
楼主
发表于 2016-11-5 22:25:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
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;
	}

}




知识改变命运!

JAVA EE 学习     JAVA EE 资料
JEE Study:企业级开发学习网!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

本版积分规则

QQ|Archiver|手机版|小黑屋|JEE Study ( 京ICP备16036936   JeeStudy企业开发官网①

GMT+8, 2024-5-4 21:34 , Processed in 0.202745 second(s), 26 queries .

Powered by JeeStudy!

© 2008-2020 JEE Study 企业级开发学习网

快速回复 返回顶部 返回列表