beanUtils操作javabean的属性(常…

1、首先要新建一个Folder,命名为lib--->把common-beanutils-1.8.0.java和common-logging.java放进去--->修改这两个的Builder path--Add Path
package com.zb.cn;

import java.util.Date;

public class Person {

private String name;
private String password;
private int age;
private Date brithday;
public String getAb()
{
return 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 getBrithday() {
return brithday;
}
public void setBrithday(Date brithday) {
this.brithday = brithday;
}
}//Person.java
---------------------------------------------------------------------------------------------------------------
package com.zb.cn;

import java.lang.reflect.InvocationTargetExceptio n;
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;

//使用beanUtils操作javabean的属性(第三方)
public class Demo1 {
 
@Test
public void test1() throws Exception, Exception
{
Person p = new Person();
BeanUtils.setProperty(p, "name", "xcc");
System.out.println(p.getName());
}
@Test
public void test2() throws Exception, Exception
{
String name = "aaaa";
String password = "123";
String age = "34";
String brithday = "1995-10-06";
Person p = new Person();
BeanUtils.setProperty(p, "name", "aaaa");
BeanUtils.setProperty(p, "password", "123");
BeanUtils.setProperty(p, "age", "34");//只支持8种基本数据类型
//BeanUtils.setProperty(p, "brithday", "1995-10-06");//不是八种基本数据类型,必须使用转换器
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
}
//为了让日期赋到bean的brithday属性上,我们给beanUtils注册一个日期转换器
//自己注册转换器
@Test
public void test3() throws Exception, Exception
{
String name = "aaaa";
String password = "123";
String age = "34";
String brithday = "1995-10-06";
//注册日期转换器
ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
// TODO Auto-generated method stub
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);//这里需要导Convert的源码,就是那个source.java包
Person p = new Person();
BeanUtils.setProperty(p, "name", "aaaa");
BeanUtils.setProperty(p, "password", "123");
BeanUtils.setProperty(p, "age", "34");//只支持8种基本数据类型
BeanUtils.setProperty(p, "brithday", "1995-10-06");//不是八种基本数据类型,必须使用转换器
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBrithday());
}
//使用php的转换器
@Test
public void test4() throws Exception, Exception
{
String name = "aaaa";
String password = "123";
String age = "34";
String brithday = "1995-10-06";
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
BeanUtils.setProperty(p, "name", "aaaa");
BeanUtils.setProperty(p, "password", "123");
BeanUtils.setProperty(p, "age", "34");//只支持8种基本数据类型
BeanUtils.setProperty(p, "brithday", brithday);//不是八种基本数据类型,必须使用转换器
System.out.println(p.getName());
System.out.println(p.getPassword());
System.out.println(p.getAge());
System.out.println(p.getBrithday());
}
@Test
public void test5() throws Exception, Exception
{
Map map = new HashMap();//客户机提交的数据都使用map进行封装
map.put("name", "aaa");
map.put("password", "123");
map.put("age", "23");
map.put("brithday", "1995-10-06");

ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person bean = new Person();
BeanUtils.populate(bean, map);//填充
System.out.println(bean.getName());
System.out.println(bean.getPassword());
System.out.println(bean.getAge());
System.out.println(bean.getBrithday());
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值