简体   繁体   English

使用s:iterator在Struts2标签中解析Hashmap的ArrayList

[英]Parsing ArrayList of Hashmap in Struts2 tag using s:iterator

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. 我有一个包含HashMapArrayList ,并且我尝试使用Struts s:iterator标记s:iterator HashMapArrayList I can iterate through the List without problems, but I cannot get it to iterate through the entries of the map. 我可以毫无问题地遍历List ,但无法遍历地图的条目。 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 我需要使用struts s:iterator的以下类似Java代码的结果

<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: 我对Struts的JSP组件不是很熟悉,但是我认为以下应该起作用:

 <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 . 根据文档[0]表示列表项,因此在这种情况下为HashMap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM