This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes logic:iterate hashmap with key of hashmap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "logic:iterate hashmap with key of hashmap" Watch "logic:iterate hashmap with key of hashmap" New topic
Author

logic:iterate hashmap with key of hashmap

azhar bharat
Ranch Hand

Joined: Jul 17, 2006
Posts: 87
Hi
Here is the scenario.
I have a bean. Which contains 2 hashmaps.
First hashmap contains a key and a string as value.
Second hashmap contains a key and corresponding value is another hashmap.
---the bean methods

public void setCategories(HashMap categories)
{
this.categories=categories;
}

public HashMap getCategories()
{
return categories;
}

//subcategories contains hashmap as value
public void setSubcategories(HashMap subcategories)
{
this.subcategories=subcategories;
}

public HashMap getSubcategories()
{
return subcategories;
}
---
I need help in iterating throught the second hashmap.
the require the result like below:
category1
-subcategory1.1
-subcategory1.1
-subcategory1.1
category2
-subcategory2.1
-subcategory2.2
-subcategory2.3

Right now, i am able to list all categories, using code below
<logic:iterate id="listcat" scope="application" name="<%=IConstants.CLASSIFIEDSCATEGORYBEAN_KEY%>" property="categories" >
<bean:write name="listcat" property="value" />
</logic:iterate>

As i said i want to list the subcategories listed along with the categories. I need to do this with nested "logic:iterate" tags.


NOTE: general idea of hashmap
catogories hashmap
key value
1 category1
2 category2
subcategories
key value
Key Value
1 11 subcategory1
12 subcategory2
2 21 subcategory1
22 subcategory2

===
I will provide the scriptlet i use now.
===
<%
ClassifiedsCategoryBean ccb=(ClassifiedsCategoryBean)application.getAttribute(IConstants.CLASSIFIEDSCATEGORYBEAN_KEY);
HashMap cats=new HashMap();
cats=(HashMap)ccb.getCategories();
Iterator ic=cats.keySet().iterator();
HashMap subts=new HashMap();
HashMap subs=new HashMap();
Iterator is=null;
while(ic.hasNext())
{
Object key = ic.next();
Object value = cats.get(key);
out.print("<font color=\'blue\'>"+value.toString()+"</font><br/>");
subts=(HashMap)ccb.getSubcategories();
subs=(HashMap)subts.get(key);
is=subs.keySet().iterator();
while(is.hasNext())
{
Object keys = is.next();
Object values = subs.get(keys);
out.print(values.toString()+"<br/>");
}
out.print("<br/><br/>");
}
%>
===
Now this works perfect. but this is temporary and i need replace the same using logic:iterator. I might have to use the indexId attribute to achieve this, but i could not get the desired result. Please help.
Vallidevi Appana
Greenhorn

Joined: Jun 30, 2006
Posts: 25
Hi

You can try out the following logic.



But here the problem is we are iterating through the subcategory map for each iteration of category map. Obviously its not an optimal solution. Correct me if I am wrong.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd suggest you use JSTL tags rather than Struts tags for this. This is considered "best practice", as JSTL tags are more a part of the J2EE standard, and they are also more powerful.

Here's a possible solution:

<c:set var="beanName" value="<%=IConstants.CLASSIFIEDSCATEGORYBEAN_KEY%>" />

<c:forEach var="listcat" items='${applicationScope[beanName].categories}'>
${listcat.value}<br>
<c:set var="subcategories" value='${applicationScope[beanName].subcategories[listcat.key]}' />
<c:forEach var="subcategory" items="${subcategories}">
${subcategory.value}<br>
</c:forEach>
</c:forEach>

Note: the above would work for JSP 2.0. It would require some modification for JSP 1.2.

Also, I'd recommend you use TreeMap rather than HashMap for your categories and subcategories. TreeMap always presents the map in order by key, while HashMap does not guarantee that the elements will be presented in any specific order.


Merrill
Consultant, Sima Solutions
azhar bharat
Ranch Hand

Joined: Jul 17, 2006
Posts: 87
Thanks vallidevi and merrill!!!

vallidevi,
your "struts:iterate" solutions worked out fine with little changes. had to specify "type" attribute in some of the "bean:define" tags without which a jsp compilation error occurs.

merrill,
thanks for suggestion to use treemap. I am still using hashmap, but used a getter method which converts it to treemap before the collection is returned for use when i require sorted output.

thanx again
 
jQuery in Action, 2nd edition
 
subject: logic:iterate hashmap with key of hashmap
 
Similar Threads
html:iterator troubles
not able to display the value using HashMap
Iterate hashmap problem
3 - level folder display in struts
accessing a Collection through a jsp-bean