2

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

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?

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?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Gopi
  • 620
  • 8
  • 16

2 Answers2

2

If you don't want to use a Map, that's easier, then you can exploit the OGNL's List Selection feature:

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.

[...]

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>

with no need of iterators at all.

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>

Enjoy

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
0

Hope this will help

Give the list to the value attribute in s:iterator Define the object name in the var attribute

<s:iterator value="labsList" status="mbrindex" var="lab">
<s:property value="%{#lab.id}" />
arbiter
  • 29
  • 1
  • 5
  • Hi..thanks for your reply.. but, this one is still not solving my issue. 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? – Gopi May 14 '14 at 20:23
  • Do an and do the rest of the logic in that if condition. – arbiter May 14 '14 at 20:42
  • This way if I've 20 lab objects and need only 2 of them , I need to iterate more than 2 times through the list. I don't want to do that and would like to know if there is a way to directly access the list for specific object. – Gopi May 14 '14 at 20:52
  • I think there is no direct way to access something from a list using iterator tag. So, the alternative I found is push the list elements to a HashMap with Object name as HashMap key and from tag I can access directly required object's attributes. – Gopi May 14 '14 at 21:51