beanUtils操作javabean的属性(常…

来源:互联网 发布:java怎么判断奇数偶数 编辑:程序博客网 时间:2024/05/21 10:55
1、首先要新建一个Folder,命名为lib--->把common-beanutils-1.8.0.java和common-logging.java放进去--->修改这两个的Builderpath--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.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;

importorg.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, Objectvalue) {
// TODO Auto-generated methodstub
if(value==null)//数据先检查再使用
{
return null;
}
if(!(value instanceofString))
{
throw newConversionException("只支持String类型的转换!");
}
String str = (String)value;
if(str.trim().equals(""))
{
return null;
}
SimpleDateFormat df = newSimpleDateFormat("yyyy-MM-dd");
try {
return df.parse(str);
} catch (ParseException e){
throw newRuntimeException(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(newDateLocaleConverter(), 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());
}
}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 吃了芥末胃疼怎么办 手撕鱿鱼咸了怎么办 孕妇吃了点芥末怎么办 葡萄酒上面有一层白霉怎么办 手机一不小心把视频删了怎么办 柑橘7月份果实小怎么办 鹦鹉鱼身上有小白点怎么办 鹦鹉鱼身上烂了怎么办 红鹦鹉鱼变黑了怎么办 唱美声总夹嗓子怎么办 发财树树干出褶怎么办 月季花夏天换盆出现黄叶怎么办 刚种的月季枯了怎么办 新买的月季黄叶了怎么办 月季花叶孑轰发黄怎么办? 新种的月季苗弱怎么办? 月季换盆后浇透水叶子黄了怎么办 肉肉移栽后浇透水怎么办 月季花扦插的没长根发芽了怎么办 君子兰发的小苗怎么办 蔷薇光长枝条不开花怎么办 牡丹发芽又干了怎么办 擦皮炎平后皮肤变黑怎么办 误喝发霉的咖啡渣怎么办 狗吃了速溶咖啡怎么办 咖啡机放豆的地方进水怎么办 干吃咖啡粉上瘾怎么办 去良友花艺住宿怎么办 充气娃娃放了气怎么办 煮杜鹃根没有锅怎么办 淘宝店卖鲜花被买家拒收货怎么办 执业医师电子注册忘记密码怎么办 怀孕吃了油炸的怎么办 百合长得太高怎么办 百合的杆没了怎么办 百合花长得太细怎么办 沙漠玫瑰的花苞打不开怎么办 鲜切花 较小的花苞怎么办 大棚玫瑰苗水大涝的不长怎么办 鲜花买回来蔫了怎么办 喝玫瑰醋上火了怎么办