How to bind a list of object to SpringMvc Controller?

后端 未结 1 826
灰色年华
灰色年华 2020-12-28 11:19

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

@RequestMapping(value = \"/test\", method = RequestMethod.GET)
public ModelAndView test(
    @Mod         


        
1条回答
  •  有刺的猬
    2020-12-28 11:53

    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 list = new AutoPopulatingList(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
    

    0 讨论(0)
提交回复
热议问题