Dashboard > Spring Discuss > Working with Spring MVC > Working with Checkboxes
  Spring Discuss Log In View a printable version of the current page.  
  Working with Checkboxes
Added by Eu Gene Lim, last edited by Eu Gene Lim on Apr 26, 2005
Labels: 
(None)

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>

Site running on a free Atlassian Confluence Open Source Project License granted to Spring Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators