Skip to content

Issue with index order when using checkbox in JSP

I have defined row and column using jsp form:checkbox as shown in the code below. I am using List to capture the entry. The problem with using form:checkbox is that it is not indexed properly. When I use form:input the input index is proper. Is there something I can do to capture index properly? By index I mean that when I put form:input I see the variable “a” has 12 values and unentered value are also shown but with form:checkbox I don’t see 12 values and order is random.

Code in controller

private List<String> a;

Code in JSP

<tr>
    <th class="text-center">JAN</th>
    <th class="text-center">FEB</th>
    <th class="text-center">MAR</th>
    <th class="text-center">APR</th>
    <th class="text-center">MAY</th>
    <th class="text-center">JUN</th>
    <th class="text-center">JUL</th>
    <th class="text-center">AUG</th>
    <th class="text-center">SEP</th>
    <th class="text-center">OCT</th>
    <th class="text-center">NOV</th>
    <th class="text-center">DEC</th>
</tr>
<tr>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
    <td><form:checkbox path="a" value="Y"/></td>
</tr>

Answer

In your implementation you will only receive in your List the values for the checkboxes that have been checked, without any order.

Please, instead of take care of the index, pay attention to the actual checkbox value. Try this instead:

<tr>
    <th class="text-center">JAN</th>
    <th class="text-center">FEB</th>
    <th class="text-center">MAR</th>
    <th class="text-center">APR</th>
    <th class="text-center">MAY</th>
    <th class="text-center">JUN</th>
    <th class="text-center">JUL</th>
    <th class="text-center">AUG</th>
    <th class="text-center">SEP</th>
    <th class="text-center">OCT</th>
    <th class="text-center">NOV</th>
    <th class="text-center">DEC</th>
</tr>
<tr>
    <td><form:checkbox path="a" value="jan"/></td>
    <td><form:checkbox path="a" value="feb"/></td>
    <td><form:checkbox path="a" value="mar"/></td>
    <td><form:checkbox path="a" value="apr"/></td>
    <td><form:checkbox path="a" value="may"/></td>
    <td><form:checkbox path="a" value="jun"/></td>
    <td><form:checkbox path="a" value="jul"/></td>
    <td><form:checkbox path="a" value="aug"/></td>
    <td><form:checkbox path="a" value="sep"/></td>
    <td><form:checkbox path="a" value="oct"/></td>
    <td><form:checkbox path="a" value="nov"/></td>
    <td><form:checkbox path="a" value="dec"/></td>
</tr>

When the user submit the information, your a List will contain as values the different literals for the months selected, jan, apr, etcetera.

I think this article could be helpful as well.