Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept 2D arrays in Spring MVC controller?

I'm making a POST request via jQuery Ajax:

$.ajax({
    type: "POST",
    url: opts.save_url,
    data: $(ul_obj).serializeFormList() + "&form_id=" + form_db_id,
});

The $(ul_obj).serializeFormList() creates a 2D array of request params. Here's the screengrab of the params passed to the Spring MVC controller:

enter image description here

Now when I handle this in the Controller I get 404 Bad Request for frmb[][] Here's the code:

public @ResponseBody String saveData(@RequestParam(value= "form_id", required = true) String formId, 
                                    @RequestParam(value= "frmb", required = true) String[][] formArray) {

                //Content removed for brevity
                }

What is the exact way to handle this request data? Please guide me. I'm stuck real bad.

like image 225
LittleLebowski Avatar asked Dec 14 '25 16:12

LittleLebowski


1 Answers

As I see you are concatenating the parameters

"&form_id=" + form_db_id

you can do the same for "frmb"

frmb=1,2&frmb=2,3

so when you try get the String [][] using

@RequestParam(value= "frmb", required = true) String[][] formArray)

you will get

formArray = [[1,2],[2,3]]
like image 111
newB Avatar answered Dec 17 '25 04:12

newB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!