aspose file tools*
The moose likes Struts and the fly likes dynamic checkboxes in struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "dynamic checkboxes in struts 2" Watch "dynamic checkboxes in struts 2" New topic
Author

dynamic checkboxes in struts 2

alberto vegas
Greenhorn

Joined: Sep 03, 2008
Posts: 5
Hi!

I'm having trouble with the following scenario.
I generate a table with a column in which i place a checkbox per row. The number of checkboxes created depends on the size of a collection. Something similar to the gmail inbox folder. I want to be able to select one or more of these checkboxes to perform some action over the objects they represent (remove them in my case)

If i use checkboxlist tag in my jsp page, (lets say i have 10 objects in the collection) it displays the 10 checkboxes toguether, and not one in each row (dont know how to format that)

So i decided to try the checkbox tag, but i cant get the values in my action class.

I have something like this:

<s:iterator value="taxSystem.operations" status="rowstatus">
<tr>
<td>
<s roperty value="#rowstatus.index" />
</td>

<td align="center">
<s roperty value="operationName" />
</td>

<td align="center">
<s roperty value="code"/>
</td>

<td align="center">
<s:checkbox name="selectedOperations[#rowstatus.index]" theme="simple" />
</td>
</tr>
</s:iterator>

In my class action i have a variable String[]selectedOperations with setter and getter... but when i see what gets inside i have only the position [0] with a String like this: "true, true, true" depending on how many checkboxes were checked. The thing is that i cant distinguis which true corresponds with which checkbox since empty checkboxes dont include "false" to that String.

Can anybody give a hand and tell me what do i do wrong?
In case it was not obvious yet, i am new in Struts 2, and dont know Struts, so please be patient

Thanks in advance
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I believe I had the same problem you are have yesterday. I was able to get it to work by doing the following:



I wish I had an explanation for why this worked for me but I don't have a good understanding of what the process is that struts2 uses to populate arrays and ArrayLists from html forms. If anyone know a good place that describes this process, please let me know.
alberto vegas
Greenhorn

Joined: Sep 03, 2008
Posts: 5
Hi Tom,

Thanks for your reply, in fact i'm a step closer to get it thanks to your post, but unfortunatelly it's still not working completelly fine for me. With your code i can get my variable operationsSelected in my action class created with the proper size, but the values inside are null. If my collection is size=5, never mind how many checkboxes i select, my array String[] operationsSelected will be created size=5, but with null inside each position...

I'm still testing so if i can find out something, i'll post it here

Regards
alberto vegas
Greenhorn

Joined: Sep 03, 2008
Posts: 5
At the end i substituted the checkboxes with links "Remove"
Its a pity that i couldnt understand how to solve that, but it was getting too much time consuming

Regards!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic checkboxes in struts 2