I am trying to bind a spring form with a set in the command object.
In my command class AInstance I defined set as
private Set<BParameter> bParameters = new HashSet<BParameter>();
In jsp I bind it as
<form:input path="bParameters " />
<form:input path="bParameters " />
As its a Java Set so there may be many fields. When I submit this form I tried to get Set as:
Set<BParameter> bParameters = aInstance.getBParameters();
I got Set with a 0 size.
I also tried to bind as
<form:input path="bParameters[${itemsRow.index}].bParmvalues[0].parmValue" />
but got exception
Invalid property 'bParameters[0]' of bean class
What is the problem with my binding?
Use an List in the controller.
In the view you can use this straight html (not sure if this works with spring tags).
Its going to be an array, which Spring will translate into a
List
; it will also instantiate theList
implementation - you don't need to do that in yourCommand
object. Try usingin your
Command
object. Those values are probably coming in as Strings.I cannot find a way to bind Set into form parameters. However other solutions suggesting to "use list instead" is not good enough for me because if the field is part of JPA one-to-many relationship with eager fetching, using List will cause duplicates
Hence the best solution I find so far is by posting the form as JSON -- using Ajax -- instead. Here's how in JQuery:
Note that you need to mark the contentType as
application/json
On the controller side, you can bind this to a Java object by using
@RequestBody
annotation:More info here: https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/
I don't have problems binding as
I use Spring 3.5. The only problem with this is, that it leaves []-marks on the field for some reason