BeanUtils 是用于简化JavaBean封装数据的操作。它可以给JavaBean封装一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中
运用BeanUtils工具类
1.定义水果类,属性为品种(kind),价钱(price);
2.定义测试类Test,定义Map集合,将name和age赋值,存入到Map集合,然后利用BeanUtils将数据封装到水果类对象中
3.将属性值取出来
public class Fruit {
private String kind;
private double price;
省略set,get方法
To String 方法
}
主方法
public class Test {
public static void main(String[] args) throws Exception, InvocationTargetException {
//创建对象
Fruit f = new Fruit();
//定义集合,
//创建集合
Map map = new HashMap();
//添加
map.put("kind", "苹果");
map.put("price", "5.2");
BeanUtils.populate(f, map);
System.out.println(f);
}
}
运用BeanUtils工具类
1.定义水果类,属性为品种(kind),价钱(price);
2.定义测试类Test,定义Map集合,将name和age赋值,存入到Map集合,然后利用BeanUtils将数据封装到水果类对象中
3.将属性值取出来
public class Fruit {
private String kind;
private double price;
省略set,get方法
To String 方法
}
主方法
public class Test {
public static void main(String[] args) throws Exception, InvocationTargetException {
//创建对象
Fruit f = new Fruit();
//定义集合,
//创建集合
Map map = new HashMap();
//添加
map.put("kind", "苹果");
map.put("price", "5.2");
BeanUtils.populate(f, map);
System.out.println(f);
}
}