Java 在@RequestParam中绑定列表

Java 在@RequestParam中绑定列表,java,spring,data-binding,collections,spring-mvc,Java,Spring,Data Binding,Collections,Spring Mvc,我通过以下方式从表单发送一些参数: myparam[0] : 'myValue1' myparam[1] : 'myValue2' myparam[2] : 'myValue3' otherParam : 'otherValue' anotherParam : 'anotherValue' ... 我知道我可以通过添加一个参数获得控制器方法中的所有参数,如 public String controllerMethod(@RequestParam Map&l

我通过以下方式从表单发送一些参数:

myparam[0]     : 'myValue1'
myparam[1]     : 'myValue2'
myparam[2]     : 'myValue3'
otherParam     : 'otherValue'
anotherParam   : 'anotherValue' 
...
我知道我可以通过添加一个参数获得控制器方法中的所有参数,如

public String controllerMethod(@RequestParam Map<String, String> params){
    ....
}
但它们都没有绑定myParams。即使向映射添加值,也无法绑定参数:

public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
    ....
}
publicstringcontrollermethod(@RequestParam(value=“myParam”)映射参数){
....
}
是否有任何语法可以将某些参数绑定到列表或数组,而不必创建一个包含列表属性的@ModelAttribute对象


感谢
@RequestParam
中的数组用于绑定多个同名参数:

myparam=myValue1&myparam=myValue2&myparam=myValue3

如果您需要绑定
@modeldattribute
-风格的索引参数,我想您无论如何都需要
@modeldattribute

实现这一点的一种方法(以一种黑客方式)是为
列表创建一个包装类。像这样:

class ListWrapper {
     List<String> myList; 
     // getters and setters
}
如果在请求中传递的集合名称与包装类的集合字段名称匹配,则无需使用
@RequestParam
@modeldattribute
注释,在我的示例中,请求参数应如下所示:

public String controllerMethod(ListWrapper wrapper) {
    ....
}
myList[0]     : 'myValue1'
myList[1]     : 'myValue2'
myList[2]     : 'myValue3'
otherParam    : 'otherValue'
anotherParam  : 'anotherValue'
<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />
@PostMapping("/post-topics")
public void handleSubscriptions(@RequestParam("topics") Collection<String> topicStrings) {

    topicStrings.forEach(topic -> System.out.println(topic));
}

或者你可以这样做:

public String controllerMethod(@RequestParam(value="myParam[]") String[] myParams){
    ....
}
例如,它适用于以下表单:

public String controllerMethod(ListWrapper wrapper) {
    ....
}
myList[0]     : 'myValue1'
myList[1]     : 'myValue2'
myList[2]     : 'myValue3'
otherParam    : 'otherValue'
anotherParam  : 'anotherValue'
<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />
@PostMapping("/post-topics")
public void handleSubscriptions(@RequestParam("topics") Collection<String> topicStrings) {

    topicStrings.forEach(topic -> System.out.println(topic));
}


这是最简单的解决方案:)

使用复选框切换更改隐藏字段值,如下所示

HTML:

<input type='hidden' value='Unchecked' id="deleteAll" name='anyName'>
<input type="checkbox"  onclick="toggle(this)"/> Delete All
function toggle(obj) {`var $input = $(obj);
    if ($input.prop('checked')) {

    $('#deleteAll').attr( 'value','Checked');

    } else {

    $('#deleteAll').attr( 'value','Unchecked');

    }

}

作为对Donal Fellows所说的补充,您可以将List与@RequestParam一起使用

public String controllerMethod(@RequestParam(value="myParam") List<ObjectToParse> myParam){
....
}
publicstringcontrollermethod(@RequestParam(value=“myParam”)列表myParam){
....
}

希望有帮助

如果
method=RequestMethod.GET
您可以使用
@RequestParam-List-groupVal
,则订阅basil在对问题本身的评论中所说的内容

然后,使用参数列表调用服务非常简单:

API_URL?groupVal=kkk,ccc,mmm

我不清楚,虽然您可以接受集合作为请求参数,但在消费者端您仍然必须以逗号分隔的值传入集合项

例如,如果服务器端api如下所示:

public String controllerMethod(ListWrapper wrapper) {
    ....
}
myList[0]     : 'myValue1'
myList[1]     : 'myValue2'
myList[2]     : 'myValue3'
otherParam    : 'otherValue'
anotherParam  : 'anotherValue'
<input type="checkbox" name="myParam[]" value="myVal1" />
<input type="checkbox" name="myParam[]" value="myVal2" />
@PostMapping("/post-topics")
public void handleSubscriptions(@RequestParam("topics") Collection<String> topicStrings) {

    topicStrings.forEach(topic -> System.out.println(topic));
}
@PostMapping(/post主题)
public void handleSubscriptions(@RequestParam(“topics”)集合主题字符串){
topicString.forEach(主题->系统输出.println(主题));
}
将集合作为RequestParam(如下面的)直接传递到RestTemplate将导致数据损坏

public void subscribeToTopics() {

    List<String> topics = Arrays.asList("first-topic", "second-topic", "third-topic");

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(
            "http://localhost:8088/post-topics?topics={topics}",
            null,
            ResponseEntity.class,
            topics);
}
public void subscribeToTopics(){
List topics=Arrays.asList(“第一主题”、“第二主题”、“第三主题”);
RestTemplate RestTemplate=新RestTemplate();
restTemplate.postForEntity(
"http://localhost:8088/post-主题?主题={topics}“,
无效的
响应级,
专题);
}
相反,你可以使用

public void subscribeToTopics() {

    List<String> topicStrings = Arrays.asList("first-topic", "second-topic", "third-topic");
    String topics = String.join(",",topicStrings);

    RestTemplate restTemplate = new RestTemplate();
    restTemplate.postForEntity(
            "http://localhost:8088/post-topics?topics={topics}",
            null,
            ResponseEntity.class,
            topics);
}
public void subscribeToTopics(){
List topicStrings=Arrays.asList(“第一主题”、“第二主题”、“第三主题”);
字符串主题=String.join(“,”主题字符串);
RestTemplate RestTemplate=新RestTemplate();
restTemplate.postForEntity(
"http://localhost:8088/post-主题?主题={topics}“,
无效的
响应级,
专题);
}

,希望它能让人省去头痛:)

我认为这是不可能的。
HandlerMethodInvoker.resolveRequestParam
中的代码仅获取第一个值(Spring Boot):关于
method=RequestMethod.GET
method=RequestMethod.POST
?如果
.GET
@RequestParam List groupVal
?groupVal=kkk,ccc,mmm
成功完成(Spring Boot),可能会出现顺序问题(在我的情况下,这一点非常重要),因为我通过序列化表单和使用ajax发送I来发送参数。我将使用“传统的”@modeldattribute方法。您是否知道如何使用UriTemplate或其他方法使用这种排序映射构造URI?(对于这类资源的客户端)。回答我自己的问题,它表明spring UriTemplate不支持RFC6570,请使用damnhandy实现:这与使用@ModelAttribute几乎相同,唯一的区别是参数没有注释。我想避免使用@modeldattribute,只是因为我不想创建包装器。我在stackoverflow的某个地方读到(我不记得确切的位置)如果在控制器方法中添加一个参数而没有@ModelAttribute注释(并且它不是像HttpRequest、HttpResponse这样的特殊对象),框架会将其视为使用@ModelAttribute注释。因此,如果这是真的,那么这与@modeldattribute完全相同。但是谢谢你的回答。这是否保留了顺序?在Spring 3.0中,我只能使用名称而不是[],因此:@RequestParam(value=“myParam”)字符串[]myParam我不分享@MSmith的发现。但是,是否可以通过此获取
列表
。也可以获得类似于
List
的Javabean,我认为可以从param name中删除括号。