Search code examples
javajspjstl

How to access java map object in JSP


I have following loop in my jsp page:

    <c:forEach var="i" begin="1" end="${ toLvvt }" step="1">
      <c:set var="mapKey">${to.id}-${record.rId}-${record.opjakso}</c:set>
        <c:if test="${MyMap[mapKey].v1s eq true}">
           ...do something...
        </c:if>>

problem here is that I want to use var i fetching map object value (above v1s) like this: v${i}s but this is wrong.

I have map Map<string, Object>. Object has boolean properties v1s, v1k, v2s, v2k...


Solution

  • Just create it the same way as you created mapKey.

    E.g.

    <c:forEach var="i" begin="1" end="${toLvvt}" step="1">
      <c:set var="mapKey" value="${to.id}-${record.rId}-${record.opjakso}" />
      <c:set var="property" value="v${i}s" />
      <c:if test="${MyMap[mapKey][property]}">
        ..
      </c:if>
    </c:forEach>
    

    (note that I simplified the one and other)