how to use struts <s:iterator> and <s:if> together? - 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>

Related

Struts2 nested iteration through Lists using dynamic variables

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>

s:iterator with collection of Object array

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>

If statement inside TreeMap in Struts 2

I have a TreeMap<Long,ArrayList<String>> that I need t print it in Struts2 jsp page. I have to add an If condition in such a way that If the content of ArrayList<String> contains certain character sequence I need to print it in one color and if does not have that character sequence I need to print it in different color.
In the below code, only else part is getting printed,
<s:set name="string1" value="check_Char" />
<s:iterator value="lgMap">
<h3>
<s:property value="key" />
</h3>
<table>
<s:iterator value="value">
<s:if test="%{<s:property />.indexOf(#string1)) == -1}">
<tr>
<td><font color="green"><s:property /></font> </td>
</tr>
</s:if>
<s:else>
<tr>
<td><font color="red"><s:property /></font></td>
</tr>
</s:else>
</s:iterator>
</table>
</s:iterator>
There is problem with the if statement i believe. Can one suggest how to reach the if part?
You can't nest Struts Tags.
Change this
<s:if test="%{<s:property />.indexOf(#string1)) == -1}">
to this
<s:if test="%{top.indexOf(#string1) == -1}">

How to compare two strings using Struts2 tags and OGNL?

I am trying to compare two values : one from session and anther from iterator
<s:iterator value="themes" status="currentRecord">
<s:if test="%{usertheme}) == %{themeName}">
<td align="center" bgcolor="red">
</s:if>
<s:else>
<td align="center" bgcolor="green">
</s:else>
</s:iterator>
But I am unable to compare my values, please can you tell me where I am doing mistakes ?
%{} should be put (if necessary) around all the statement, not in the middle.
For Strings you should use .equals, .equalsIgnoreCase, .contains, .indexOf etc... Not ==.
Change to this:
<s:iterator value="themes" status="currentRecord">
<s:if test="%{#session.usertheme.equalsIgnoreCase(themeName)}">
<td align="center" bgcolor="red">
</s:if>
<s:else>
<td align="center" bgcolor="yellow">
</s:else>
....
this works too:
<s:if test="#session.usertheme.equalsIgnoreCase(themeName)">
(Not an answer, but two suggestions, and I needed formatting; Andrea's answer is correct.)
For the sanity of yourself and those that follow, turn that chunk of JSP into a single line:
<s:iterator value="themes">
<tr>
<s:set var="currTheme" value="%{userTheme == themeName ? 'red' : 'green'}"/>
<td bgcolor="${currTheme}">Cell content</td>
</tr>
</s:iterator>
Consider using theme-named CSS instead of inline CSS and avoid it completely, roughly:
td.theme1 {
background-color: red;
}
td.theme2 {
background-color: green;
}
td.theme3 {
background-color: #daa520;
}
(Assuming themes named "theme1", "theme2", "theme3", but that's not relevant.)
<table class="themed-table">
<s:iterator value="themes">
<tr>
<td class="${themeName}">Cell content</td>
</tr>
</s:iterator>
</table>
It'd be nicer to move the style info "up" a level, e.g., table.theme1 td, but you get the idea. Doing so allows a lot of flexibility in where the theme info comes from and so on.
<!--name attribute inside select tag must be a variable in action class with getter/setter -->
<!-- test variable sets the value of selected item in action class -->
<select name="test">
<!-- name attribute could be anything you want but value attribute must be a model class variable-->
<s:set name="lead_string_LS_ID" value="MasterDataModel.string_LS_ID" />
<!-- value attribute must be a list to iterate, status (an instanceof IteratorStatus will be pushed into stack upon each iteration)or result -->
<!-- var Name used to reference the value pushed into the Value Stack (my list contain leadSource_String_Id)-->
<s:iterator value="leadSource_list" status="result" var="leadSource_String_Id">
<!--#lead_string_LS_ID is value taken from <set> tag above. Note # must be in-front of the name
leadSource_String_Id is element specified in var of <iterator> tag
-->
<s:if test='(#lead_string_LS_ID.equals(leadSource_String_Id))'>
<option value="<s:property value='leadSource_String_Id'/>" selected="selected">
<s:property value="leadSource_String_Name" />
</option>
</s:if>
<s:else>
<option
value="<s:property value='leadSource_String_Id'/>">
<s:property value="leadSource_String_Name" />
</option>
</s:else>
</s:iterator>
</select>

struts2 contains test in iterator tag

This is my code in jsp:
<s:iterator value="sources" status="sourceStatus">
<s:property value="sourcesCheck"/>
<s:property value="#sourceStatus.index"/>
<s:if test="%{sourcesCheck.contains(#sourceStatus.index)}">
true
</s:if>
<s:else>
false
</s:else>
</s:iterator>
sourcesCheck is an array I pass from my action populated with values 0,1,2,3,4,5. The results on my jsp show 0,1,2,3,4,5 for sourcesCheck, however, the test is always false. Why? I have tried with both int and String values for sourcesCheck. How does contains work? If index is not a String or int, what is it?
Use this code:
<s:iterator value="sources" status="sourceStatus">
<s:property value="sourcesCheck"/>
<s:property value="#sourceStatus.index"/>
<s:if test="%{#sourceStatus.index in sourcesCheck}">
true
</s:if>
<s:else>
false
</s:else>
</s:iterator>
Array doesn't have any methods to implement. Consider the java.util.List.
For example :
List<Integer> sourcesCheck = Arrays.asList(0, 1, 2, 3, 4, 5);
Compare by java.util.List.contains(Integer)
<s:if test="sourcesCheck.contains(#sourceStatus.index)">
</s:if>
I think it must be as follow
<s:if test="%{sourcesCheck.contains('#sourceStatus.index')}">
because you want to check a value not a variable.

Categories