wood burning stoves 2.0*
The moose likes Struts and the fly likes How to display Dynamic Menus and sub-menus using <s:iterator> tag in struts 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 "How to display Dynamic Menus and sub-menus using <s:iterator> tag in struts" Watch "How to display Dynamic Menus and sub-menus using <s:iterator> tag in struts" New topic
Author

How to display Dynamic Menus and sub-menus using <s:iterator> tag in struts

Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
Hi, I am devolping a project using Struts 2 but i'm caught in this problem where i want to display the main menus as headings which are in say List1 and the submenus for all the main menus are in List 2. Now as i am using a nested iteration, it displays all the submenus under each main menu. I suppose this is quite valid as per the code i have written coz there is no way to break the inner iteration depending upon which submenus belong to the main menu. Can somebody help me with this situation. Thanks in advance

Here is my JSP code



And the action code t retrieve the menus
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You don't send the data to the page with any information regarding which sub-menus are associated with which top-level menus; it might make more sense to use a data structure that encapsulates that information.
Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
Thanks for the reply. but can you explain a bit, what kind of data structure you are referring to.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A tree of some sort, although in your case only one level deep. Really just some nested lists.
Abhilash Chander
Ranch Hand

Joined: Oct 18, 2009
Posts: 39
I finally found the solution. It has nothing to do with any of the tags. I just added one more object to the sub menu list containing the parent menu id. On the jsp page inside the first <s:iterator> tag i used an <s:if> tag to compare whether the parent id matches the parent id in each sub menu. The inner s iterator only iterates if the two matches. Otherwise it is skipped. That way all the submenus contain their parent's id and when it matches they are dispalyed under their parent. I'm pasting the code if it is of any help.



David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Seems unnecessarily complicated, but if it works, that's great :)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display Dynamic Menus and sub-menus using <s:iterator> tag in struts
 
Similar Threads
Exception while using JNDI resources
Error occured while initialising for loop:
Sending Email from jsp page
iterate an object in struts-2
jsp to pdf