如何将 BeanUtils setProperty 方法与 map 一起使用。
例如这个方法: public void setAddress(String type, Address 地址); 可以使用以下方式设置: BeanUtils.setProperty(beanObject, "地址(家)", addressObject );
但是如果我要设置的对象是一个Map,有可能吗?怎么办?
最佳答案
使用Map语法foo(bar)
(bar
是map foo
的key):
public static class Bean{
private Map<String, String> data = new HashMap<String, String>();
public Map<String, String> getData(){
return data;
}
public void setData(final Map<String, String> data){
this.data = data;
}
}
public static void main(final String[] args) throws Exception{
final Bean bean = new Bean();
// assign the foo key of the data property to the value 'bar'
BeanUtils.setProperty(bean, "data(foo)", "bar");
System.out.println(bean.data);
}
输出:
{foo=bar}
关于java - 带有 map 的 BeanUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1330467/