Java-使用beanUtils操作javabean(学习笔记)

Apache BeanUtils是一个用于操作JavaBean属性的框架,本文介绍如何将其导入工程并使用。首先,在工程下新建lib文件夹,导入common-beanutils和common-logging jar包。然后,通过build Path将其加入构建路径。示例中展示了使用BeanUtils为Person对象属性赋值以及处理表单数据类型的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作bean的属性在开发中非常常见,apache开发了一个框架专门用来操作bean属性以简化工作,这个框架就是我们今天要学习的beanUtils。

由于这个框架不是sun公司的,所以我们需要将beanUtiles相关开发包导入到工程中,也就是我们平时所说的导入第三方开发包。

步骤:

①在工程下鼠标右键新建一个文件夹(new folder),通常命名为lib;

②拷贝beanUtils开发包到文件夹中(如果网上下载的beanUtlis开发包包含很多jar包,通常导入命名比较短的那个,conmon-beanutils jar);

③beanUtils工作时还需要一个日志记录器支持,所以还需要复制一个conmon-logging jar包到文件夹中;

④选中两个jar包鼠标右键,选择build Path->add to build path,jar包就加到了构建路径里面去了,这样就可以使用jar包里面的API了。

实例:

(1)给一个人的属性赋值:

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Test;
//使用beanUtiles操作bean的属性
public class Demo1 {
	@Test
	public void test1() throws Exception, InvocationTargetException{
		Person p = new Person();
		BeanUtils.setProperty(p, "name", "love");//名字命名为love
		System.out.println(p.getName());
		
	}
}
说明:与上一篇内省的使用相比更简洁易书写。

注:Person类在Java-内省一篇中有,此处不粘贴上去

(2)在表单中填写数据(String类型),在服务器端beanUtils会对数据类型进行转换

public void test2() throws Exception, InvocationTargetException{
		String name = "love";
		String password = "520";
		String age = "520";//用户填写在表单上的信息都是String类型
	
		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);//beanUtils自动转换数据类型,String转换为int,但是转换只支持8种数据类型

		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
            }


如果在Person类属性中添加private date birthday的属性,那么用户在填写表单时还需要填写出生日期,是String类型的,服务器端就无法完成类型转换,出现报错:
java.lang.IllegalArgumentException: Cannot invoke beanutils.Person.setBirthday - argument type mismatch。

(3)当出现上面的情况时,我们需要给beanUtils注册一个转换器:beanUtils提供了一个ConvertUtils工具类,该工具类提供了register方法(注册)
public void test3() throws Exception, InvocationTargetException{
		String name = "love";
		String password = "520";
		String age = "520";
		String birthday = "1993-10-01";
		//为了让日期赋到bean的birthday属性上,我们给beanUtils注册一个日期转换器
		/*ConvertUtils.register(new Converter(), Date.class);
		Converter是接口,不能直接new,因为含有未实现的抽象方法,所以实现他的方法就可以new了*/
		ConvertUtils.register((type, 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 df = new SimpleDateFormat("yyyy-MM-dd");
			try{
				return df.parse(str);}
			catch(ParseException e){
				throw new RuntimeException(e);//异常链不能断
			}
		}, Date.class);
		Person p = new Person();
		BeanUtils.setProperty(p, "name", name);
		BeanUtils.setProperty(p, "password", password);
		BeanUtils.setProperty(p, "age", age);//转换只支持8种数据类型
		BeanUtils.setProperty(p, "birthday", birthday);//转换只支持8种数据类型

		System.out.println(p.getName());
		System.out.println(p.getPassword());
		System.out.println(p.getAge());
		/*Date data = p.getBirthday();
		System.out.println(data.toString());*/
		System.out.println(p.getBirthday());
	}
(4)客户端提交的的数据用map集合进行封装//request请求
public void test5() throws Exception{
		Map map = new HashMap();
		map.put("name", "aaa");
		map.put("password", "520");
		map.put("age", "520");
		map.put("birthday", "1994-04-03");
		
		ConvertUtils.register(new DateLocaleConverter(),Date.class);
		Person bean = new Person();
		BeanUtils.populate(bean, map);//用map集合中的值填充bean的属性
		
		System.out.println(bean.getName());
		System.out.println(bean.getPassword());
		System.out.println(bean.getAge());
		System.out.println(bean.getBirthday());
		
	}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值