3
我试图使用BeanUtils的同一个Java bean类似于以下交互的列表索引属性:设置与BeanUtils的
public class Bean {
private List<Integer> prices = new LinkedList<Integer>();
public List<Integer> getPrices() {
return prices;
}
}
按照BeanUtils documentation,BeanUtils的不支持索引属性是List
S:
作为对JavaBeans规范的扩展,BeanUtils包认为其基础数据类型为java.util.List(或List的实现)的任何属性也被索引。
然而,假设我尝试做一些这样的:
Bean bean = new Bean();
// Add nulls to make the list the correct size
bean.getPrices().add(null);
bean.getPrices().add(null);
BeanUtils.setProperty(bean, "prices[0]", 123);
PropertyUtils.setProperty(bean, "prices[1]", 456);
System.out.println("prices[0] = " + BeanUtils.getProperty(bean, "prices[0]"));
System.out.println("prices[1] = " + BeanUtils.getProperty(bean, "prices[1]"));
输出是:
prices[0] = null
prices[1] = 456
为什么BeanUtils.setProperty()
无法设置索引的属性,而PropertyUtils.setProperty()
能? BeanUtils是否不支持List
s中的对象的类型转换?
谢谢,这确实使它发挥作用。然而,通过实际的BeanUtils源代码,我发现它实际上并没有使用setPrices()方法来修改价格List的内容 - 它只是使用getPrices()方法并直接修改列表。 – 2011-01-31 14:29:38