File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Using checkboxes with nested logic:iterate tags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Using checkboxes with nested logic:iterate tags" Watch "Using checkboxes with nested logic:iterate tags" New topic
Author

Using checkboxes with nested logic:iterate tags

Nathan England
Greenhorn

Joined: Jul 13, 2009
Posts: 20
I'm having a difficult time with checkboxes and iterating through a few nested collections.

-My form page contains a Hashtable of OrderMaster objects.
-An OrderMaster contains an ArrayList of Order objects.
-The Order objects themselves contain an ArrayList of details.

I am displaying that structure on my JSP page as an unordered list, and I would like to have a checkbox on at least the detail level (lowest level).

My unordered list ends up looking like this:
(the [] denotes a check box)

Order (Order Master 1)
----[] Release 1(Order)
--------[] Detail 1
--------[] Detail 2
----[] Release 2 (Order)
--------[] Detail 1


I have the details of the structure displaying correctly. The data in each object is being printed to the screen. My problem is the values of the check boxes are not being submitted with the form.

Starting from the lowest level going up, here are my data objects and form.

The OrderDetail is a plain java object:


Now for my Order object, which contains a "selected" flag, and a list of OrderDetails. Another plain java object:


One more plain java object, which contains a list of Orders


Now for my ActionForm. The form object contains a table of OrderMaster objects


Here is the JSP code for my list. This seems to display the data properly for each nested iterate, however, clicking on the check box and submitting doesn't change any of the bean's "selected" value.
I can't figure out why that doesn't work:

Nathan England
Greenhorn

Joined: Jul 13, 2009
Posts: 20
Could this have something to do with my form being in session scope?

I'm going to attempt to write some JSP that sets data in the form object when the check box is clicked, and see if that works...
Nathan England
Greenhorn

Joined: Jul 13, 2009
Posts: 20
A solution!!

I finally got it to work, after much trial and error.

Multiple loops with multiple levels of check boxes work much better using Struts <nested> tags. I'm not really sure why, but using nested tags with <nested:checkbox> works like a charm. Seems really simple now.

Posting solution here. (I added an "int id;" to each of my data objects, not sure if that is relevant or not but I was simply following some examples from the web).

My ActionForm is in session scope.

(my ActionForm is parent, has list of MasterOrder objects, MasterOrder is parent of a list of Order(s) and Order is parent of a list of OrderDetail.)

The OrderDetail is a plain java object:


Now for my Order object, which contains a "selected" flag, and a list of OrderDetails. Another plain java object:


One more plain java object, which contains a list of Orders


Now for my ActionForm. The form object contains a table of OrderMaster objects

(*Side note, while doing research I may need to put code in the reset() method to handle unchecked values..if that is the case I'll post that fix below. Right now it's not really doing anything.)

Now for the JSP code. Which seems ridiculously simple now:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using checkboxes with nested logic:iterate tags
 
Similar Threads
LWUIT: Custom List Renderer with checkboxes to select multiple items
Saving form bean with Array of objects (collection)
How to get selected records from Data Table in JSF
Struts2 Checkbox: How to set value back to Bean
Checking Checkbox values in indexed form