22

I am currently facing some difficulties with Struts2 and the s:iterate tag.

I want to display a label, which is the key in the HashMap, followed by a table (the value in the HashMap) containing every elements in the ArrayList, for each elements in the HashMap.

For example,

     label
  ----------
  | test1  |
  ----------
  | test2  |
  ----------



    label2
  ----------
  | test1  |
  ----------
  | test2  |
  ----------

I saw a lot of example for an HashMap but didn't find one for my case.

How can I do this ?

Thanks,

lschin
  • 6,745
  • 2
  • 38
  • 52
Zeym
  • 239
  • 1
  • 2
  • 5

2 Answers2

28
<s:iterator value="map">
  <h3><s:property value="key" /></h3>
  <table>
  <s:iterator value="value">
    <tr><td><s:property /></td></tr>
  </s:iterator>
  </table>
</s:iterator>

The iterator of a map is Map.Entry which gets put on the value stack and has two accessors, getKey() and getValue(). Iterate over Entry printing the key, then iterate over the values printing the value. (The list item gets put on top of the value stack so s:property just prints the top.)

NKijak
  • 1,174
  • 9
  • 19
  • What if I need to iterate over an specific map item instead of every map value? Lets say I'm trying to iterate over map[some_other_variable] list? – jpaoletti Aug 23 '13 at 13:05
  • 3
    The tags use OGLN which is a simple language with a guide available at http://commons.apache.org/proper/commons-ognl/language-guide.html – NKijak Aug 23 '13 at 13:24
0
Map<String,List<String>> mapVo=new  HashMap<String,List<String>>();
<s:iterator value="mapVo"  var="mapList" status="status">
 <table>
    <s:property value="#status.index"></s:property>
   <s:property value="key"></s:property>
   <s:iterator  value="mapList" var="item" status="rowstatus">
     <tr>
       item
     </tr>
   </s:iterator>
 </table>
</s:iterator>
P Rajesh
  • 326
  • 1
  • 2
  • 11