java之用beanUtils框架来操作bean
来源:互联网 发布:windows 访问samba 编辑:程序博客网 时间:2024/06/06 02:31
下面是几段beanUtils框架来操作bean测试代码,其中bean是Person,其中这个框架是阿帕奇公司写的第三方,我们导入了相应的jar包,这几段代码体现出了手动写转换器的必要性,从理论上说明了问题
但是从@Test3之后一直报错(相同的错我没有找出来),也希望高手大牛路过的时候能帮忙看看原因所在,我不胜感激!!
package beanstr;
import java.lang.reflect.InvocationTargetException;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
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;
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 Introspector {
private static final Object Date = null;
private static final Object Date = null;
// 使用beanUntils框架来操作bean(第三方),也是用来封装数据的
// 使用beanUntils框架来操作bean(第三方)当把beanUntils导入的时候还需要它运行的api也就是commons
// logging来支持
@Test//操作单个值
public void test1() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
BeanUtils.copyProperty(p, "name", "hyl");
// 将某个bean的哪个值修改为什么值
System.out.println(p.getName());
}
// 使用beanUntils框架来操作bean(第三方)当把beanUntils导入的时候还需要它运行的api也就是commons
// logging来支持
@Test//操作单个值
public void test1() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();
BeanUtils.copyProperty(p, "name", "hyl");
// 将某个bean的哪个值修改为什么值
System.out.println(p.getName());
}
@Test//操作三个值
public void test2() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();// 将用户提交的数据都封装起来
String name = "hyl";
String age = "12";
String password = "123132";
public void test2() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();// 将用户提交的数据都封装起来
String name = "hyl";
String age = "12";
String password = "123132";
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
}
System.out.println(p.getAge());
System.out.println(p.getPassword());
}
@Test//重点是手动写了一个转换器,用来将字符串转换成Date类型
public void test3() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();// 将用户提交的数据都封装起来
String name = "hyl";
String age = "12";
String password = "123132";
String birthday = "1995-02-15";
public void test3() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();// 将用户提交的数据都封装起来
String name = "hyl";
String age = "12";
String password = "123132";
String birthday = "1995-02-15";
// 为了让日期赋值到bean的birthday的属性上,我们给beanUntils注册一个日期转换器(这已经不再是sun公司的jar包了)
ConvertUtils.register(new Converter() {
ConvertUtils.register(new Converter() {
public Object convert(Class type, Object value) {
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("请输入字符串");
}
String s = (String) value;
if (s.trim().equals("")) {// trim()是去掉所有空格
return null;
}
if (value == null) {
return null;
}
if (!(value instanceof String)) {
throw new ConversionException("请输入字符串");
}
String s = (String) value;
if (s.trim().equals("")) {// trim()是去掉所有空格
return null;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {// 由于不能抛出比父类更多的异常只能try
return df.parse(s);
} catch (ParseException e) {
throw new RuntimeException(e); // 若有异常抛向上级,从而使程序停止执行;把异常信息封装进去保持异常链的连续性把导致异常的的异常封装起来
}
try {// 由于不能抛出比父类更多的异常只能try
return df.parse(s);
} catch (ParseException e) {
throw new RuntimeException(e); // 若有异常抛向上级,从而使程序停止执行;把异常信息封装进去保持异常链的连续性把导致异常的的异常封装起来
}
}
}, Date.class);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "birthday", birthday);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "birthday", birthday);// 而自动转换转换基本的8种类型
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBiethday());// 打印出来是date.toString的特定格式
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBiethday());// 打印出来是date.toString的特定格式
}
@Test//使用了阿帕奇公司的api
public void test4() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();// 将用户提交的数据都封装起来
String name = "hyl";
String age = "12";
String password = "123132";
String birthday = "1995-02-15";
public void test4() throws IllegalAccessException,
InvocationTargetException {
Person p = new Person();// 将用户提交的数据都封装起来
String name = "hyl";
String age = "12";
String password = "123132";
String birthday = "1995-02-15";
// 使用阿帕奇公司写好的转换器
ConvertUtils.register(new DateLocaleConverter(), (Class<?>) Date);
ConvertUtils.register(new DateLocaleConverter(), (Class<?>) Date);
BeanUtils.setProperty(p, "name", name);
BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "birthday", birthday);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "age", age);// beanUntils 将用户提交的整数自动转换成了String
BeanUtils.setProperty(p, "password", password);// 而自动转换转换基本的8种类型
BeanUtils.setProperty(p, "birthday", birthday);// 而自动转换转换基本的8种类型
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBiethday().toLocaleString());// 打印出来是date.toString的特定格式
}
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBiethday().toLocaleString());// 打印出来是date.toString的特定格式
}
@Test//用map的集合填充bean的属性
public void test5() throws IllegalAccessException,
InvocationTargetException {
Map map = new HashMap();
map.put("name", "hyl");
map.put("password", "123");
map.put("age", "32");
map.put("brithday", "1995-02-15");
public void test5() throws IllegalAccessException,
InvocationTargetException {
Map map = new HashMap();
map.put("name", "hyl");
map.put("password", "123");
map.put("age", "32");
map.put("brithday", "1995-02-15");
ConvertUtils.register(new DateLocaleConverter(), Date.class);
Person p = new Person();
BeanUtils.populate(p, map);//将map填充到person中
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBiethday().toLocaleString());
}
Person p = new Person();
BeanUtils.populate(p, map);//将map填充到person中
System.out.println(p.getName());
System.out.println(p.getAge());
System.out.println(p.getPassword());
System.out.println(p.getBiethday().toLocaleString());
}
}
1 0
- java之用beanUtils框架来操作bean
- 学会使用BeanUtils 来操作bean属性
- JavaBean操作技术之二:BeanUtils框架
- BeanUtils操作Bean的属性
- BeanUtils操作bean的属性
- 用BeanUtils框架操作类的属性
- 使用beanutils操作bean的属性
- 使用BeanUtils操作Bean的各种属性
- java基础加强之通过BeanUtils工具包操作Java
- Java高新技术之内省操作(BeanUtils、PropertyUtils和ConverUtils.register)
- 用BeanUtils类实现bean的拷贝
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
- ccnu_2016_暑期模拟赛(1)
- 安卓ViewPager+RadioGroup实现多组图片滑动展示(每组图片不定量)
- 红黑树
- Codevs 2460 树的统计 [树链剖分]
- 算法等基本概念
- java之用beanUtils框架来操作bean
- 【笔记】Java Mail发送邮件(使用QQ邮箱)
- 杭电ACM 盐水的故事
- UGUI 学习02
- 关于异常 libc: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x64616570 in tid 17260 (FinalizerDaemon)
- ACM457
- 今天在ubuntu上装chromium,因无法加载flashplayer,我卸载了,卸载之后重新安装,就启动不了
- mytest
- tjut 4300