How to send multiple rows of data to Spring Controller

Common requirement would be sending multiple rows of data to Spring Controller.

We use @RequestParam String value to hold the single form field. But when we dealing with multiple rows @RequestParam String value won’t work.

If It is single row like below like below

S.NO First Name Last Name Age
1 AAA BBB 23

We can use @RequestParam annotation to capture the form data:

public String saveEmployee(@RequestParam Integer sNo, @RequestParam String firstName, @RequestParam String firstName, @RequestParam Integer age){
//here we can access the form data.
}

Suppose we have a form with multiple rows. Then We have to use @RequestParam String[] values,

Let’s see the example below:

S.NO First Name Last Name Age
1 AAA BBB 23
2 CCC DDD 24
3 EEE FFF 28

And here is the Controller Code:


public String saveEmployees(@RequestParam Integer[] sNo, @RequestParam[] String firstName, @RequestParam[] String lastName, @RequestParam[] Integer age){

 //first row data
 Integer sNo_0 = sNo[0];
 String firstName_0 = firstName[0];
 String lastName_0 = lastName[0];
 Integer age_0 = age[0];

//second row data

 Integer sNo_1 = sNo[1];
 String firstName_1 = firstName[1];
 String lastName_1 = lastName[1];
 Integer age_1 = age[1];

//third row data

 Integer sNo_2 = sNo[2];
 String firstName_2 = firstName[2];
 String lastName_2 = lastName[2];
 Integer age_2 = age[2];
 }

Thanks for reading.

Responses

  1. Nice blog here! Also your website loads up fast!
    What host are you using? Can I get your affiliate link to your host?
    I wish my web site loaded up as quickly as yours lol

  2. Thank you

Leave a reply to Nuwan Mudalie Cancel reply