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

后端 未结 4 1208
Happy的楠姐
Happy的楠姐 2021-02-19 07:12

I want to do something like this:


But it does not work throws exception: \"${summary${selChrm

相关标签:
4条回答
  • 2021-02-19 07:32

    It worked for me.

    ${not empty marginLeft ? 'margin-left:'.concat(marginLeft) : ''}
    
    0 讨论(0)
  • 2021-02-19 07:48

    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.

    0 讨论(0)
  • 2021-02-19 07:52

    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}" />
    
    0 讨论(0)
  • 2021-02-19 07:52

    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))}
    
    0 讨论(0)
提交回复
热议问题