I am working with Spring MVC pattern and I am trying to make a JSP file which is like this as of now -
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>
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) {
}
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?
This is the first time I started working with Spring MVC so having some difficulties..
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];
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments