[英]Nested JSTL variable name? is it possible?
I am trying to use a JSTL variable inside another variable. 我正在尝试在另一个变量内使用JSTL变量。 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"). 因此,基本上我将一个列表传递给显示表标记“ name =“ $ {actionBean.list}”。然后,我使用显示表标记的id属性遍历列表对象(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. 这是通过for:each来完成的,它为我提供了所有对象属性名称。 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. 如果您想知道为什么要实现它而不是仅仅使用列,那是因为我使用的是DynaBean对象并且属性是动态的。
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} 我需要:1)计算columnName并获取一个值(假设我得到“ price”字符串)2)将这个值连接到我们的变量中($ {resultsRow.price})3)执行$ {resultsRow.price}
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>
From the el
tag info page : 在el
标签信息页面上 :
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. 您可以使用所谓的括号符号[]来通过动态名称访问属性,通过包含句点的键来访问映射值,使用名称/键(它们本身是Java中的保留文字)以及通过以下方式访问数组或列表项:指数。
${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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.