1

I am trying to use a JSTL variable inside another variable. The code below will explain it better.

 <display:table id="resultsRow" name="${actionBean.list}" >                                                         

       <c:forEach items="${actionBean.anotherList}" var="columnName">

          <display:column sortable="true" property="${resultsRow.${columnName}"/>

       </c:forEach>

 </display:table>

So basically i am passing a list to the display table tag "name="${actionBean.list}". Then i use the id property of the display table tag to loop through the list objects (id="resultsRow").

Now for the column property attribute i need to access different properties inside the list object. This is being done using the for:each which provides me will all the object property names. If you are wondering why its implemented this was instead of just column, its because i am using a DynaBean Object and the properties are dynamic.

Question: Can i used nested vairable names like i did in display column tag property attribute?

I need to: 1) evaluate columnName and get a value ( Lets say i get "price" string) 2) concatenate this value to the our variable (${resultsRow.price}) 3) execute ${resultsRow.price}

user3752790
  • 61
  • 2
  • 6

2 Answers2

1

As mentioned in this answer, you should be able to use bracket notation to access 'dynamic' properties like so:

<display:table id="resultsRow" name="${actionBean.list}">                                                         

   <c:forEach items="${actionBean.anotherList}" var="columnName">

      <display:column sortable="true" property="${resultsRow[columnName]}"/>

   </c:forEach>

</display:table>
Community
  • 1
  • 1
Aaron Gotreaux
  • 273
  • 2
  • 4
  • 10
0

From the el tag info page:

You can use the so-called brace notation [] to access properties by a dynamic name, to access map values by a key containing periods, to use names/keys which are by itself reserved literals in Java and to access array or list items by index.

${sessionScope[dynamicName]}
${someMap[dynamicKey]}
${someMap['key.with.periods']}
${some['class'].simpleName}
${someList[0].name}
${someArray[0].name}

The above does essentially the same as

session.getAttribute(dynamicName);
someMap.get(dynamicKey);
someMap.get("key.with.periods");
some.getClass().getSimpleName();
someList.get(0).getName();
someArray[0].getName();
Community
  • 1
  • 1
fujy
  • 5,168
  • 5
  • 31
  • 50