Spring MVC 可以处理多值查询参数吗? [英] Can Spring MVC handle multivalue query parameter?

查看:21
本文介绍了Spring MVC 可以处理多值查询参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有这个 http://myserver/find-by-phones?phone=123&phone=345 请求,是否可以处理这样的事情:

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){
       ...
    }
}

Spring MVC 能否将多值参数 phones 转换为 String 列表(或其他对象?

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

谢谢.

亚历克斯

推荐答案

@RequestParam中的Arrays"用于绑定多个同名参数:

"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){
    ....
}

然后您可以使用 Arrays.asList(..) 方法将其转换为列表

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

编辑 1:

根据 emdadul 的建议,最新版本的 spring 也可以像下面这样:

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){
    ....
}

这篇关于Spring MVC 可以处理多值查询参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆