有没有办法从对象列表中访问特定对象的属性。
我有一个实验室列表,每个实验室对象都有多个属性。使用标签,我们可以从实验室列表中访问Lab1对象的attribute1值吗?
假设:我的一个 Lab
对象有一个名为 labname
的属性,其值为“BP”,另一个实验室对象的 labname
为“A1c”。
现在,如果我想以 labname
作为“BP”访问 Lab 对象的 labvalue
属性,我该如何实现呢?
最佳答案
如果您不想使用 map ,那更容易,那么您可以利用 OGNL's List Selection 特点:
Selecting From Collections
OGNL provides a simple way to use an expression to choose some elements from a collection and save the results in a new collection. We call this "selection," from the database term for choosing a subset of rows from a table. For example, this expression:
listeners.{? #this instanceof ActionListener}
returns a list of all those listeners that are instances of the ActionListener class.
[...]
然后在您描述的情况下,如果您只想过滤列表中 labname
属性等于“BP”的元素,则为:
<span>
labvalue attribute for the (first, if any) laboratory with labname="BP" is :
<s:property value="labsList.{^ #this.labname == 'BP' }[0].labvalue" />
</span>
根本不需要迭代器。
您还可以迭代投影/选定列表,顺便说一句;)
<span>
All labvalue attributes for all the laboratories with labname="BP" are :
<s:iterator value="labsList.{? #this.labname == 'BP' }" >
<s:property value="labvalue" />
</s:iterator>
</span>
享受
关于list - Struts2 迭代器标签 - 从对象列表中访问特定对象值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23664009/