List objects as RequestParam in Spring MVC

I want to send a list of object identifier (generated by a user select flag) using POST , so I can convert java.util.List<MyObject>using MyObjectEditor.

So can this be done?

@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 will look like this:

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

Thank!

+3
source share
1 answer

This type of binding is not supported @RequestParam, so you should use @ModelAttribute:

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

public String action (@ModelAttribute MyObjects myObjects, Model model) { ... }
+3
source

All Articles