如何使用Spring MVC将多个值从表单传递给控制器? [英] How to pass multiple values from the form to a controller using Spring MVC?
问题描述
我正在使用Spring MVC模式,我正在尝试创建一个类似于此的JSP文件 -
I am working with Spring MVC pattern and I am trying to make a JSP file which is like this as of now -
在表单中,我有四行,第一行仅用于标记和其他三行我需要将我的数据放在文本框中。例如 - 对于DC1,我将在文本框中插入 numServers
值,我将在文本框中插入 ipaddress
值主机名
文本框中的值。
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.
<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>
现在我应该在点击提交按钮后阅读这些值,因为我将输入必要的值文本框。我在下面的代码中使用 RequestMapping
-
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(value = "test", method = RequestMethod.GET)
public HashMap<String, String> testRequest(@RequestParam MultiValueMap<?, ?> allRequestParams) {
}
最初,我使用的是 MultiValueMap
,但我不确定我的上述输入标准是如何适合这个的?一般来说,我不确定如何在上述用例的上述方法中构造输入,以便我可以在方法中轻松提取所有值?
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?
这是我第一次开始使用Spring MVC,因此遇到了一些困难..
This is the first time I started working with Spring MVC so having some difficulties..
推荐答案
您可以将数组传递给spring控制器如下所示:
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];
}
这篇关于如何使用Spring MVC将多个值从表单传递给控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!