使用s:iterator迭代地图列表 [英] Iterate over a List of Maps using s:iterator
问题描述
我试图通过使用s:iterator迭代地图列表。我可以遍历列表而没有任何问题,但是我无法让它遍历地图的条目。到目前为止,我已经得到了:
I'm trying to iterate through a List of Maps using s:iterator. I can iterate through the List without problems, but I can not get it to iterate through the entries of the map. So far I've got this:
[..]
<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>
对于每个条目,但不是第二个迭代器,所以我想我在做值属性有问题。你可以帮我吗?
for each entry, but it is not going throug the second iterator, so I suppose I'm doing something wrong with the value attribute. Can you help me with it?
谢谢
Jose
推荐答案
这是一个通过地图列表循环的演示:
Here is a demo that loops through lists of map:
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对象,每次迭代都是有用的,如果我们想知道当前的迭代。
Note the inner iterator is context sensitive it will use the last value pushed onto the stack. The status attribute gives us a IteratorStatus object each iteration which is useful if we want to know the current iteration.
这篇关于使用s:iterator迭代地图列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!