This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "dynamic checkboxes in struts 2" Watch "dynamic checkboxes in struts 2" New topic

dynamic checkboxes in struts 2

alberto vegas

Joined: Sep 03, 2008
Posts: 5

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">
<s roperty value="#rowstatus.index" />

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

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

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

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

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

alberto vegas

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

I agree. Here's the link:
subject: dynamic checkboxes in struts 2
It's not a secret anymore!