【问题标题】:Iterate over a List of Maps using s:iterator使用 s:iterator 遍历 Map 列表
【发布时间】:2011-05-24 22:02:09
【问题描述】:

我正在尝试使用 s:iterator 遍历地图列表。我可以毫无问题地遍历列表,但我无法让它遍历地图的条目。到目前为止,我得到了这个:

[..]
<s:iterator value="records" status="recordsStatus" var="record">
        <s:if test="#recordsStatus.index ==0">
            <tr>
                <td colspan="*"></td>
            </tr>
        </s:if>
        <tr>
            <s:iterator value="record.entrySet()" status="fieldStatus">
            <td>
                <s:property value="key"/>/<s:property value="value"/>
            </td>
            </s:iterator>
        </tr>
    </s:iterator>
[..]

标签生成

<tr></tr>

对于每个条目,但它不会通过第二个迭代器,所以我想我在 value 属性上做错了。你能帮我吗?

谢谢

何塞

【问题讨论】:

    标签: map struts2 iterator


    【解决方案1】:

    这是一个循环遍历地图列表的演示:

    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(){
        listmap = new ArrayList();
        Map map = new HashMap();
        map.put("a", "alpha");
        map.put("b", "bravo");
        map.put("c", "charlie");
        listmap.add(map);
        Map map2 = new HashMap();
        map2.put("d", "delta");
        map2.put("e", "echo");
        map2.put("f", "foxtrot");
        listmap.add(map2);
        return SUCCESS;
      }
    }
    

    这里是渲染它的 JSP:

    <%@taglib prefix="s" uri="/struts-tags"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <body>
            <h1>Map Test</h1>
            <table>
                <thead>
                    <tr>
                        <th>List #</th>
                        <th>key</th>
                        <th>value</th>
                    </tr>
                </thead>
                <tbody>
                    <s:iterator value="listmap" status="stat">
                        <s:iterator>
                            <tr>
                                <th><s:property value="#stat.index"/></th>
                                <td><s:property value="key"/></td>
                                <td><s:property value="value"/></td>
                            </tr>
                        </s:iterator>
                    </s:iterator>
                </tbody>
            </table>
        </body>
    </html>
    

    注意,内部迭代器是上下文相关的,它将使用最后压入堆栈的值。 status 属性为我们提供了每次迭代的 IteratorStatus 对象,如果我们想知道当前的迭代,这很有用。

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多