如何在< c:forEach>中迭代嵌套的地图 [英] How to iterate over a nested map in <c:forEach>

查看:181
本文介绍了如何在< c:forEach>中迭代嵌套的地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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(mask,masks);
ftqSet.put(ftqid,srcMasks);

这个 ftqSet 适合在下面的数据结构:

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

在我的测试JSP文件我一直在使用< c:forEach> 来访问数据:

 < c:forEach items =#{bean.ftqSet}var =f> 
本文不打印
$ {f.feedId}
< / c:forEach>

但不会输出 $ {f.feedId} 。为什么会这样?我如何访问这个结构的单个元素,这样我就可以创建一个漂亮的表格了?

解决方案 Map c:forEach 中给出 Map.Entry 实例,该实例又具有 getKey() getValue() 方法。这与在普通Java中为(Entry entry:map.entrySet())做类似。



p>

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

如果 Map< Integer,Map< String,String [] >>< / code>< code>#{entry.value} 返回一个 Map< String,String []> code>,所以你需要迭代它:

 < c:forEach items =# bean.map}var =entry> 
< c:forEach items =#{entry.value}var =nestedentry>
< / c:forEach>< br />
< / c:forEach>

但在你的情况下,#{nestedentry.value} 实际上是一个 String [] ,所以我们需要再次迭代它:

 < c:forEach items =#{bean.map}var =entry> 
< c:forEach items =#{entry.value}var =nestedentry>
< c:forEach items =#{nestedentry.value}var =nestednestedentry>
< h:outputText value =#{nestednestedentry}/>
< / c:forEach>< br />
< / c:forEach>< br />
< / c:forEach>

顺便说一句,这应该与 rich:dataList


I've a Map in a bean as follows:

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:

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>:

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

But it's not outputting ${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. It's similar to doing for (Entry entry : map.entrySet()) in plain Java.

E.g.

<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:

<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:

<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.

这篇关于如何在&lt; c:forEach&gt;中迭代嵌套的地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆