简体   繁体   中英

How do I dynamically access request parameters with JSP EL?

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>
<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 ;-(

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

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>

简短回答:

${param.item_[count.index]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM