Converter转换器使用

<span style="font-family:SimSun;">package com.xu.javabean;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

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;

public class Test {

	// Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景
	// 开发了一套简单、易用的API操作Bean的属性——BeanUtils,
	// 在Beanutil中可以直接进行类型的自动转换。
	// 使用beanUtils操作bean的属性(首先导入jar包,第三方开发工具包)

	@org.junit.Test
	public void test1() throws Exception {
		// BeanUtils可以填充JavaBeans属性通过反射实用方法。

		Person p = new Person();
		// void org.apache.commons.beanutils.BeanUtils
		// .setProperty(Object bean, String name, Object value)
		// throws IllegalAccessException, InvocationTargetException
		// 设置指定的属性值,进行类型转换为所需的符合目标的属性类型。
		BeanUtils.setProperty(p, "name", "zhangsan");
		System.out.println(p.getName());
	}

	@org.junit.Test
	public void test2() throws IllegalAccessException,
			InvocationTargetException {
		// 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
		// 由于是表单提交,所以都是字符串
		String name = "lisi";
		String age = "67";
		String sex = "男";
		String birthday = "1989-09-16";

		// 为了让日期赋到bean的birthday属性上,我们给BeanUtils注册一个日期转换器
		// ConvertUtils类转换为字符串的标量值来指定类的对象的实用方法,字符串数组来指定类的数组。

		// static void register(Converter converter, Class clazz)
		// 注册为指定目标类的自定义转换器,取代任何以前注册的转换器。
		ConvertUtils.register(new Converter() {
			// Object convert(Class type, Object value)
			// 将指定的输入对象转换为指定类型的输出对象。

			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 (str.trim().equals("")) {
					return null;
				}
				// SimpleDateFormat(String pattern)
				// 用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				// Date parse(String source)
				// 从给定字符串的开始解析文本,以生成一个日期。
				try {
					// 此方法会抛出异常,因为是匿名内部类实现接口,所以不能抛只能处理
					return sdf.parse(str);
				} catch (ParseException e) {
					// 如果发生此类异常,那么就让程序停止,
					// 必须加上参数e,因为异常链不能断,要让调用者看到异常信息
					throw new RuntimeException(e);
				}
			}
		}, Date.class);

		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		// p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
		// 只支持8种基本数据类型的转换
		// 其实可以用 BeanUtils.populate(bean, properties);
		BeanUtils.setProperty(p, "age", age);
		BeanUtils.setProperty(p, "sex", sex);
		BeanUtils.setProperty(p, "birthday", birthday);

		System.out.println(p.getName());
		System.out.println(p.getAge());
		System.out.println(p.getSex());
		System.out.println(p.getBirthday());
	}

	@org.junit.Test
	public void test3() throws IllegalAccessException,
			InvocationTargetException {
		// 例如:接收用户浏览器传递过来的姓名、年龄、性别等信息
		// 由于是表单提交,所以都是字符串
		String name = "lisi";
		String age = "67";
		String sex = "男";
		String birthday = "1989-09-16";

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

		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		// p对象的age属性应该接收int类型的数据,但是使用BeanUtils可以自动帮我们完成转换动作
		// 只支持8种基本数据类型的转换
		BeanUtils.setProperty(p, "age", age);
		BeanUtils.setProperty(p, "sex", sex);
		BeanUtils.setProperty(p, "birthday", birthday);

		System.out.println(p.getName());
		System.out.println(p.getAge());
		System.out.println(p.getSex());
		System.out.println(p.getBirthday());
	}

}
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值