MR_AD
MR_AD

Reputation: 71

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>

Upvotes: 3

Views: 170

Answers (2)

Olivier
Olivier

Reputation: 18250

Spring supports array access for the path attribute, so you could do something like this:

Java:

private boolean[] a = new boolean[12];

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[0]"/></td>
    <td><form:checkbox path="a[1]"/></td>
    <td><form:checkbox path="a[2]"/></td>
    <td><form:checkbox path="a[3]"/></td>
    <td><form:checkbox path="a[4]"/></td>
    <td><form:checkbox path="a[5]"/></td>
    <td><form:checkbox path="a[6]"/></td>
    <td><form:checkbox path="a[7]"/></td>
    <td><form:checkbox path="a[8]"/></td>
    <td><form:checkbox path="a[9]"/></td>
    <td><form:checkbox path="a[10]"/></td>
    <td><form:checkbox path="a[11]"/></td>
</tr>

Upvotes: 2

jccampanero
jccampanero

Reputation: 53441

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.

Upvotes: 2

Related Questions