2

I'm using the following action on a SpringMvc application:

@RequestMapping(value = "/test", method = RequestMethod.GET)
public ModelAndView test(
    @ModelAttribute List<Group> groups
) { 
 //return whatever
}

My Group class has an 'id' and a 'name' property. Default getter/setter. How should i call this action in order to have this list correctly instanciated?

I tried something like:
/test?groups.id=2&groups.name=stackrocks&groups.id=3&groups.name=stackrules
Didn't work.

Also tried:
/test?groups[].id=2&groups[].name=stackrocks&groups[].id=3&groups[].name=stackrules
No success.

So, how to bind a list when using SpringMvc?

goenning
  • 6,514
  • 1
  • 35
  • 42

1 Answers1

6

You can't bind parameters of method with the exactly that signature. @ModelAttribute binds attributes to the fields of the corresponding model object, so you can encapsulate your List into object:

public class Groups {
    private List<Group> list = new AutoPopulatingList<Group>(Group.class);  
    ...    
}

@RequestMapping(value = "/test", method = RequestMethod.GET)  
public ModelAndView test(  
    @ModelAttribute Groups groups  
) {   
 //return whatever  
}  

and then call it as follows:

/test?list[0].id=2&list[0].name=stackrocks&list[1].id=3&list[1].name=stackrules
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Sadly, no success. I did exactly the way you suggested and the "list" property is always empty. Any other ideas? – goenning Jul 19 '10 at 20:13
  • @axtavt can you please check on this similiar issue,i followed your steps https://stackoverflow.com/questions/45005544/bind-same-attribute-names-of-multiple-dynamically-created-fields-using-dto – BeginnerBro Jul 13 '17 at 11:33