Struts2 迭代器标签 - 从对象列表中访问特定对象值 [英] Struts2 iterator tag - access specific object value from list of objects
问题描述
有没有办法从对象列表中访问特定对象的属性.
Is there a way to access an attribute of specific object from list of objects.
我有一个实验室列表,每个实验室对象都有多个属性.使用标签,我们可以从实验室列表中访问 Lab1 对象的属性 1 的值吗?
I've a List of Labs and each Lab object has multiple attributes. Using tag, can we access value of attribute1 of Lab1 object from the list of labs?
假设:我的一个 Lab
对象有一个名为 labname
的属性,其值为BP",而另一个实验室对象的 labname
为"A1c".
现在,如果我想以 labname
为BP"访问实验室对象的 labvalue
属性,我该如何实现?
Let's say: one of my Lab
object has an attribute called labname
with value "BP" and another lab object has labname
of "A1c".
Now, if I want to access the labvalue
attribute of lab object with labname
as "BP" how do I achieve it?
推荐答案
如果你不想使用地图,那更容易,那么你可以利用 OGNL 的列表选择功能:
If you don't want to use a Map, that's easier, then you can exploit the OGNL's List Selection feature:
从集合中选择
OGNL 提供了一种简单的方法来使用表达式来选择一些集合中的元素并将结果保存在新集合中.我们称之为选择",来自用于选择子集的数据库术语表中的行数.例如,这个表达式:
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}
返回所有监听器的列表,这些监听器是ActionListener 类.
returns a list of all those listeners that are instances of the ActionListener class.
[...]
那么在您描述的情况下,如果您只想过滤 labname
属性等于BP"的列表元素,它将是:
Then in the case you described, if you want to filter only the element of the list with the labname
attribute equals to "BP" it would be:
<span>
labvalue attribute for the (first, if any) laboratory with labname="BP" is :
<s:property value="labsList.{^ #this.labname == 'BP' }[0].labvalue" />
</span>
根本不需要迭代器.
您还可以迭代 projected/selected 列表,顺便说一句;)
You can also iterate a projected / selected list, btw ;)
<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>
享受
这篇关于Struts2 迭代器标签 - 从对象列表中访问特定对象值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!