Iterate over param JSTL using foreach

703 views Asked by At

I have list of request parameters that the key is an integer, like

0=hello&1=by&2=sss .......

I want to iterate over this list of parameters using foreach:

<c:forEach var="i" begin="0" end="${fn:length(param)-1}" >
  <c:out value="${param['i']}"/>
</c:forEach>
1

There are 1 answers

2
alfreema On BEST ANSWER

OK, see if this trick works:

<c:forEach var="i" begin="0" end="${fn:length(param)-1}" >
    <c:set var="iString" value="t${i}"/>
    <c:out value="${param[fn:substringAfter(1, iString, fn:length(iString))]}"/>
</c:forEach>

1) The c:set statement converts i from a number to a string of the form "t0, t1, t2 .."

2) Now strip off the first character, and substringAfter returns a string containing just the number.


It's super close. I believe all you need to do is remove the apostrophes:

From:

${param['i']}

To:

${param[i]}

With the apostrophes you are looking for a parameter named "i" instead of a parameter named "0".