Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 views
in Technique[技术] by (71.8m points)

Can Spring MVC handle multivalue query parameter?

Having this http://myserver/find-by-phones?phone=123&phone=345 request, is it possible to handle with something like this:

@Controller
public class Controller{
    @RequestMapping("/find-by-phones")
    public String find(List<String> phones){
       ...
    }
}

Can Spring MVC some how convert multi-value param phones to a list of Strings (or other objects?

Thanks.

Alex

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

phone=val1&phone=val2&phone=val3

-

public String method(@RequestParam(value="phone") String[] phoneArray){
    ....
}

You can then convert it into a list using Arrays.asList(..) method

EDIT1:

As suggested by emdadul, latest version of spring can do like below as well:

public String method(@RequestParam(value="phone", required=false) List<String> phones){
    ....
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...