I'm iterating through a List<SomeObject>, but I want only to print out certain attributes of SomeObject depending on the attributes specified in another List. So far, I have got it. What I'm not sure, how to use the item in the List as the attribute of SomeObject that I want to print out.
<s:iterator value="theList" var="listItem" status="theListCount">
<s:iterator value="listOfAttributes" var="attr" status="attrListCount">
Attribute=<s:property value="#attr"/> Value=<s:property value='#listItem.[%{#attr}]'/>
</s:iterator>
</s:iterator>
No need to force OGNL in the part of the expression
<s:iterator value="theList" var="listItem" status="theListCount">
<s:iterator value="listOfAttributes" var="atr" status="attrListCount">
Attribute=<s:property value="#atr"/> Value=<s:property value="#listItem[#atr]"/>
</s:iterator>
</s:iterator>
First of all #attr is a reserved keyword, so don't use it as a variable value. And use [] notation to reference property of the object.
<s:iterator value="theList" var="listItem" status="theListCount">
<s:iterator value="listOfAttributes" var="atrbt" status="attrListCount">
Attribute=<s:property value="#atrbt"/> Value=<s:property value="#listItem[#atrbt]"/>
</s:iterator>
</s:iterator>
Related
I have Array list called matrixList, on which i am iterating. I want to display different value if return value from getStatus() method equals to value "GREEN" .For some reason i am getting error
"target is null for method equals"
<s:iterator status="stat" value="matrixList">
<tr>
<s:if test="%{#status.equals('GREEN')}">
THIS IS green
</s:if>
<td class="R0C1"><s:property value="status"/></td>
<td class="R0C1"><s:property value="releaseTarget"/></td>
</tr>
</s:iterator>
Any idea what I'm missing?
Omit # if status is on the value stack (as opposed to a named value in the stack context):
<s:if test="%{status.equals('GREEN')}">
If <s:property value="status"/> works why reference it differently in the <s:if> tag?
The property name used in the if statement should be stat and not status.
<s:if test="%{#stat.equals('GREEN')}">
This is because the name of the variable in the iteration scope is defined as stat in the status = "stat" statement.
I have a ArrayList which contains a list of Object array Eg: new Object ['a','b','c']
The list is a member variable of data object.
Now, how can I access each 3 element data while iterating it in the s:iterator tag loop
<s:iterator value="data.list" status="cnt" var="searchList">
<s:property value="searchList[0]"/>
<s:property value="searchList[1]"/>
<s:property value="searchList[2]"/>
</s:iterator>
I tried above code, but it displayed nothing.
<s:iterator value="data.list" status="cnt" var="searchList">
<s:property value="#searchList[0]" />
<s:property value="#searchList[1]" />
<s:property value="#searchList[2]" />
</s:iterator>
<s:iterator value="data.list" status="cnt" var="searchList">
<s:property value="#searchList"/>
</s:iterator>
Use the property directly by the index
<s:iterator value="data.list" status="cnt" var="searchList">
<s:iterator begin="0" end="data.list.length" var="idx">
<s:property value="[1].data.list[%{#idx}]}"/>
</s:iterator>
</s:iterator>
I have nested iteration in Struts2:
<s:iterator var="gift" value="user.gift">
<s:iterator value="giftItems">
<s:property value="type"/>
</s:iterator>
</s:iterator>
I want to have up to 10 type only. How to break at some point in Struts2 Iteration? I can have a counter variable, but don't know to break in iteration.
Thanks.
You could use the begin and end attributes if you know when to break out before begining the iteration. If the break has to be calculated within the iteration, set a variable and check its value using each time before doing the iteration. In the later case, the loop will technically not 'break', but the effect would be the same.
<s:set var = "breakLoop" value = "%{false}" />
<s:iterator....>
<s:if test = "!#breakLoop">
//process here and change the var to true based on some codition
<s:set var = "breakLoop" value = "%{true}"/>
</s:if>
</s:iterator>
AFAIK, you can't break. Your iteration will continue until the end of the List.
But (if you don't choose to limit the List from Java side, before reaching the JSP),
you can use the index (0-based) or count (1-based) property of the StatusIterator object provided by the <s:iterator /> to limit the rendering to ten elements:
To limit to 10 types per giftItem:
<s:iterator var="gift" value="user.gift">
<s:iterator value="giftItems" status="ctr">
<s:if test="#ctr.index < 10">
<s:property value="type"/>
</s:if>
</s:iterator>
</s:iterator>
To limit to 10 types at all (as requested in comments):
<s:set var="rowPrinted" value="0"/>
<s:iterator var="gift" value="user.gift" >
<s:iterator value="giftItems" >
<s:if test="#rowPrinted < 10">
<s:property value="type"/>
<s:set var="rowPrinted" value="%{#rowPrinted + 1}"/>
</s:if>
</s:iterator>
</s:iterator>
As you can see, you can simply declare a 0-based variable, check it to be under the limit, then increment it (as you would do in Java).
I have lists of objects named allAlbums and allPhotos. Now I want to show all photos in each album so I used the following method. My code is
<s:iterator value="allAlbums">
<s:iterator value="allPhotos">
<s:if test="%{#allAlbums.albumid == #allPhotos.albumid}">
<s:property value="photourl"/>
</s:if>
</s:iterator>
</s:iterator>
But it is not working. Any suggestion what am I doing wrong?
Make sure that both of your object have appropriate getters/setters, then you can use feature provided by OGNL called projection.
<s:iterator value="allAlbums" var="album">
<s:iterator value="allPhotos.{? #this.albumid == #album.albumid}">
<s:property value="photourl"/>
</s:iterator>
</s:iterator>
You are referencing wrong object. Define a var attribute for the records you would reference and reference it instead.
<s:iterator var="album" value="allAlbums">
<s:iterator var="photo" value="allPhotos">
<s:if test="#album.id == #photo.albumId">
<s:property value="photoUrl"/>
</s:if>
</s:iterator>
</s:iterator>
I have a list of Strings that are attribute names and a Map.
I'm trying to access a model(ex.project) in the map using attribute name in string list.
Here is what I have now.
<s:iterator value="theMap" var="element">
<tr>
<s:iterator value="attributeList" var="attrName">
<td><p><s:property value="#element.Project.#attrName" /></p></td>
</s:iterator>
</tr>
</s:iterator>
If I hard code the attribute name it works fine:
<td><p><s:property value="#element.Project.projectName" /></p></td>
Any advice is appreciated.
Using OGNL <s:property value="#element.Project[#attrName]" />