How to work with checkboxes in Spring MVC

The usual problem with checkboxes in web environment is that if they were checked when you  are rendering the page and user unchecks them, there is no way by default to know that on back end. This is especially true when you are working with the ‘form’ for Spring wizard, since formBackingObject, where you can reset the checkbox boolean values to false,  is only called once for the first page and not on the subsequent pages. Spring MVC has a better solution to this problem, buy using the following construct:

<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>

This approach is discussed on http://forum.springframework.org/archive/index.php/t-16093.html and on http://opensource.atlassian.com/confluence/spring/display/DISC/Working+with+Checkboxes

Leave a Reply

You must be logged in to post a comment.