嗨:我用BeanUtil来获取一些豆类的属性,然后把它们放到地图上。但是我发现getProperty()只能返回字符串值,我想知道它是否能够返回本机(原始)类型?
例如:
豆子:
public class Entity {
private String name;
private int age;
private List<Date> childs = new ArrayList<Date>();
public Entity(String name, int age, List<Date> childs) {
this.name = name;
this.age = age;
this.childs = childs;
}
public static Entity newInstance() {
List<Date> list = new ArrayList<Date>();
list.add(new Date());
list.add(new Date());
return new Entity("Beanutil", 23, list);
}
//the getter and setter for all the fields
}Then I want to get some field property of it,and I do not exactly know which fields are requried,so I use it this way:
public class BeanUtilMain {
public static void main(String[] args){
String[] requestPro={"name","childs"}; //this field is specified by clien
Map<String, Object> map=new HashMap<String, Object>();
Entity en=Entity.newInstance();
for(String p:requestPro){
map.put(p, BeanUtils.getProperty(en, p));
}
System.out.println(map);
}
}然后地图是:
{name=beanutil, childs=Wed Dec 01 12:24:37 CST 2010}"childs“字段的类型是java.util.List,但在示例中,它被转换为java.lang.String。
我必须保持地图中字段的原始类型。
有什么想法吗?
发布于 2011-03-10 18:56:22
您可以使用PropertyUtils.getProperty(Object, String)精确地完成这一任务。它返回没有类型转换的字段的值。
https://stackoverflow.com/questions/4321283
复制相似问题