如何使用 BeanUtils.populate 方法从 String[] 转换为 ArrayList?看起来我可以使用 ArrayConverter 类,但我很难找到关于如何做到这一点的解释。
这是我创建的一些测试代码:
public class TestAction {
public static TestBean testPopulate() throws IllegalAccessException, InvocationTargetException {
HashMap<String, Object> map = new HashMap<String, Object>();
TestBean bean = new TestBean();
String[] names = {"teststring","testboolean","testinteger","testarray"};
String[] array = {"hi","bye"};
Object[] values = {"TEST","","100",array};
int i = 0;
while (i < names.length) {
String name = names[i];
map.put(name, values[i]);
i++;
}
BeanUtils.populate(bean, map);
return bean;
}
public static void main(String[] args) {
try {
TestBean bean = testPopulate();
System.out.println(bean.getTeststring() + " " + bean.getTestinteger() + " " + bean.getTeststring() + " " + bean.isTestboolean());
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class TestBean {
private String teststring;
private boolean testboolean;
private int testinteger;
private ArrayList<String> testarray;
public String getTeststring() {
return teststring;
}
public void setTeststring(String teststring) {
this.teststring = teststring;
}
public boolean isTestboolean() {
return testboolean;
}
public void setTestboolean(boolean testboolean) {
this.testboolean = testboolean;
}
public int getTestinteger() {
return testinteger;
}
public void setTestinteger(int testinteger) {
this.testinteger = testinteger;
}
public ArrayList<String> getTestarray() {
return testarray;
}
public void setTestarray(ArrayList<String> testarray) {
this.testarray = testarray;
}
}
我传入一个 String[] 类型,希望它知道如何转换为 ArrayList,但它失败了。