• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I need help, with logic:iterate in HashMap

 
Jose Larico
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to do a ITERATE in a jsp page. For this I have been using a HashMap and each element of this map contains an ArrayList. In the array list different objects are stocked. I would lilke to recuperate each ArrayList and make it de second iterate. Ex.
ArrayList liste = new ArrayList();
UnObject o1 = new UnObject();
o1.setId(1);
o1.setSubject("Some text 1");
liste.add(o);
UnObject o2 = new UnObject();
o2.setId(2);
o2.setSubject("Some text 2");
liste.add(o2);
java.util.HashMap h = new java.util.HashMap();
h.put("MyID",liste);
request.setAttribute("myIterate",h);
<logic:iterate id="ref" name="myIterate">
.....
.....
</logic:iterate>
Thank...
 
Uwe Barthel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Larico:
I would like to do a ITERATE in a jsp page. For this I have been using a HashMap and each element of this map contains an ArrayList. In the array list different objects are stocked. I would lilke to recuperate each ArrayList and make it de second iterate. Ex.
ArrayList liste = new ArrayList();
UnObject o1 = new UnObject();
o1.setId(1);
o1.setSubject("Some text 1");
liste.add(o);
UnObject o2 = new UnObject();
o2.setId(2);
o2.setSubject("Some text 2");
liste.add(o2);
java.util.HashMap h = new java.util.HashMap();
h.put("MyID",liste);
request.setAttribute("myIterate",h);
<logic:iterate id="ref" name="myIterate">
.....
.....
</logic:iterate>
Thank...

Ok, take a look into the documentation of the struts logic tag.
http://jakarta.apache.org/struts/userGuide/struts-logic.html
-----8<----------8<----------8<-----
...
Normally, each object exposed by the ITERATE TAG is an element of the underlaying collection you are iterating over. However, if you iterate over a Map, the exposed object is of type Map.Entry that has two properties:
  • key - The key under which this item is stored in the underlaying Map
  • value - The value that corresponds to this key.

  • So, if you wish to iterate over the values of a Hashmap, you would implement code like the following:

    ...
    -----8<----------8<----------8<-----
    So, hope that helps
    Uwe Barthel
     
    sha ram
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Is there a nested logic iterate tag? If so, Please give me an example.
     
    sha ram
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Try this....
    <%
    ArrayList liste = new ArrayList();
    liste.add("apple");
    liste.add("grapes");
    java.util.HashMap h = new java.util.HashMap();
    h.put("MyID",liste);
    request.setAttribute("attr",h);
    %>
    <logic:iterate id="it" name="attr">
    <logic:iterate name="it" property="value" id="order">
    <bean:write name="order" />
    </logic:iterate>
    </logic:iterate>
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic