How to use the index variable of a JSTL forEach loop to access a map entry?

42,626

What number do you want to show? Is it index number of each map entry?

<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> 
Share:
42,626
Admin
Author by

Admin

Updated on May 15, 2020

Comments

  • Admin
    Admin almost 4 years

    With a forEach loop I'd like to create table cells (for a row) whereas each cell contains an input field of a form. The number of table cells is always fixed (12). That is actually no problem. However, here comes the challenge: the forEach should also enter a variable number of default values into the input fields that have to be obtained from a Map(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 doesn't show any value from the Map in the input fields. I guess the problem is that "number" is of type String and not Long. So I wonder if this problem can be solved without using scriptlets.