vol*_*vox 8 foreach jsp jstl hashmap
我Map在bean中有如下内容:
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);
}
Run Code Online (Sandbox Code Playgroud)
这ftqSet符合以下数据结构:
feedId = "5",
feedName = "myFeedName",
ftqSet => {
1 => {
srcDirs = ["/path/string"],
masks = ["p.txt", "q.csv"]
}
2 => { ...
}
}, ...
Run Code Online (Sandbox Code Playgroud)
在我的测试JSP文件中,我一直在尝试使用<c:forEach>以下方法访问数据:
<c:forEach items="#{bean.ftqSet}" var="f">
this text does not print
${f.feedId}
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
但它没有输出${f.feedId}.为什么会这样?我如何访问这个结构的各个元素,以便创建一个漂亮的表?
Bal*_*usC 17
的每次迭代Map中c:forEach给出了一个Map.Entry实例,它反过来又getKey()和getValue()方法.它类似于for (Entry entry : map.entrySet())用普通Java 做的.
例如
<c:forEach items="#{bean.map}" var="entry">
<h:outputText value="Key: #{entry.key}, Value: #{entry.value}" /><br />
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
在的情况下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>
Run Code Online (Sandbox Code Playgroud)
但在你的情况下,#{nestedentry.value}实际上是a 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>
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这应该rich:dataList也适用.