Spring mvc is Working with Checkboxes
To be able to bind multiple values (from HTML table) onto a collection of objects
This is already possible, provided that your Collection is a nested property of your form object. More specifically, you have to use a List or an array, as you need to refer to them by index.
<c:forEach items="${person.addresses}" var="address" varStatus="loopStatus">
<spring:bind path="person.addresses[${loopStatus.index}].street">
Street: <input type="text" name="<%= status.getExpression() %>" value="<%= status.getDisplayValue() %>">
</spring:bind>
<p>
<spring:bind path="person.addresses[${loopStatus.index}].zip">
Zip: <input type="text" name="<%= status.getExpression() %>" value="<%= status.getValue() %>">
</spring:bind>
<p>
</c:forEach>
If you have a Collection that is not part of a form object, simply create a form object just for your web form, containing the Collection as nested property. You could also try to bind to multiple unrelated form objects, but that's far more hassle, as our out-of-the-box controller implementations are designed for a single command/form object.
After, how can we validate the objects of this list
if we have, for example, a AddressValidator.java ?
I have:
<bean name="/EditActorForm" class="com.EditActorForm">
<property name="formView"><value>ActorForm</value></property>
<property name="successView"><value>ViewActor</value></property>
<property name="validator"><ref local="AddressValidator"/></property>
</bean>
and:
public class AddressValidator implements Validator {
public boolean supports(Class clazz) {
return Address.class.isAssignableFrom(clazz);
}
public void validate(Object obj, Errors errors) {
ValidationUtils.rejectIfEmpt (errors, "street","STREET", "Info: Empty is not allowed.");
}
}
<input name="person[5].name" value="Joe"/>
however the square brackets prevent javascript from getting to that HTML id, for example this won't work:
<a href="#" onClick="javascript:alert('Yo: ' + document.forms[0].person[5].name)">click me</a>
as it tries to look for an HTML ID of "person" (you can put an HTML ID of that in your page, but that's not going to be related to person[5]).
I haven't looked at the binding code to see if the square brackets are needed as I don't need this functionality yet.
Single Checkbox
Note that the hidden field is neccessary to bind when the checkbox is unchecked.
<spring:bind path="command.myBooleanProperty"> <input type="hidden" name="_<c:out value="${status.expression}"/>"> <input type="checkbox" name="<c:out value="${status.expression}"/>" value="true" <c:if test="${status.value}">checked</c:if>/> </spring:bind> </c:forEach>
Multiple Checkboxes
One way of binding multiple checkboxes is to create a child array where its objects have a boolean flag to indicate selected status.
<c:forEach items="${command.childArray}" var="child" varStatus="loopStatus"> <spring:bind path="command.childArray[${loopStatus.index}].selected"> <input type="hidden" name="_<c:out value="${status.expression}"/>"> <input type="checkbox" name="<c:out value="${status.expression}"/>" value="true" <c:if test="${status.value}">checked</c:if>/> </spring:bind> </c:forEach>