简体   繁体   中英

How to take LIST input in @RequestParam in Spring MVC?

Heyy, I want to take a list of data in my request param,here "personIdCollection" is a set of list but when i am hitting through postman i am getting a bad request. Here is my code.

controller

@PostMapping("/face-tag-data")
public String getFaceTaggedData(@RequestParam String projectId,@RequestParam List<String> personIdCollection) {
    return null;
}

and here is my ajax

var data = {};
            data.personIdCollection = personIdCollection;
            data.projectId = $("#projectId").val();
            $.ajax({
                type:'POST',
                url:contextPath+'/face-tag-data',
                data:data,
                success:function(resp){
                    console.log(resp);
                },
                failure:function(resp){
                    console.log(resp);
                }
            });

This is working for me. I do not use an ajax-request but instead submit my form directly but it should work either way.

My controller looks like:

@RequestMapping(value="addSingleArticles", method=RequestMethod.POST)
public @ResponseBody String addSingleArticles(
        ArticleSubmitData pupilPackageData,
        HttpServletRequest request) {

    ... // do something with the data

}

As you can see I have defined my own composite type which consists of three lists. So you obviously can use it with only one list directly.

public class ArticleSubmitData {

  private List<Article> singleArticles;
  private List<Article> packageArticle;
  private List<Article> popupArticles;

  ... // getter & setter, inner classes etc.

}

In my server page oder faclet I use the following html-code to make this work

...
    <input id="" class="" type="text" name="singleArticles[${line.index}].engraving" />
...

So the trick is to define the variables on your webpage as an array and use this in your controller as a list. As you can see in my example I also use an inner class in my composite class which has extra attributes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM