【发布时间】:2011-10-17 13:56:14
【问题描述】:
我有一个带有 ArrayList 字段的 DTO(bean):
public MyDTO {
...
private List<MyThing> things;
...
... getters, setters and so on
}
在我的 initBinder 我有:
@InitBinder
public void initBinder(WebDataBinder binder) {
...
binder.registerCustomEditor(List.class, "things", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
List<MyThing> things = new ArrayList<MyThings>;
// fill things array with data from text
...
// On that stage things value is correct!
super.setValue(things);
}
});
}
在我的控制器请求方法中:
@RequestMapping({"save"})
public ModelAndView doSaveMyDTO(@ModelAttribute MyDTO myDTO) {
// very strange myDTO comes here=(
}
问题是,当我在 registerCustomEditor 工作人员时,things 数组没问题。
但是当我使用 doSaveMyDTO 方法时 - MyDTO.things 看起来像一个元素数组的实际值数组:
预期(initBinder 中的东西):
[value1, value2, value3]
进入doSaveMyDTO (myDTO.getThings()):
[[value1], [value2], [value3]]
为什么?请解释...
【问题讨论】:
标签: spring binding spring-mvc