1

I want to know how to pass the code below to jstl changing the name according to index.

List<Map> inventariozonas = new ArrayList<Map>();
for(int i = 1; i < 20; i++){
  Map r3 = new HashMap();
  r3.put("puntoventa", "puntoventa"+i);
  inventariozonas.add(r3);
}

I've been trying to get values in HTML by JSTL but I don't know how to get them dynamically due every variable has a different name.

With the code below just repeat the same value.

<c:forEach items="${inventariozonas}" var="r">
  <tr>
    <td>${r.puntoventa1}</td>
  </tr>
</c:forEach>

Is there any possibility to do something like this:

<c:forEach items="${inventariozonas}" var="r">
  <tr>
    <td>${r.puntoventa+index}</td>
  </tr>
</c:forEach>
azro
  • 53,056
  • 7
  • 34
  • 70
xiul2194
  • 25
  • 2

1 Answers1

1

You can try this way :

<c:forEach items="${inventariozonas}" var="r" varStatus="vs">
  <tr>
    <td>${r['puntoventa' + vs.index]}</td>
  </tr>
</c:forEach>
  1. First to get the index you can use varStatus, for more details read this How to get a index value from foreach loop in jstl
  2. to get the value from a map you can use r['key'], for more details read this Get value from hashmap based on key to JSTL.

If you want to loop over the element of each map, then would suggest this way instead :

<c:forEach items="${inventariozonas}" var="myMap">
  <c:forEach items="${myMap}" var="entry">
     <tr>
       <td>${entry.value}</td>
     </tr>
  </c:forEach>
</c:forEach>
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140