BeanUtils框架的简单运用

Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils
Beanutils工具包的常用类:
•BeanUtils
•PropertyUtils
•ConvertUtils.regsiter(Converter convert, Class clazz)
•自定义转换器
 
首先我们需要导入两个Jar包
 
package com.cn.gbx;



import java.util.Date;



public class Person {

	

	private String name;

	private String password;

	private int age;

	private Date date = null;

	

	public String getName() {

		return name;

	}

	public void setName(String name) {

		this.name = name;

	}

	public String getPassword() {

		return password;

	}

	public void setPassword(String password) {

		this.password = password;

	}

	public int getAge() {

		return age;

	}

	public void setAge(int age) {

		this.age = age;

	}

	public Date getDate() {

		return date;

	}

	public void setDate(Date date) {

		this.date = date;

	}

	

	@Override

	public String toString() {

		return name + " " + password + " " + age + " " + date.toLocaleString();

	} 

}

  

package com.cn.gbx;



import java.lang.reflect.InvocationTargetException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;



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 BeanUtilsDemo {

	@Test

	public void test1() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {

		Person p = new Person();

		BeanUtils.setProperty(p, "name", "gbx");

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

		

		String name = null;

		BeanUtils.getMappedProperty("name", name);

		System.out.println(name);

	}

	

	@Test

	public void test2() throws IllegalAccessException, InvocationTargetException {

		

		//为BeanUtils 注册转自己的换器     String to Date

		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 str = (String) value;

				if ("".equals(str.trim())) {

					return null;

				}

				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

				Date date = null;

				try {

					date = sf.parse(str);

				} catch (ParseException e) { 

					throw new RuntimeException(e); //异常连不可断

				}

				return date;

			}

		},

		Date.class);

		

		String name = "gbx";

		String age = "23";

		String password = "123";

		String birth = "1990-11-14";

		

		Person p = new Person();

		BeanUtils.setProperty(p, "name", name);

		BeanUtils.setProperty(p, "age", age);   // BeanUtils自动帮我们完成了转换, 自动的只支持八中基本数据类型

		BeanUtils.setProperty(p, "password", password);

		

		//非八种基本数据类型 我们需要注册自己的转换器

		BeanUtils.setProperty(p, "date", birth);

		

		System.out.println(p);

	}

	

	@Test

	public void test3() throws IllegalAccessException, InvocationTargetException {

		String name = "gbx";

		String age = "23";

		String password = "123";

		String birth = "1990-11-14";

		

		//注册系统为我们提供的转换器

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

		

		Person p = new Person();

		BeanUtils.setProperty(p, "name", name);

		BeanUtils.setProperty(p, "age", age);   // BeanUtils自动帮我们完成了转换, 自动的只支持八中基本数据类型

		BeanUtils.setProperty(p, "password", password);

		

	

		BeanUtils.setProperty(p, "date", birth);

		

		System.out.println(p);

	}

	

	//这样可以直接把表单提交 request交给对象了

	@Test

	public void test4() throws IllegalAccessException, InvocationTargetException {

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

		Person p = new Person();

		Map<String, String> info = new HashMap<String, String>();

		info.put("name", "gbx");

		info.put("age","23");

		info.put("password","123");

		info.put("date", "1990-11-14");

		

		BeanUtils.populate(p, info); // 。。。

		System.out.println(info);

	}

	



}

  

 

 

你可能感兴趣的:(BeanUtils)