I have an ArrayList
containing a HashMap
, and I am trying to iterate through that ArrayList
of HashMap
s using the Struts s:iterator
tag. I can iterate through the List
without problems, but I cannot get it to iterate through the entries of the map. So far I've got this:
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class mapTest extends ActionSupport {
public List<Map> listmap;
public String execute() {
listmapObject = new ArrayList();
Map map = new HashMap();
map.put("stationId", "alpha");
map.put("stationName", "USA");
map.put("CustomerName", "charlie");
Map map2 = new HashMap();
map2.put("stationId", "Beta");
map2.put("stationName", "UK");
map2.put("CustomerName", "johnny");
listmapObject.add(map);
listmapObject.add(map2);
return SUCCESS;
}
}
I need the result like the below Java code equivalent using struts s:iterator
<table>
<thead>
<th> <Station id> </th>
<th> station Name </th>
<th> Name </th>
</thead>
<s:iterator value="listmapObject">
<tr>
<td> ((HashMap) listmapObject.get(i)).get("stationId") </td>
<td> ((HashMap) listmapObject.get(i)).get("stationName") </td>
<td> ((HashMap) listmapObject.get(i)).get("CustomerName") </td>
</tr>
</s:iterator>
</table>
Thanks for everyone for helping me
I am not very familiar with the JSP component of Struts, but I think the following should work:
<s:iterator value="listmapObject">
<tr>
<td><s:property value="[0]['stationId']" /></td>
<td><s:property value="[0]['stationName']" /></td>
<td><s:property value="[0]['CustomerName']" /></td>
</tr>
</s:iterator>
According to the documentation, the [0]
means the list item, so in this case your HashMap
.