开发者

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

开发者 https://www.devze.com 2023-01-04 18:22 出处:网络
I want to do something like this: <display:table name=\"${summary${index}}\"> But it does not work throws exception: \"${summary${selChrm}}\" contains inval开发者_Go百科id expression(s).

I want to do something like this:

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

But it does not work throws exception: "${summary${selChrm}}" contains inval开发者_Go百科id 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.


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


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.


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


It worked for me.

${not empty marginLeft ? 'margin-left:'.concat(marginLeft) : ''}
0

精彩评论

暂无评论...
验证码 换一张
取 消