how to access an element of arraylist in jsp when index is a variable?

Shubham picture Shubham · Feb 27, 2013 · Viewed 10.4k times · Source

i m accessing an element in an arraylist. the problem arises as the index of the required element is a variable. can anyone plz help me?

eg <td><c:out value="${PARENT_MODULE[module.moduleId]}"> </c:out></td>

here module.moduleId is a variable.

i have already tried

<c:out value="${PARENT_MODULE[${module.moduleId}]}">

however this gives an error.

org.apache.jasper.JasperException: /WEB-INF/jsp/showModules.jsp(40,20) "${PARENT_MODULE[${module.moduleId}]}" contains invalid expression(s): javax.el.ELException: Error Parsing: ${PARENT_MODULE[${module.moduleId}]}

thanks

Answer

obourgain picture obourgain · Feb 27, 2013

As module.moduleId is inside an EL expression, it will be evaluated. So you don't need to enclose it in an additional ${}.

<c:out value="${PARENT_MODULE[module.moduleId]}"> will work.