将字符串与变量的值组合为EL中另一个变量的名称 [英] Combining a string with the value of a variable to be the name of another variable in EL
问题描述
我想做这样的事情:
<display:table name="${summary${index}}">
但是它不起作用,将引发异常:"$ {summary $ {selChrm}}"包含无效的表达式. 我希望用户使用下拉列表选择一个选项并将其放入变量索引中.然后,该表将显示相应的javabean对象列表,即.名为summary01,summary02等.我该怎么做?预先感谢.
But it does not work throws exception: "${summary${selChrm}}" contains invalid expression(s). I would like the user to use a drop down list to choose a selection and put in the variable index. Then the table will display the corresponding list of javabean objects, ie. named summary01, summary02, etc. How can I do that? Thanks in advance.
更新:谢谢.问题在于嵌套的EL.我尝试使用
Update: Thanks guys. The problem is with the nested EL. I tried to use
<display:table name="summary${index}">
但是表中没有任何显示,因为summary01是变量名.如果我对变量名进行硬编码,它将起作用:
But nothing shows in the table since summary01 is the variable name. If I hard code the variable name, it will work:
<display:table name="${summary01}">
因此,我该如何嵌套EL?如果不是在JSTL中,我如何实现用户可以使用下拉列表确定要在表中显示哪些内容的行为?再次感谢.
Therefore, how can I do nested EL? If it is not possibly in JSTL, how can I achieve the behavior that user can use a dropdown list to determine what contents to be display in the table? Thanks again.
推荐答案
您不能嵌套EL表达式.但是,您可以将它们串联起来.
You cannot nest EL expressions. You can however just concatenate them.
<display:table name="${summary}${index}">
或者,如果${summary}
实际上不是任何范围内的对象,请执行
Or, if ${summary}
is actually not an object in any of the scopes, do
<display:table name="summary${index}">
更新:好的,您实际上需要访问${summary01}
.如果您事先知道范围,则可以使用大括号表示法访问它.想象一下,它的请求范围很广,您可以按以下方式纯"访问它:
Update: OK, you actually need to access ${summary01}
. If you know the scope beforehand, then you can access it using the brace notation. Imagine that it's request scoped, you could access it "plain" as follows:
<display:table name="${requestScope['summary01']}">
但是实际上您也可以在花括号中使用另一个EL变量(只需将其取消引号即可):
But you can actually also use another EL variable in the braces (just unquote it):
<display:table name="${requestScope[summary]}">
因此,我们想将其设置在某个地方. JSTL c:set
标签非常适合于此:
So, we'd like to set it somewhere. The JSTL c:set
tag is perfectly suitable for this:
<c:set var="summary" value="summary${index}" />
这篇关于将字符串与变量的值组合为EL中另一个变量的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!