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

查看:39
本文介绍了如何在另一个 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.

只有事先知道这些变量的作用域,才能实现具体的功能需求.这样您就可以在直接访问范围映射时使用大括号表示法.您可以使用 在由多个变量组成的 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.

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

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