如何使用JSP EL动态访问请求参数?

时间:2021-04-29 23:58:52

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>

4 个解决方案

#1


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

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

不幸的是,+对普通Java中的字符串不起作用,所以

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

doesn't work ;-(

不起作用;-(

#2


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

J2EE 1.4文档的表达式语言文档部分中有一个隐式对象列表。你在找param。

#3


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:

您只需要使用“方括号”表示法。通过使用JSTL 标记,您可以生成正确的参数名称:

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

#4


Short answer:

${param.item_[count.index]}

#1


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

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

不幸的是,+对普通Java中的字符串不起作用,所以

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

doesn't work ;-(

不起作用;-(

#2


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

J2EE 1.4文档的表达式语言文档部分中有一个隐式对象列表。你在找param。

#3


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:

您只需要使用“方括号”表示法。通过使用JSTL 标记,您可以生成正确的参数名称:

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

#4


Short answer:

${param.item_[count.index]}