问题描述
我有一个DTO(豆)与的ArrayList
字段:
I've got a DTO (bean) with ArrayList
field:
public MyDTO {
...
private List<MyThing> things;
...
... getters, setters and so on
}
在我initBinder我有:
In my initBinder I have:
@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);
}
});
}
和在我的控制器请求方式:
And in my controller request method:
@RequestMapping({"save"})
public ModelAndView doSaveMyDTO(@ModelAttribute MyDTO myDTO) {
// very strange myDTO comes here=(
}
现在的问题是,当我在 registerCustomEditor
工作人员的事情
阵列就可以了。
The problem is that while I'm in registerCustomEditor
staff the things
array is ok.
但是,当我到了 doSaveMyDTO
法 - MyDTO.things
看起来像实际的一个元素数组的数组值:
But when I get to the doSaveMyDTO
method - MyDTO.things
looks like Array of one element arrays of actual values:
预计(在initBinder的东西):
Expected (things in initBinder):
[value1, value2, value3]
获取doSaveMyDTO(myDTO.getThings()):
Get in doSaveMyDTO (myDTO.getThings()):
[[value1], [value2], [value3]]
为什么呢?请解释...
Why? Please explain...
推荐答案
如果该请求是正确形成(东西= V1和放大器;东西= v2和放大器;东西= V3
或东西= V1,V2,V3
),春天的内置转换器应适当将其转换为列表
- 无需注册自己的。
If the request is correctly formed (things=v1&things=v2&things=v3
or things=v1,v2,v3
), spring's built-in converters should properly convert it to a List
- no need to register your own.
如果您输入JSON,那么你需要 @RequestBody
而不是 @ModelAttribute
If your input is JSON, then you'd need @RequestBody
instead of @ModelAttribute
这篇关于Spring MVC的绑定:如何绑定的ArrayList&LT; ...&GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!