Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jsp

el

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.

like image 264
Ken Avatar asked Jun 28 '10 02:06

Ken


People also ask

How do you combine a string with a variable?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect. let myPet = 'seahorse'; console.

How do I concatenate a variable and a string in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator. $str1="My name is vignesh."

How do you concatenate a variable in JavaScript?

Use the addition (+) operator to concatenate a string with a variable, e.g. 'hello' + myVar . The addition (+) operator is used to concatenate strings or sum numbers.


3 Answers

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}" />
like image 154
BalusC Avatar answered Oct 07 '22 18:10

BalusC


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.

like image 31
Minutis Avatar answered Oct 07 '22 18:10

Minutis


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))}
like image 1
Pointy Avatar answered Oct 07 '22 16:10

Pointy