4

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?

4 Answers 4

1

Use an List in the controller.

In the view you can use this straight html (not sure if this works with spring tags).

<input name="bParameters[{idx}].bParmvalues[0].parmValue" />
2
  • 1
    By using List<> instead of Set<> in controller. Problem solved. Jul 28, 2011 at 4:32
  • 3
    It's more like a workaround. If you work directly on your JPA objects through the form this is not possible. It should not be necessary to workaround this but I haven't been able to find a solution
    – Marc
    Jun 16, 2014 at 12:45
1

Its going to be an array, which Spring will translate into a List; it will also instantiate the List implementation - you don't need to do that in your Command object. Try using

private List<String> bParameters;

public void setBParameters(List<String> bParameters) {
    this.bParameters= bParameters;
}
public List<String> getBParameters() {
    return bParameters;
}

in your Command object. Those values are probably coming in as Strings.

1
  • By using List<> instead of Set<> in controller. Problem solved. Jul 28, 2011 at 4:32
1

I don't have problems binding as

 private Set<Types> typeses = new HashSet<Types>(0);


  <form:textarea path="typeses" style="width:200px;height:150px"/>

I use Spring 3.5. The only problem with this is, that it leaves []-marks on the field for some reason

6
  • I would like to know that too! :)
    – mjgirl
    Jul 28, 2011 at 6:17
  • You mean all the fields in your form or what?
    – mjgirl
    Jul 29, 2011 at 6:16
  • When I bind a path for a field e.g <form:input path="bParameters[${itemsRow.index}].bParmvalues[0].parmValue" /> []-mark diappears. Similarly I bind my all fields and get rid of []-mark. Jul 29, 2011 at 17:18
  • Can you still tell me where you get itemsRow ja ParmValue attributes? Are they attributes of objects that your set hold?
    – mjgirl
    Aug 1, 2011 at 10:03
  • I could not understand your question. itemsRow is a loop counter in jsp which can be set in for-each tag as varStatus="itemsRow">. Hope it may help you. Aug 2, 2011 at 10:04
0

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:

var person = // .. construct form object here

$.ajax(url, {
  method : 'post',
  contentType : 'application/json',
  data : JSON.stringify(person)
});

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:

@RequestMapping(..)
public void save(@RequestBody Person person) {
  ..
}

More info here: https://gerrydevstory.com/2013/08/14/posting-json-to-spring-mvc-controller/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.