-1

I am trying to bind form data that is serialized alongside other data within an AJAX call. I can't seem to get Spring to pick it up. It just instantiates an empty form.

JavaScript:

$.ajax({
method: "GET",
url: contextPath,
data: {"form": serializedForm, "otherStuff": otherStuff},
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});

Controller:

@ResponseBody
@RequestMapping(value="/blah", method=RequestMethod.GET)
public String blah(@ModelAttribute("form") Form form, @RequestParam(value="otherStuff[]", required=false) String[] otherStuff, HttpServletResponse response){
    //stuff
}

Is there a way to get Spring to map my form in the method parameter?

It seems like I can grab the parameters separately from an HttpServletRequest, but I would ideally like my form to go through an InitBinder.

Logan Lim
  • 1
  • 3
  • 1
    Possible duplicate of [Spring MVC: Complex object as GET @RequestParam](https://stackoverflow.com/questions/16942193/spring-mvc-complex-object-as-get-requestparam) – Vasan May 07 '18 at 22:24

1 Answers1

0

The issue seemed to be with the AJAX request. Using a key-value pairing for each method parameter doesn't work for the data binding. Instead if I concatenate the serialized data into a single string Spring will be able to map it to each object properly.

$.ajax({
method: "GET",
url: contextPath,
data: serialized + "&otherStuff=" + otherStuff,
contentType:"application/json; charset=utf-8",
cache: false,
success: function(data) {});

I'm not sure if there is a cleaner way of doing this that avoids clumping your objects into a single string, but this is functional.

Logan Lim
  • 1
  • 3