Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

handle form post with a array of items in spring MVC

I'm trying to send some data from the client side to the server, and have it processed into a file download. I'm using a simple HTML form because I want to initialize a file download (and not AJAX). one of the form fields is an array of items. (the other two are name and description strings). I'm serializing this field to a string (JSON.stringify) before submitting the form.

on the server side I tried a million techniques (@ModelAttribute vs. @RequestBody, different jackson mapping bean configurations) to either convert this to a single type or to three separate types (String + String + List/Array).

the examples I found were only for AJAX... can anyone supply a working example or a description of one?

=======

Update: I've implemented a workaround by JSON.stringify-ing the collection and passing it in one of the inputs, and on the server side I have:

@RequestMapping(method = RequestMethod.POST, value = "exportSectionsToExcel")
  public HttpEntity<byte[]> createExcelWorkBook(@ModelAttribute ExportSectionsListForm exportSectionsListForm) {
Section[] sectionObjects = gson.fromJson(exportSectionsListForm.getSections(), Section[].class);
...

with ExportSectionsListForm object containing strings only:

public class ExportSectionsListForm {
private String name;
private String url;
private String rssUrl;
private String sections;
...
(omitting ctor, getters and setters)

additionally, I found this promising link: http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/ but didn't try it - seems like I'll need to dynamically generate input elements for this to work, but it might actually be the right solution. has anyone tried this?

like image 309
Yonatan Karni Avatar asked Jan 14 '23 22:01

Yonatan Karni


1 Answers

The @ModelAttribute tag will try to build the object based on form postings. Since you are serializing your form values to JSON, this wont work. @RequestBody simply gives you a String representing the request body. So, you could get the String representing the JSON being passed in, then demarshal the JSON using Jackson of FlexJSON (or whatever JSON library you use). I am not sure this is the best approach, though.

I would question why you need to serialize the form to JSON to begin with. Spring handles forms with Lists/Maps just fine. Simply submit the form using the @ModelAttribute, making your "array" and List, or whatever you are expecting, on the Controller. So, if I am interpreting your example correctly, my ModelAttribute would look like:

public class ExportSectionsFormBean {
  private String name;
  private String url;
  private String rssUrl;
  private List<String> sections;
  /* getters/setters */
}

Then my Controller method would look like:

@RequestMapping(method = RequestMethod.POST, value = "exportSectionsToExcel")
public HttpEntity<byte[]> createExcelWorkBook(@ModelAttribute ExportSectionsFormBean exportSectionsFormBean ) {
  /* Do whatever with your  */
}

On the form side, using the Spring JSTL tags, simply make your "sections" fields look like:

<form:input path="sections[0]" />
<form:input path="sections[1]" />

Or, if you'd rather use HTML, then

<input type="text" name="sections[0]" id="sections0" />
<input type="text" name="sections[1]" id="sections1" />

Which is what gets generated by the above JSTL tags. As long as the values for "sections" is put in the HTTP request as 'section[#]=value', you are all set.

like image 164
CodeChimp Avatar answered Jan 17 '23 10:01

CodeChimp