Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 302
Joaquín L. Robles Avatar asked Oct 31 '25 05:10

Joaquín L. Robles


1 Answers

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) { ... }
like image 167
axtavt Avatar answered Nov 02 '25 19:11

axtavt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!