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>
Related
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>
here is my JSP code.
<h3>All Records:</h3>
<s:iterator value="mainlist">
<fieldset>
<s:form action="test.action" method="GET">
<s:property value="questions"/><br/>
<s:checkboxlist list="list" name="op1"/>
<s:property value="answer"/><br/>
<s:submit value="end exam"></s:submit>
</s:form>
</fieldset>
</s:iterator>
here mainlist contains 2 questions with 4 options each. those 4 options are there is 'list'. I am trying to display those questions with options on web page. but nothing is displayed on the page, except 'All Records:'. what is the problem with the code ?
This is how it can be done using Iterator
<s:iterator value="list" >
<s:property value="columnName"/>
</s:iterator>
<c:forEach var="variable" items='${requestScope.attributeName}'>
<c:out value="${variable.attr}"></c:out>
</c:forEach>
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>
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.
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]" />