Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 317
mvk Avatar asked Aug 09 '12 13:08

mvk


1 Answers

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> 
like image 79
kapandron Avatar answered Oct 13 '22 16:10

kapandron