如何使用 Spring MVC 为每个对象传递列表参数? [英] How to pass list parameters for each object using Spring MVC?
问题描述
我在这里使用 Spring MVC RequestMapping
作为 GET
参数.下面是我的代码 -
I am using Spring MVC RequestMapping
here for GET
parameters. Below is my code -
@RequestMapping(value = "index", method = RequestMethod.GET)
public HashMap<String, String> handleRequest(@RequestParam("dc1Servers") String dc1Servers, @RequestParam("dc2Servers") String dc2Servers, @RequestParam("dc3Servers") String dc3Servers) {
HashMap<String, String> model = new HashMap<String, String>();
String helloWorld = "Hello World!";
model.put("greeting", helloWorld);
System.out.println(dc1Servers);
System.out.println(dc2Servers);
System.out.println(dc3Servers);
return model;
}
我正在点击这个 URL - http://127.0.0.1:8080/dataweb/index?dc1Servers=3&dc2Servers=3&dc3Servers=3
然后它进入上面的代码并打印在控制台上输出 3
以进行所有打印并且工作正常.
I am hitting this URL - http://127.0.0.1:8080/dataweb/index?dc1Servers=3&dc2Servers=3&dc3Servers=3
then it goes into the above code and prints out 3
on the console for all the print and works fine.
现在,如果你看到,我有 dc1
、dc2
和 dc3
.
Now if you see, I have dc1
, dc2
and dc3
.
所以对于dc1
,我想在URL中将这些作为参数发送-
So for dc1
, I would like to send these in the URL as the parameters-
dc1Servers=3
dc1HostA
dc1HostB
dc1HostC
dc1IPAddresssA
dc1IPAddresssB
dc1IPAddresssC
与dc2
类似,我想在URL中将这些作为参数发送-
Similarly for dc2
, I would like to send these in the URL as the parameters-
dc2Servers=3
dc2HostA
dc2HostB
dc2HostC
dc2IPAddresssA
dc2IPAddresssB
dc2IPAddresssC
与dc3
类似,我想在URL中将这些作为参数发送-
Similarly for dc3
, I would like to send these in the URL as the parameters-
dc3Servers=3
dc3HostA
dc3HostB
dc3HostC
dc3IPAddresssA
dc3IPAddresssB
dc3IPAddresssC
现在我不确定我将如何为这个用例构建一个 URL 以及我的方法会是什么样子?我想通过一个 URL 调用发送它们.
Now I am not sure how would I construct an URL for this use case and how would my method will look like? I would like to send them in one single URL call.
这个用例可以使用 Spring MVC 吗?
Is this use case possible to do using Spring MVC?
推荐答案
你可以以任何你认为合适的方式传递参数,一种方法是通过它们的自然分组传递它们,如下所示:http://127.0.0.1:8080/dataweb/index?dc1Server=dc1HostA,dc1IPAddressA&dc1Server=dcHostB,dc1IPAddressB....
You could pass the parameters any way you see fit, one way is to pass them by their natural grouping like this: http://127.0.0.1:8080/dataweb/index?dc1Server=dc1HostA,dc1IPAddressA&dc1Server=dcHostB,dc1IPAddressB....
通过为每个组使用相同的参数名称,您需要使用 MultiValueMap
.MultiValueMap
或多或少是一个 Map
,因此具有相同名称(dc1Server、dc2Server 等)的参数将位于相同的列表.您可以使用它来获取您传入的参数的完整列表,如下所示:
By using the same parameter name for each group you'd need to use a MultiValueMap
. A MultiValueMap
is more or less a Map<String, List<String>>
, so parameters with the same name(dc1Server, dc2Server, etc) would be in the same list. You would use this to get a full list of the params you passed in like this:
public HashMap<String, String> handleRequest(@RequestParam MultiValueMap<String,String> allRequestParams)
那么你有 3 个键,dc1Server
将包含所有 dc1Server 数据,dc2Server
将包含所有dc2Server 等
Then you'd have 3 keys, dc1Server
would contain all the dc1Server data, dc2Server
all the
dc2Server etc
如果你想直接映射到域对象,可以考虑使用 @ModelAttribute
:链接
Could look into using @ModelAttribute
if you want to do direct mapping to domain objects: Link
编辑
public void testSpringMultiValueMap() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("dc1Server", "dc1HostA,dc1IPAddressA");
map.add("dc1Server", "dc1HostB,dc1IPAddressB");
map.add("dc1Server", "dc1HostC,dc1IPAddressC");
map.add("dc2Server", "dc2HostA,dc2IPAddressA");
map.add("dc2Server", "dc2HostB,dc2IPAddressB");
map.add("dc2Server", "dc2HostC,dc2IPAddressC");
List<String> dc1List = map.get("dc1Server");
for(String pair: dc1List) {
String[] tokens = pair.split(",");
System.out.println("host: " + tokens[0] + " ip: " + tokens[1]);
}
}
这篇关于如何使用 Spring MVC 为每个对象传递列表参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!