How to pass multiple values from the form to a controller using Spring MVC?

user2467545

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">&nbsp;</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..

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];
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass checkbox values to the controller in Spring MVC

how to get form values to controller in spring mvc

Pass Map value to Spring MVC controller using <form:hidden>

How to I pass multiple parameters from a form to a controller in Asp.net MVC core?

How to pass textbox values and what was the action to spring mvc controller?

How to Pass Image File and Form Data From Ajax to MVC Controller

Pass many arguments from controller to service using spring MVC

Pass value from AJAX to Controller using Spring MVC

How to send multiple values for the same field from JSP to Controller in Spring MVC (Eclipse)?

How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC?

how to pass jstl values in spring mvc in commandObject/modelAttribute of form

How to pass a null value to a Spring controller from html form

How to Pass List using Ajax call to Spring MVC Controller

how to pass a list from controller to jsp and iterate it in javascript in spring mvc

How to Pass Multiple Objects from Controller action to View in MVC?

In Spring MVC How to get form object in JSP from controller ?

how to get dynamic values from posting a form in the Spring controller

MVC : Pass values from textbox to controller action

How to pass data from one html form to multiple tables using spring boot

How to pass specific data from webgrid into MVC Controller using Javascript?

How to save multiple identical entities from one form in Spring Controller?

Thymeleaf + Spring MVC - How to persist values from a list or enum on a form?

How to Pass Html Input value(Html Form) to java controller ? - Spring MVC

In MVC/Razor, how do I get the values of multiple checkboxes and pass them all to the controller?

How to pass an array from a form to a controller method?

how to pass the array in mvc controller using javascript

Spring MVC Controller, how to keep BindingResult errors, while emptying the form values

How to pass the results from a HTMLDropDownList to a MVC Controller?

Unable to pass multipart file using Ajax from JSP to Controller in Spring MVC