簡體   English   中英

使用JSTL迭代Hashmap / ArrayList

[英]Iterate the Hashmap/ArrayList using JSTL

我有以下數組:

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在名稱,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>

假設您的${xml}包含請求/會話的array屬性:

  • 您正在嘗試直接讀取Map的條目。 您應該開始從列表中讀取Map的元素。
  • 來自如何使用JSTL在HashMap中迭代ArrayList? ,則使用錯誤的語法來訪問地圖的當前條目值。 您應該在代碼中使用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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM