Struts2迭代器标记 - 从对象列表中访问特定对象值 [英] Struts2 iterator tag - access specific object value from list of objects

查看:129
本文介绍了Struts2迭代器标记 - 从对象列表中访问特定对象值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从对象列表中访问特定对象的属性。

Is there a way to access an attribute of specific object from list of objects.

我有一个实验室列表,每个Lab对象都有多个属性。使用标签,我们可以从实验室列表中访问Lab1对象的attribute1的值吗?

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 object有一个名为 labname 的属性,其值为BP,而另一个实验对象的 labname 为A1c 。

现在,如果我想使用 labname 访问lab对象的 labvalue 属性BP我该如何实现呢?

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>

根本不需要迭代器。

您还可以迭代投影 / 选择的列表,顺便说一句;)

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆