3

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

2 Answers 2

2
+50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It was the last resort I was thinking of. I wanted to avoid it but if there is no other option then I think I have to go with this.
Hi @MR_AD. I understand. As I told, my advice is that you use different values for your checkboxes instead of taking care of the index. AFAIK, this is the standard approach and it should work properly. Maybe you can try something with Javascript and return an array with all the checkboxes and whether they are checked or not, but I found it error prone, and you are missing your form:checkbox logic with javascript, I do not know...
2

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.