Check boxes not showing as checked even though values set to true on server side

144 Views Asked by At

All my check boxes stay unchecked even though the test variable has been set to true on the server side. When i inspect the code all checkbox values=true.

   <c:if test="${not empty myObject.objectList}">
        <c:forEach items="${myObject.objectList}" varStatus="index">
            <tr class="cdata">
                <td align="center">
                    <td><form:checkbox  path="test" /> <c:out 
            value="${testName}" 
                /></td>
               </td>
            </tr>
        </c:forEach>
      </c:if>
2

There are 2 best solutions below

3
kowsikbabu On

Setting values true need not necessarily reflect check boxes to be checked

Instead, you can use the checked attribute. Just write

checked ="checked"

in the checkbox and it'll reflect the change

0
Alan Hay On

The issue is you have not bound the checkbox to a specific item. You can bind it to a specific item in the list as below.

You must ensure consistent ordering of the list to ensure submitted values are updated correctly.

<c:if test="${not empty myObject.objectList}">
    <c:forEach var="items" items="${myObject.objectList}" varStatus="status">
        <tr class="cdata">
            <td align="center">
                <form:checkbox path="items[${status.index}].test" /> 
                ${testName}
            </td>
        </tr>
    </c:forEach>
 </c:if>