You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Laurie Harper <la...@holoweb.net> on 2007/02/12 22:04:03 UTC

[s2] Multiple s:checkbox backed by array/list

I have a view with a table each row or which shows a single table 
record. Each row has a checkbox so you can select multiple items and 
click 'delete selected' to remove them in one go. This is implemented 
with an s:checkbox backed by a property in the action. What I want is 
for the property to contain a list of selected values or, if none were 
selected, for the property to be null / an empty list. I can't get that 
to work :-(

Firstly, the checkbox values are record IDs, so naturally I started out 
trying to make the property an array of Integer. That works fine when 
there are boxes checked, but if not, I get a conversion error on *every* 
checkbox saying it has an invalid value.

So then I made the property an array of String. That works too when 
there are boxes checked, but now I have to handle conversion to int by 
hand. However, if no boxes are checked, the action property gets set to 
an array of length one: the String value "false"! That explains why the 
Integer array didn't work.

Here's an outline of the relevant code:

Action:

     private Integer[] selected;
     public Integer[] getSelected() {...}
     public void setSelected(Integer[] value) {...}

JSP:

     <table>
       <s:iterate value="%{users}>
         <tr>
           <td>
             <s:checkox name="selected" fieldValue="%{id}"/>
           </td>
         </tr>
       </s:iterate>
     </table>

Again, the intent is that the 'selected' property should be null or an 
empty list if no checkboxes are selected. I need Struts *not* to try and 
store a 'false' in there in that case.

Any ideas?

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org