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.
Related
I have added some Http session variables in a Java class inside a loop, and each session variable contains a unique identifier as part of it's name.
I want to be able to read these variables from a loop on a Thymeleaf template, using the identifier which was set for each. Is this possible? And if so what syntax should I use to insert the id to the name of my session variable in the Thymeleaf template?
The name of each Http session variable is "Sid"+unique_id, as below.
<tr th:each="item : ${items}">
<td th:text="${item.id}"></td>
<td th:text="${item.name}"></td>
<td th:if="${item.expired != null
and session.Sid${item.id} == 'available'}">
</tr>
You can do this using preprocessing.
<tr th:each="item : ${items}">
<td th:text="${item.id}"></td>
<td th:text="${item.name}"></td>
<td th:if="${item.expired != null and session.Sid__${item.id}__ == 'available'}">??<td>
</tr>
(It works, but in general I wouldn't recommend this kind of structure. I think you should be using a map, or some other data structure built for pulling out data like this. Then you could do something like: session.sids[item.id] or session.sids.get(item.id))
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>
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).
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]" />