Search code examples
jspjstl

How do I access a dynamic list item in a forEach loop?


I have a forEach loop that looks like this:

<c:forEach var="getDetailsList" items="${getDetailsList}">

</c:forEach>

Each loop in the list looks like this:

id=1
desc=HELLO WORLD
203=1
211=0

id and desc are always the same but 203 and 211 would be dynamic numbers.

When I try to access 203 and 211 e.g.

<c:out value="${getDetailsList.203}"/>

I get a 500 error:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Is what I'm trying to do simply not achievable with JSTL?


Solution

  • If it is a key value pairs like a HashMap, you can always print them using key and values instead of accessing the variable directly.

    <c:forEach var="getDetailsList" items="${getDetailsList}">
        Key is ${getDetailsList.key}
        Value is ${getDetailsList.value}
    </c:forEach>
    

    If it is not a key value pair, please post the stacktrace to suggest more options.