我有以下数组:
List<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
[{name=BIRTH_DATE}, {catVal=07.11.2011}, {catStat=162}, {catVal=30.04.2011}, {catStat=108}, {CatVal=26.01.2011}]
我想使用JSTL在name,catVal和CatStat之间进行选择。我尝试了以下,但它不起作用。我怎样才能拿到钥匙?
<table border="1">
<c:forEach items="${xml}" var="map">
<tr>
<c:choose>
<c:when test="${map.key =='name'}">
<td>${map.name}</td>
</c:when>
<c:when test="${map.key == 'catVal'}">
<td>${map.catVal}</td>
</c:when>
<c:when test="${map.key == 'catStat'}">
<td>${map.catStat}</td>
</c:when>
</c:choose>
</tr>
</c:forEach>
答案 0 :(得分:0)
假设您的${xml}
包含请求/会话的array
属性:
Map
的条目。您应该开始阅读列表中Map
s。从How to iterate an ArrayList inside a HashMap using JSTL?开始,您使用了错误的语法来访问地图的当前条目值。您应该在代码中使用entry.value
:
<table border="1">
<c:forEach items="${xml}" var="itemMap">
<c:forEach items="${itemMap}" var="mapEntry">
<tr>
<td>
<c:choose>
<c:when test="${mapEntry.key == 'name'}">
<c:out value="Name:" />
</c:when>
<c:when test="${mapEntry.key == 'catVal'}">
<c:out value="Cat value:" />
</c:when>
<c:when test="${mapEntry.key == 'catStat'}">
<c:out value="Cat status:" />
</c:when>
</c:choose>
</td>
<td>
${mapEntry.value}
</td>
</tr>
</c:forEach>
</c:forEach>
</table>