开发者

List of Objects as RequestParam in Spring MVC

开发者 https://www.devze.com 2023-03-11 06:06 出处:网络
I want to send a list of object id\'s (generated by the user selecting checkboxes) by POST to an action, so I can get a java.util.List<MyObject> converted using an My开发者_StackOverflow社区Obje

I want to send a list of object id's (generated by the user selecting checkboxes) by POST to an action, so I can get a java.util.List<MyObject> converted using an My开发者_StackOverflow社区ObjectEditor.

So, is it possible to do this?

@InitBinder
public void initBinder (WebDataBinder binder) {
    binder.registerCustomEditor(MyObject.class, new MyObjectEditor());
}
@RequestMapping (value = "", method = RequestMethod.POST)
public String action (@RequestParam List<MyObject> myList, Model model) {
    // more stuff here
}

And my POST would be like this:

myList[0] = 12
myList[1] = 15
myList[2] = 7

Thanks!


This kind of binding is not supported by @RequestParam, so you have to use @ModelAttribute:

class MyObjects {
    private List<MyObject> myList;
    ...
}

public String action (@ModelAttribute MyObjects myObjects, Model model) { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消