Binding a list in @RequestParam

Total Post:3

 1256  View(s)
Rate this:
I'm sending some parameters from a form in this way:

myparam[0]     : 'myValue1'
myparam[1]     : 'myValue2'
myparam[2]     : 'myValue3'
otherParam     : 'otherValue'
anotherParam   : 'anotherValue' 
I know I can get all the params in the controller method by adding a parameter like

public String controllerMethod(@RequestParam Map<String, String> params){
I want to bind the parameters myParam[] (not the other ones) to a list or array (anything that keeps the index order), so I've tried with a syntax like:

public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){

public String controllerMethod(@RequestParam(value="myParam") String[] myParams){
but none of them are binding the myParams. Even when I add a value to the map it is not able to bind the params:

public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
Is there any syntax to bind some params to a list or array without having to create an object as @ModelAttribute with a list attribute in it?
  1. Post:397

    Re: Binding a list in @RequestParam

    Arrays in @RequestParam are used for binding several parameters of the same name:

    If you need to bind @ModelAttribute-style indexed parameters, I guess you need @ModelAttribute anyway.

    Or you could just do it that way:

    public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
    That works for example for forms like this:

    <input type="checkbox" name="myParam[]" value="myVal1" />
    <input type="checkbox" name="myParam[]" value="myVal2" />