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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/458697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:52:33  来源:igfitidea点击:

How do I dynamically access request parameters with JSP EL?

javajspjstlel

提问by ScArcher2

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.

我正在遍历项目列表,我想根据项目的索引获取请求参数。我可以使用如下所示的 scriptlet 轻松完成,但我想使用表达式语言。

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

采纳答案by Peter ?tibrany

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

不起作用;-(

回答by McDowell

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

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

回答by evnafets

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:set> 标记,您可以生成正确的参数名称:

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

回答by Triqui

Short answer:

简短的回答:

${param.item_[count.index]}