tags:

views:

5554

answers:

3

I'm looping through a list of items, and I'd like to get a request parameter based on the item's index. I could easily do it with a scriptlet as done below, but I'd like to use expression language.

<c:forEach var="item" items="${list}" varStatus="count">

   <!-- This would work -->
   <%=request.getParameter("item_" + count.index)%>

   <!-- I'd like to make this work -->
   ${param.?????}

</c:forEach>
+2  A: 
<c:set var="index" value="item_${count.index}" />
${param[index]}

Unfortunately, + doesn't work for strings like in plain Java, so

${param["index_" + count.index]}

doesn't work ;-(

Peter Štibraný
i corrected the reference to params. it's supposed to be param. But your answer gave me what I needed to get it working. Thanks!
ScArcher2
+2  A: 

There is a list of implicit objects in the Expression Language documentation section of the J2EE 1.4 documentation. You're looking for param.

McDowell
Thanks I looked it up and realized I was accessing the wrong thing. The main thing I was missing was the bracket syntax for accessing a property.
ScArcher2
+1  A: 

You just need to use the "square brackets" notation. With the use of a JSTL <c:set> tag you can generate the correct parameter name:

<c:forEach var="item" items="${list}" varStatus="count">
  <c:set var="paramName">item_${count.index}</c:set>
  ${param[paramName]}
</c:forEach>
evnafets