Question

I display rows in an HTML table with a checkbox in a column in each row of the table something like the following.

<s:iterator value="objects" status="loopStatus">

    <td>
        <s:label for="%{id}" value="%{#loopStatus.index+1}"/>
        <s:checkbox id="%{id}" name="chk" fieldValue="%{id}" value="false"/>
    </td>

    <!--Other columns-->

</s:iterator>

objects is is a list of objects of a JPA entity, List<Entity> initialized in an action class. id is a value of type Long that corresponds to a primary key column in the database.


The values of these checkboxes are set to its corresponding property in an action class, when the form is submitted.

private List<Long>chk;

//setters & getters.

The values of selected checkboxes are correctly set to this property chk. This list of values is then used to delete rows from the database.


This works just fine. A problem occurs, when there is only one row in the table. In which case, an unnecessary conversion error occurs, while performing operations other than deletion (like updating/inserting a row) which requires this checkbox to be selected which is quite unnecessary and should happen only while performing deletion.

Invalid field value for field "chk".

If I deliberately add an extra checkbox with the same name chk then, this conversion error disappears.

Presumably it appears that a single checkbox is not correctly mapped to a list of values of type Long, List<Long>.

What is the way to get rid of this error, when there is only a single row to be displayed in an HTML table?

Was it helpful?

Solution

If you don't check a single checkbox it value by default is set to false. You can set the default in checkbox interceptor uncheckedValue property.

Additionally you may consider to add hidden field so that checkbox interceptor will think there are multiple chekboxes when there is only one and won't add default value.

For your checkbox the hidden field name will be __checkbox_chk.

<s:hidden name="__checkbox_chk"/>

OTHER TIPS

Try this one which explicitly cites the array index:

<s:iterator value="objects" status="loopStatus">

    <td>
        <s:label for="%{id}" value="%{#loopStatus.index+1}"/>
        <s:checkbox id="%{id}" name="chk[%{#loopStatus.count}]" fieldValue="%{id}" value="false"/>
    </td>

    <!--Other columns-->

</s:iterator>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top