简体   繁体   中英

List of Objects as RequestParam in Spring MVC

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 MyObjectEditor .

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) { ... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM