views:

112

answers:

1

we have a list of domain objects needing to be edited on an html page. For example, the command & domain objects:

class MyCommand {
    List<Person> persons;
}

class Person {
    String fname;
    String lname;
}

Then, the HTML I expect to have the Spring MVC tag libraries generate is like this:

<form>
   <input name="persons[0].fname">&nbsp;<input name="persons[0].lname"><br/>
   <input name="persons[1].fname">&nbsp;<input name="persons[1].lname"><br/>
   <input name="persons[2].fname">&nbsp;<input name="persons[2].lname"><br/>
   ...
   <input name="persons[n].fname">&nbsp;<input name="persons[n].lname"><br/>
</form>

But can't see how to express this using the Spring Form Tag Libraries (using Spring 2.5.6.). I want to use the tag libraries so that it takes care of binding existing values to the tags for editing (when they're there).

Any tips?

+1  A: 

There isn't a way to simply have the Spring Form Tags generate the whole list based on the collection (it will do this for the options in a select box, but that's the only collection-based expansion I'm aware of). However, you can still use the Spring Form Tags within a loop like so:

<c:forEach var="person" varStatus="loopStatus" items="myCommand.persons">
   <form:input path="persons[${loopStatus.index}].fname" />&nbsp;<form:input path="persons[${loopStatus.index}].lname" /><br />
</c:forEach>
JacobM
works like a charm -- thanks much!
eqbridges