I have a form with a list of nested fieldsets corresponding to a collection of objects, backed by a form object server-side. Fieldsets can be added or removed client-side. I want to submit a form without caring about object indices or sparse lists in command object.
Here's my Controller method code:
@PostMapping("/foobar")
public String doPost(FoobarForm form) {
//...
}
In PHP, Rails etc it's very easy:
<input name="prop[]">
, and it will automatically populate $_POST["prop"]
with all the values.
Working with Spring MVC, I tried these things:
<input name="prop[]">
- doesn't work sayingInvalid property 'prop[]' of bean class [...]: Invalid index in property path 'prop[]'; nested exception is java.lang.NumberFormatException: For input string: ""
<input name="prop">
- will not bind to a list-typed bean property, even when multiple fields present.<input name="prop[${i}]">
- implies all that hassle with sparse list and index handling, both client-side and server-side. Certainly not the right way to do things when working with a powerful web framework.
I'm wondering why can't I just use []
in property name and let Spring create a list automatically? It was asked three times on Spring JIRA without any reasonable responses.