Combining a string with the value of a variable to be the name of another variable in EL
Asked Answered
A

4

14

I want to do something like this:

<display:table name="${summary${index}}">

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.


Update: Thanks guys. The problem is with the nested EL. I tried to use

<display:table name="summary${index}">

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

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.

Affined answered 28/6, 2010 at 2:31 Comment(1)
Please don't confuse JSTL with EL. JSTL is this taglib. EL are those ${} things: Expression LanguageGneiss
G
19

You cannot nest EL expressions. You can however just concatenate them.

<display:table name="${summary}${index}">

Or, if ${summary} is actually not an object in any of the scopes, do

<display:table name="summary${index}">

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']}">

But you can actually also use another EL variable in the braces (just unquote it):

<display:table name="${requestScope[summary]}">

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}" />
Gneiss answered 28/6, 2010 at 2:43 Comment(0)
W
3

Step one:

<c:set var="summary" value="${requestScope['summary'.concat(index)]}" />

Step two:

<display:table name="${summary}">

Simple as that :)

UPDATE as BalusC mentioned in comments, you need 3.0 Servlets to use this.

Waechter answered 25/1, 2012 at 21:47 Comment(1)
Note that this requires EL 2.2 (Servlet 3.0) which was relatively new at the moment this question was asked. In older EL versions you cannot call methods like that.Gneiss
L
1

This is a thing that really sucks about JSTL: there's no direct way to do that if you want to do it indide a JSTL expression. There's no string concatenation operator, in other words.

You can always do

 <something foo='${something} ${something_else}'/>

but that's not always useful.

In my world, I've implemented my own "concat" function, so I can say

$(foo:bar(mystuff:concat("something", something.else))}
Lindberg answered 28/6, 2010 at 2:43 Comment(0)
M
0

It worked for me.

${not empty marginLeft ? 'margin-left:'.concat(marginLeft) : ''}
Messier answered 14/10, 2017 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.