简体   繁体   English

如何迭代嵌套映射<c:forEach>

[英]How to iterate over a nested map in <c:forEach>

I've a Map in a bean as follows: 我在bean中有一个Map如下:

public class TaskListData {
    private Map<String, String[]> srcMasks = new HashMap<String, String[]>();
    private Map<Integer, Map<String, String[]>> ftqSet = new HashMap<Integer, Map<String, String[]>>();

    public void setFTQSet(Integer ftqid, String[] src, String[] masks) {  
        srcMasks.put("srcDir", src);
        srcMasks.put("masks", masks);
        ftqSet.put(ftqid, srcMasks);
    }

This ftqSet fits in below datastructure: ftqSet适合以下数据结构:

feedId = "5",
feedName = "myFeedName",
ftqSet => {
            1 => {
                    srcDirs = ["/path/string"],
                    masks = ["p.txt", "q.csv"]
                 }
            2 => { ...
                 }
          }, ...

In my test JSP file I've been trying to access the data using <c:forEach> : 在我的测试JSP文件中,我一直在尝试使用<c:forEach>访问数据:

<c:forEach items="#{bean.ftqSet}" var="f">
    this text does not print
    ${f.feedId}
</c:forEach>

But it's not outputting ${f.feedId} . 但它没有输出${f.feedId} Why would this be? 为什么会这样? How would I access this structure's individual elements so I can create a nice table? 我如何访问这个结构的各个元素,以便创建一个漂亮的表?

Each iteration of Map in a c:forEach gives a Map.Entry instance which in turn has getKey() and getValue() methods. c:forEachMap每次迭代都会给出一个Map.Entry实例,该实例又包含getKey()getValue()方法。 It's similar to doing for (Entry entry : map.entrySet()) in plain Java. 它与普通Java中的for (Entry entry : map.entrySet())相似。

Eg 例如

<c:forEach items="#{bean.map}" var="entry">
    <h:outputText value="Key: #{entry.key}, Value: #{entry.value}" /><br />
</c:forEach>

In case of a Map<Integer, Map<String, String[]>> the #{entry.value} returns a Map<String, String[]> , so you need to iterate over it as well: 如果是Map<Integer, Map<String, String[]>> #{entry.value}返回Map<String, String[]> ,所以你需要迭代它:

<c:forEach items="#{bean.map}" var="entry">
    <h:outputText value="Key: #{entry.key}, Values:" />
    <c:forEach items="#{entry.value}" var="nestedentry">
        <h:outputText value="Nested Key: #{nestedentry.key}, Nested Value: #{nestedentry.value}" />
    </c:forEach><br />
</c:forEach>

But in your case, the #{nestedentry.value} is actually a String[] , so we need to iterate over it again: 但在你的情况下, #{nestedentry.value}实际上是一个String[] ,所以我们需要再次迭代它:

<c:forEach items="#{bean.map}" var="entry">
    <h:outputText value="Key: #{entry.key}, Values:" />
    <c:forEach items="#{entry.value}" var="nestedentry">
        <h:outputText value="Nested Key: #{nestedentry.key}, Nested Values: " />
        <c:forEach items="#{nestedentry.value}" var="nestednestedentry">
            <h:outputText value="#{nestednestedentry}" />
        </c:forEach><br />
    </c:forEach><br />
</c:forEach>

By the way, this ought to work with rich:dataList as well. 顺便说一句,这应该与rich:dataList一起使用。

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

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