org.apache.commons.beanutils.PropertyUtilsBean

bean和beanform的对应的关系,同名的类型一致
public Integer x;
	public String y;
	public Date z;
	public boolean k;

	public Integer getX() {
		return x;
	}

	public void setX(Integer x) {
		this.x = x;
	}

	public String getY() {
		return y;
	}

	public void setY(String y) {
		this.y = y;
	}

	public Date getZ() {
		return z;
	}

	public void setZ(Date z) {
		this.z = z;
	}

	public boolean isK() {
		return k;
	}

	public void setK(boolean k) {
		this.k = k;
	}

public static void main(String[] args) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		TestPropertyUtilsBean testPropertyUtilsBean = new TestPropertyUtilsBean();
		Bean bean = new Bean();
		
		//判断是否public方法,getWriteMethod,getReadMethod方法调用MethodUtils
		System.out.println("isReadable:");
		System.out.println(testPropertyUtilsBean.isReadable(bean, "x"));
		System.out.println(testPropertyUtilsBean.isReadable(bean, "y"));
		System.out.println(testPropertyUtilsBean.isReadable(bean, "z"));
		System.out.println(testPropertyUtilsBean.isReadable(bean, "k"));
		System.out.println("-----------------------------------");

		System.out.println("isWriteable:");
		System.out.println(testPropertyUtilsBean.isWriteable(bean, "x"));
		System.out.println(testPropertyUtilsBean.isWriteable(bean, "y"));
		System.out.println(testPropertyUtilsBean.isWriteable(bean, "z"));
		System.out.println(testPropertyUtilsBean.isWriteable(bean, "k"));
		System.out.println("-----------------------------------");
		
		//调用getPropertyDescriptors方法,得到name对应的PropertyDescriptor对象
		System.out.println("setSimpleProperty and getSimpleProperty:");
		testPropertyUtilsBean.setSimpleProperty(bean, "x", 1);
		System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "x"));
		testPropertyUtilsBean.setSimpleProperty(bean, "y", "y");
		System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "y"));
		testPropertyUtilsBean.setSimpleProperty(bean, "z", new Date());
		System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "z"));
		testPropertyUtilsBean.setSimpleProperty(bean, "k", true);
		System.out.println(testPropertyUtilsBean.getSimpleProperty(bean, "k"));
		System.out.println("-----------------------------------");
		
		//把bean的值转化为map输出
		System.out.println("describe:");
		System.out.println(testPropertyUtilsBean.describe(bean));
		System.out.println("-----------------------------------");
		
		Beanform form = new Beanform();
		testPropertyUtilsBean.copyProperties(form, bean);
		System.out.println(testPropertyUtilsBean.describe(form));
		
	}


测试类是继承PropertyUtilsBean,因为测试的有些方法是protected

控制台:

isReadable:
true
true
true
true
-----------------------------------
isWriteable:
true
true
true
true
-----------------------------------
setSimpleProperty and getSimpleProperty:
1
y
Sun May 20 15:31:49 CST 2012
true
-----------------------------------
describe:
{class=class Bean, z=Sun May 20 15:31:49 CST 2012, k=true, y=y, x=1}
-----------------------------------
{class=class Beanform, z=Sun May 20 15:31:49 CST 2012, k=true, y=y, x=1}

注意,PropertyUtilsBeancopy时,类型要一致,不然会报错

其他方法,没做测试,只是看了下代码
getProperty(Object bean, String name)
getNestedProperty(bean, name)  取bean相对应的name的值
getPropertyOfMapBean(Map bean, String propertyName),取bean的对应key的值
getMappedProperty(Object bean, String name)-->getMappedProperty(Object bean,String name, String key)先取bean的map,再通过map的key取值
getIndexedProperty(Object bean, String name)-->getIndexedProperty(Object bean,String name, int index)先取bean的list,再通过索引取值



setProperty(Object bean, String name, Object value)
setNestedProperty(Object bean,String name, Object value)给bean的name属性赋value值
setPropertyOfMapBean(Map bean, String propertyName, Object value)给bean的对应key的设值
setIndexedProperty(Object bean, String name,Object value)-->setIndexedProperty(Object bean, String name,int index, Object value)先取bean的map,再通过map的key设值
setMappedProperty(Object bean, String name,Object value)-->setMappedProperty(Object bean, String name,String key, Object value)先取bean的list,再通过索引设值



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值