在@RequestParam中绑定列表
我以这种方式从表单发送一些参数:
myparam[0] : 'myValue1'
myparam[1] : 'myValue2'
myparam[2] : 'myValue3'
otherParam : 'otherValue'
anotherParam : 'anotherValue'
...
我知道我可以通过添加参数来获取控制器方法中的所有参数,就像
public String controllerMethod(@RequestParam Map<String, String> params){
....
}
我想将参数 myParam[] (而不是其他参数)绑定到列表或数组(任何保持索引顺序的东西),所以我尝试使用如下语法:
public String controllerMethod(@RequestParam(value="myParam") List<String> myParams){
....
}
但
public String controllerMethod(@RequestParam(value="myParam") String[] myParams){
....
}
它们都没有绑定 myParams。即使当我向映射添加值时,它也无法绑定参数:
public String controllerMethod(@RequestParam(value="myParam") Map<String, String> params){
....
}
是否有任何语法可以将某些参数绑定到列表或数组,而不必创建一个带有列表属性的对象作为 @ModelAttribute ?
谢谢
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){
....
}
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){
....
}
and
public String controllerMethod(@RequestParam(value="myParam") String[] 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){
....
}
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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
或者你可以这样做:
例如,这适用于这样的表单:
这是最简单的解决方案:)
Or you could just do it that way:
That works for example for forms like this:
This is the simplest solution :)
@RequestParam
中的数组用于绑定多个同名参数:如果你需要绑定
@ModelAttribute
风格的索引参数,我猜你需要@ModelAttribute无论如何。
Arrays in
@RequestParam
are used for binding several parameters of the same name:If you need to bind
@ModelAttribute
-style indexed parameters, I guess you need@ModelAttribute
anyway.订阅 basil 在问题本身的评论中所说的话,如果
method = RequestMethod.GET
你可以使用@RequestParam List;组值
。然后使用参数列表调用服务非常简单:
Subscribing what basil said in a comment to the question itself, if
method = RequestMethod.GET
you can use@RequestParam List<String> groupVal
.Then calling the service with the list of params is as simple as:
只是补充 Donal Fellows 所说的,您可以使用 List 和 @RequestParam
希望它有帮助!
Just complementing what Donal Fellows said, you can use List with @RequestParam
Hope it helps!
实现此目的的一种方法(以一种黑客的方式)是为
List
创建一个包装类。像这样:那么你的控制器方法签名将如下所示:
如果您在请求中传递的集合名称与集合匹配,则无需使用
@RequestParam
或@ModelAttribute
注释包装类的字段名称,在我的示例中,您的请求参数应如下所示:One way you could accomplish this (in a hackish way) is to create a wrapper class for the
List
. Like this:Then your controller method signature would look like this:
No need to use the
@RequestParam
or@ModelAttribute
annotation if the collection name you pass in the request matches the collection field name of the wrapper class, in my example your request parameters should look like this:对我来说,虽然您可以接受集合作为请求参数,但在消费者方面您仍然必须将集合项作为逗号分隔值传递,这对我来说并不明显。
例如,如果服务器端 api 如下所示:
直接将集合作为 RequestParam 传递到 RestTemplate,如下将导致数据损坏
相反,您可以使用
完整的示例可以在这里找到,希望它能帮别人解决头痛:)
It wasn't obvious to me that although you can accept a Collection as a request param, but on the consumer side you still have to pass in the collection items as comma separated values.
For example if the server side api looks like this:
Directly passing in a collection to the RestTemplate as a RequestParam like below will result in data corruption
Instead you can use
The complete example can be found here, hope it saves someone the headache :)
使用复选框切换更改隐藏字段值,如下所示...
HTML:
脚本:
Change hidden field value with checkbox toggle like below...
HTML:
Script: