Java 如何使用 Spring MVC 将多个值从表单传递到控制器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21952041/
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 pass multiple values from the form to a controller using Spring MVC?
提问by
I am working with Spring MVC pattern and I am trying to make a JSP file which is like this as of now -
我正在使用 Spring MVC 模式,并且我正在尝试制作一个像这样的 JSP 文件 -
In the form, I have four rows, first row is just for labelling and other three rows I need to put my data in the text box. For example- for DC1, I will insert numServers
value in the textbox, I will insert ipaddress
value in the textbox and hostname
value in the textbox.
在表单中,我有四行,第一行仅用于标记,其他三行我需要将数据放入文本框中。例如 - 对于 DC1,我将numServers
在文本框中插入值,我将在文本框中插入ipaddress
值并在文本框中插入值hostname
。
<form method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Datacenter Name</td>
<td>Number of Servers</td>
<td>IP Address(comma separated)</td>
<td>Host Name(comma separated)</td>
</tr>
<tr>
<td><label for="dc1">DC1</label></td>
<td><input type="text" name="numservers" size="20"></td>
<td><input type="text" name="ipaddress" size="60"></td>
<td><input type="text" name="hostname" size="60"></td>
</tr>
<tr>
<td><label for="dc1">DC2</label></td>
<td><input type="text" name="numservers" size="20"></td>
<td><input type="text" name="ipaddress" size="60"></td>
<td><input type="text" name="hostname" size="60"></td>
</tr>
<tr>
<td><label for="dc1">DC3</label></td>
<td><input type="text" name="numservers" size="20"></td>
<td><input type="text" name="ipaddress" size="60"></td>
<td><input type="text" name="hostname" size="60"></td>
</tr>
<tr><td colspan="2"> </td></tr>
</table>
<input type="submit">
</form>
Now I am supposed to read these values after hitting the submit button as I will be typing necessary values in the textbox. I am using RequestMapping
in my below code -
现在我应该在点击提交按钮后读取这些值,因为我将在文本框中输入必要的值。我RequestMapping
在下面的代码中使用 -
@RequestMapping(value = "test", method = RequestMethod.GET)
public HashMap<String, String> testRequest(@RequestParam MultiValueMap<?, ?> allRequestParams) {
}
Initially, I was using MultiValueMap
but I am not sure how my above input criteria will fit into this? In general, I am not sure how do I structure my input in the above method for the above use case so that I can extract all the values easily in the method?
最初,我正在使用,MultiValueMap
但我不确定我上面的输入标准如何适合这个?一般来说,我不确定如何在上述方法中为上述用例构建我的输入,以便我可以在该方法中轻松提取所有值?
This is the first time I started working with Spring MVC so having some difficulties..
这是我第一次开始使用 Spring MVC,所以遇到了一些困难..
采纳答案by Ramesh Kotha
You can pass an array to spring controller like below:
您可以将数组传递给弹簧控制器,如下所示:
@RequestMapping(value = "test", method = RequestMethod.GET)
public HashMap<String, String> testRequest(@RequestParam String[] numservers, @RequestParam String[] ipaddress, @RequestParam String[] hostname) {
//By using array position you can determine each row
//DC1 values
String dc1_numServer = numservers[0];
String dc1_ipaddres= ipaddress[0];
String dc1_hostname= hostname[0];
//DC2 values
String dc2_numServer = numservers[1];
String dc2_ipaddres= ipaddress[1];
String dc2_hostname= hostname[1];
//DC3 values
String dc3_numServer = numservers[2];
String dc3_ipaddres= ipaddress[2];
String dc3_hostname= hostname[2];
}
回答by Biju Kunjummen
A simple way would be to define a model to hold this entire information:
一种简单的方法是定义一个模型来保存整个信息:
public class Datacenter {
private String name;
private String ipAddress;
private String hostName;
private String numServers;
// Add getters and setters..
}
public class Datacenters {
private List<Datacenter> datacenters;
//Getters and setters..
}
Now, you will have to change your form to cleanly bind to this data structure in the following way:
现在,您必须通过以下方式更改表单以干净地绑定到此数据结构:
<tr>
<td><label for="dc1">DC1</label></td>
<input type="hidden" name="datacenters[0].name" value="DC1"/>
<td><input type="text" name="datacenter[0].numServers" size="20"></td>
<td><input type="text" name="datacenter[0].ipAddress" size="60"></td>
<td><input type="text" name="datacenter[0].hostName" size="60"></td>
</tr>
<tr>
<td><label for="dc1">DC2</label></td>
<input type="hidden" name="datacenters[1].name" value="DC2"/>
<td><input type="text" name="datacenter[1].numServers" size="20"></td>
<td><input type="text" name="datacenter[1].ipAddress" size="60"></td>
<td><input type="text" name="datacenter[1].hostName" size="60"></td>
</tr>
With this change you can simply bind this model to your Controller in this way:
通过此更改,您可以通过这种方式简单地将此模型绑定到您的控制器:
@RequestMapping(value = "test", method = RequestMethod.GET)
public String testRequest(DataCenters dataCenters) {
//.. process request
}
That's it, Spring should bind everything for you.
就是这样,Spring 应该为你绑定一切。
回答by A. Gesta
The Status 405 occurs because your listener for the method is a GET. You need to change it to POST so that it'll accept a post.
发生状态 405 是因为该方法的侦听器是 GET。您需要将其更改为 POST 以便它接受帖子。