简体   繁体   English

如何在另一个 EL 表达式中嵌套一个 EL 表达式

[英]How to nest an EL expression in another EL expression

I'm writing JSP / JSTL, and I'm trying to iterate over several items in a database.我正在编写 JSP/JSTL,并且我正在尝试遍历数据库中的几个项目。

I currently have three columns in the database, ${image1} , ${image2} and ${image3} .我目前在数据库中有三列, ${image1}${image2}${image3} I'm trying to use the following code to print out information for them:我正在尝试使用以下代码为他们打印信息:

<c:forEach begin="1" end="3" var="i">
  ${image${i}}
</c:forEach>

Is there any way I can make this work?有什么办法可以使这项工作?

You can't nest EL expressions like that.你不能像那样嵌套 EL 表达式。

You can achieve the concrete functional requirement only if you know the scope of those variables beforehand.只有事先知道这些变量的范围,才能实现具体的功能需求。 This way you can use the brace notation while accessing the scope map directly.这样您就可以在直接访问范围映射时使用大括号表示法。 You can use <c:set> to create a new string variable in EL scope composed of multiple variables.您可以使用<c:set>在由多个变量组成的 EL 范围内创建一个新的字符串变量。 You can use eg ${requestScope} to access the mapping of request scoped variables.您可以使用例如${requestScope}来访问请求范围变量的映射。

Thus, provided that you've indeed stored those variables in the request scope, then this should do:因此,如果您确实已将这些变量存储在请求范围内,则应该这样做:

<c:forEach begin="1" end="3" var="i">
    <c:set var="image" value="image${i}" />
    ${requestScope[image]}
</c:forEach>

For the session scope, use the ${sessionScope} map instead.对于会话范围,请改用${sessionScope}映射。

See also:也可以看看:

好吧,我能够通过将嵌套表达式放在括号 () 中来创建嵌套表达式,请参见下面的示例..

#{row.Dateapproved eq null ? 'true' : (row.Appealrange > (bindings.Appealvalidation.inputValue) ? 'true' : 'false') }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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