How to use the JSTL forEach loop index variable to access map records?

With a forEach loop, I would like to create table cells (for a row), while each cell contains a form input field. The number of table cells is always fixed (12). This is actually not a problem. However, the problem arises here: forEach must also enter a variable number of default values ​​in the input fields that must be received from the card (Long, Double).

This is my (simplified) attempt:

<c:forEach var="number" begin="1" end="12" >
  <td>
      <input type="text" value="${requestScope.aMapWithData[number]}" /> 
  </td> 
</c:forEach>

But this does not display the values ​​from the Map in the input fields. I think the problem is that the "number" is of type String and not Long. So I wonder if this problem can be solved without using scripts.

+5
source share
2 answers

? ?

<c:forEach items="${aMapWithData}" var="item" varStatus="status"> 
    <td> 
        <c:out value="${status.count}."/>  
        <input type="text" name="${item.key}" value="${item.value}" />  
    </td> 
</c:forEach> 
+7

<c:forEach items="${aMapWithData}" var="mapEntry">
   <c:set var="mapKey" value="${mapEntry.key}"></c:set>
   <c:set var="mapValue" value="${mapEntry.value}"></c:set>
</c:forEach>
0

All Articles