请使用beanUitls框架操作bean的属性,然后会自定义转换器

来源:互联网 发布:搜狐媒体大厦淘宝地址 编辑:程序博客网 时间:2024/06/07 20:46

import java.lang.reflect.InvocationTargetException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

public class Demo1 {

@Test
public void test1() throws IllegalAccessException, InvocationTargetException{
Person p=new Person();
BeanUtils.setProperty(p,"name","张三");
System.out.println(p.getName());

}
@Test
public void test2() throws IllegalAccessException, InvocationTargetException{
//自定义转换器
ConvertUtils.register(new Converter(){

@Override
public Object convert(Class type, Object value) {

if(value==null){
return null;
}
if(!(value instanceof String)){
throw new ConversionException("只能转String数据");
}
String s=(String)value;

if(s.trim().equals("")){
return null;
}

SimpleDateFormat sdf=new SimpleDateFormat("2012-9-18");
try {
Date d=sdf.parse(s);
return d;
} catch (ParseException e) {
// TODO Auto-generated catch block
throw new ConversionException("转换错误");
}
}
}, Date.class);



String name="刘毅杰";
String age="20";
String birthday="19930226";

Person p=new Person();
BeanUtils.setProperty(p,"name",name);
BeanUtils.setProperty(p,"age",age);
BeanUtils.setProperty(p,"birthday",birthday);

System.out.println(p.getName()+"..."+p.getAge()+"...");


}

@Test
public void test3() throws IllegalAccessException, InvocationTargetException{

ConvertUtils.register(new DateLocaleConverter(), Date.class);

Person p=new Person();
BeanUtils.setProperty(p,"birthday","  ");

}


}





package com.hbsi.introspector;

public class Person {

private String name;
private int age;
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 String getAb(){
return null;
}



}

package com.hbsi.introspector;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

public class Demo1 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

}

@Test
public void test1() throws Exception{
BeanInfo bi=Introspector.getBeanInfo(Person.class,Object.class);

PropertyDescriptor[] pds=bi.getPropertyDescriptors();

for(PropertyDescriptor pd:pds){
String name=pd.getName();
System.out.println(name);
}

}
@Test
public void test2() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{

Person p=new Person();

BeanInfo bi=Introspector.getBeanInfo(Person.class);

PropertyDescriptor[] pds=bi.getPropertyDescriptors();

for(PropertyDescriptor pd:pds){
String name=pd.getName();
if(name.equals("name")){
Method m=pd.getWriteMethod();
m.invoke(p, "abc");
}
}
System.out.println(p.getName());
}
@Test
public void Test3() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{

Person p=new Person();

PropertyDescriptor pd=new PropertyDescriptor("name", p.getClass());

Method m=pd.getWriteMethod();

m.invoke(p,"ffff");

System.out.println(p.getName());

}

@Test
public void Test4() throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException{
Person p=new Person();
p.setName("abcdef");

PropertyDescriptor pd=new PropertyDescriptor("name",p.getClass());

Method m=pd.getReadMethod();

String str=(String)m.invoke(p, null);
System.out.println(str);
}




}