【问题标题】:Iterate the Hashmap/ArrayList using JSTL使用 JSTL 迭代 Hashmap/ArrayList
【发布时间】:2013-03-31 21:33:05
【问题描述】:

我有以下数组:

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>

【问题讨论】:

    标签: java arraylist hashmap jstl


    【解决方案1】:

    假设您的${xml} 包含请求/会话的array 属性:

    • 您正试图直接读取Maps 的条目。您应该开始阅读列表中Maps 的元素。
    • 来自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>
      

    【讨论】:

    • 感谢您的全面解释!
    • @LuiggiMendoza 我也有类似的问题here。看看你能不能帮助我。任何帮助将不胜感激。
    • @SSH 我几个小时前读过它,我赞成当前发布的答案,因为它解决了你的问题。
    猜你喜欢
    • 2011-01-08
    • 2014-10-19
    • 2012-09-01
    • 2016-09-08
    • 1970-01-01
    • 2012-03-04
    • 2018-02-13
    • 2011-12-02
    • 2017-08-12
    相关资源
    最近更新 更多