spring 如何使用spring mvc使用@RequestParam捕获多个参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22398892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to capture multiple parameters using @RequestParam using spring mvc?
提问by Shivayan Mukherjee
Suppose a hyperlink is clicked and an url is fired with the following parameter list myparam=myValue1&myparam=myValue2&myparam=myValue3. Now how can I capture all the parameters using @RequestParamin spring mvc?
假设点击了一个超链接,并使用以下参数列表触发了一个 url myparam=myValue1&myparam=myValue2&myparam=myValue3。现在如何捕获@RequestParam在 spring mvc 中使用的所有参数?
My requirement is I have to capture all the params and put them in a map.
我的要求是我必须捕获所有参数并将它们放在地图中。
Please help!
请帮忙!
回答by Touchstone
@RequestMapping(value = "users/newuser", method = RequestMethod.POST)
public String saveUser(@RequestParam Map<String,String> requestParams) throws Exception{
String userName=requestParams.get("email");
String password=requestParams.get("password");
//perform DB operations
return "profile";
}
You could use RequestParam in the above mentioned manner.
您可以以上述方式使用 RequestParam。
回答by Vitaly Velikodny
It seems you can't get
好像拿不到
Map<String,String>
because all your params have same name "myparam"
因为您所有的参数都具有相同的名称“myparam”
Try this instead:
试试这个:
public ModelAndView method(@RequestParam("myparam") List<String> params) { }
回答by Ralph
To get all parameters at once try this:
要一次获取所有参数,请尝试以下操作:
public ModelAndView postResultPage(@RequestParam MultiValueMap<String, String> params)
This feature is described in the @RequestParamjava doc (3. Paragraph):
这个特性在@RequestParamjava doc(3. Paragraph)中有描述:
Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.
If the method parameter type is Map and a request parameter name is specified, then the request parameter value is converted to a Map assuming an appropriate conversion strategy is available.
If the method parameter is
Map<String, String>orMultiValueMap<String, String>and a parameter name is not specified, then the map parameter is populated with all request parameter names and values.
指示方法参数应绑定到 Web 请求参数的注释。支持 Servlet 和 Portlet 环境中带注释的处理程序方法。
如果方法参数类型为 Map 并且指定了请求参数名称,则假定适当的转换策略可用,请求参数值将转换为 Map。
如果方法参数是
Map<String, String>orMultiValueMap<String, String>并且未指定参数名称,则使用所有请求参数名称和值填充 map 参数。
回答by Brian
As of Spring 3.0, you can also use MultiValueMapto achieve this:
从 Spring 3.0 开始,您还可以使用MultiValueMap来实现这一点:
A rudimentary example would be:
一个基本的例子是:
public String someMethod(@RequestParam MultiValueMap<String,String> params) {
final Iterator<Entry<String, List<String>>> it = params.entrySet().iterator();
while(it.hasNext()) {
final String k = it.next().getKey();
final List<String> values = it.next().getValue();
}
return "dummy_response";
}
回答by Akki
If anyone is trying to do the same in Spring Boot, use RequestBodyin place of RequestParam
如果有人试图在 Spring Boot 中做同样的事情,请使用RequestBody代替RequestParam
回答by Frank.Chang
Spring mvc can support List<Object>, Set<Object>and Map<Object>param, but without @RequestParam.
Spring mvc 可以支持List<Object>,Set<Object>和Map<Object>param,但没有 @RequestParam。
Take List<Object>as example, if your object is User.java, and it like this:
以List<Object>为例,如果你的目标是User.java,它是这样的:
public class User {
private String name;
private int age;
// getter and setter
}
And you want pass a param of List<User>, you can use url like this
并且你想传递一个参数List<User>,你可以像这样使用 url
http://127.0.0.1:8080/list?users[0].name=Alice&users[0].age=26&users[1].name=Bob&users[1].age=16
Remember to encode the url, the url after encoded is like this:
记得对url进行编码,编码后的url是这样的:
http://127.0.0.1:8080/list?users%5B0%5D.name=Alice&users%5B0%5D.age=26&users%5B1%5D.name=Bob&users%5B1%5D.age=16
Example of List<Object>, Set<Object>and Map<Object>is displayed in my github.
的示例List<Object>,Set<Object>并Map<Object>显示在我的 github 中。

