list - Struts2 迭代器标签 - 从对象列表中访问特定对象值

标签 list jsp struts2 iterator ognl

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

我有一个实验室列表,每个实验室对象都有多个属性。使用标签,我们可以从实验室列表中访问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/

相关文章:

java - 列表中项目的随机分布,最多可达最大计数

jsp - javax.servlet.jsp.PageContext 无法解析为类型

java - 如何在给定 JSP 中的键的情况下获取 Map 值?

jQuery 复选框值以逗号分隔列表

python - 在另一个列表中附加一个列表

java - 值未显示在 c :foreach loop 中

java - 如何从 ajax post 重定向到 servlet 到 jsp?

spring - 为什么我在 tomcat 6 中得到 "HTTP Status 503 - This application is not currently available"?

java - 即使在无效后 session 值仍然可见

r - Advanced R 中修改列表的示例