如何在另一个EL表达式中嵌套一个EL表达式 [英] How to nest an EL expression in another EL expression

查看:289
本文介绍了如何在另一个EL表达式中嵌套一个EL表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写JSP/JSTL,并且试图遍历数据库中的多个项目.

I'm writing JSP / JSTL, and I'm trying to iterate over several items in a database.

我当前在数据库中有三列,分别是${image1}${image2}${image3}.我正在尝试使用以下代码为他们打印出信息:

I currently have three columns in the database, ${image1}, ${image2} and ${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?

推荐答案

您不能嵌套这样的EL表达式.

You can't nest EL expressions like that.

仅在事先知道这些变量的范围的情况下,才能实现具体的功能要求.这样,您可以在直接访问范围映射时使用花括号符号.您可以使用<c:set>在EL范围内创建由多个变量组成的新字符串变量.您可以使用例如${requestScope}访问请求范围变量的映射.

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. You can use e.g. ${requestScope} to access the mapping of request scoped variables.

因此,假设您确实已将这些变量存储在请求范围内,则应这样做:

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>

对于会话范围,请改用${sessionScope}映射.

For the session scope, use the ${sessionScope} map instead.

  • Our EL wiki page
  • String Concatenation in EL

这篇关于如何在另一个EL表达式中嵌套一个EL表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆