2013-03-31 79 views
0

我有以下阵列:迭代HashMap中/ ArrayList的使用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> 

回答

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> 
    
+0

感谢您а全面的解释! –

+0

@GeorgeSharvadze不客气。 –

+0

@LuiggiMendoza我也有类似的问题[这里](http://stackoverflow.com/questions/22293665/how-to-show-multiple-tables-depending-on-key-in-the-map-using-jstl) 。看看你能否帮助我..任何帮助将不胜感激。 – 2014-03-10 21:46:45