在@RequestParam中绑定列表 [英] Binding a list in @RequestParam
问题描述
我以这种方式从表单发送一些参数:
I'm sending some parameters from a form in this way:
myparam[0] : 'myValue1'
myparam[1] : 'myValue2'
myparam[2] : 'myValue3'
otherParam : 'otherValue'
anotherParam : 'anotherValue'
...
我知道我可以通过添加一个参数来获得控制器方法中的所有参数:
I know I can get all the params in the controller method by adding a parameter like
public String controllerMethod(@RequestParam Map<String, String> params){
....
}
我想将参数myParam [](而不是其他的)绑定到一个列表或数组所以我尝试了一个语法如:
I want to bind the parameters myParam[] (not the other ones) to a list or array (anything that keeps the index order), so I've tried with a syntax like:
public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){
....
}
public String controllerMethod(@RequestParam(value="myParam") String[] myParams){
....
}
但没有一个绑定myParams。即使当我添加一个值到地图,它不能绑定的参数:
but none of them are binding the myParams. Even when I add a value to the map it is not able to bind the params:
public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
....
}
b $ b
有没有任何语法将一些参数绑定到列表或数组,而不必创建一个对象作为@ModelAttribute与其列表属性?
Is there any syntax to bind some params to a list or array without having to create an object as @ModelAttribute with a list attribute in it?
感谢
推荐答案
@RequestParam
中的数组用于绑定多个参数同名:
Arrays in @RequestParam
are used for binding several parameters of the same name:
myparam=myValue1&myparam=myValue2&myparam=myValue3
如果您需要绑定 @ModelAttribute
- 索引参数,我想您需要 @ModelAttribute
无论如何。
If you need to bind @ModelAttribute
-style indexed parameters, I guess you need @ModelAttribute
anyway.
这篇关于在@RequestParam中绑定列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!