struts 2 if to test value of iterator - struts2

Am using struts 2 iterator and if tag can anyone tell me how to test a particular value of iterator
Example: I got a List of type String with values
StrList = {"start","hello","hi","name","start","value",.."start",..}
I want to test for start if start is found i want to display HI and else Hello.

<s:if StrList.contains("start")>
Display HI
</s:if>
<s:else>
Display HELLO
</s:else>
Untested please see S2 web site tag lib documentation. Also assumed StrList was a public field, otherwise it should be called strList and have an appropriate getter/setter (as that would be following Java conventions).
Edit: I didn't read the question well enough... Use the var attribute of the iterator to give each iteration of the iterator a convinent handle, like so:
<s:iterator value='{"start","something","start","something else","start"}' var="curStr">
<s:if curStr.compareTo("start")> //or perhaps compareToIngnoreCase()
Display HI
</s:if>
<s:else>
Display HELLO
</s:else>
</s:iterator>

Related

Struts2 Iterate value from a textfield

I am trying to iterate over the textfield's value, for eg. 5. Is there a way to get the value?
I have a textfield with value 5:
<s:textfield
theme="simple"
cssClass="form-control"
name="instCount"
value="5"
style="width:25%;"
onkeyup="javascript:isNumber(this);"
/>
I need to iterate the value 5:
<s:if test='<s:property value="%{instCount}"/> > 0'>
<s:iterator value='<s:property value="%{instCount}"/>' var="count" status="countStatus">
</s:iterator>
</s:if>
You cannot nest JSP tags like that, and it appears that you haven't wrapped your head around OGNL or ELs in general, even though you use it correctly in the <s:property> tag.
Let's take a step back: what is this doing?
<s:property value="%{instCount}" />
It's referring to an action property named instCount.
How? Via the OGNL expression %{instCount}.
How does the <s:if> tag work? By evaluating an OGNL expression in the test property. Ah, OGNL expression, which we've already seen.
<s:if test='instCount > 0'>
How does the <s:iterator> tag work? By evaluating an OGNL expression in the value tag.
<s:iterator value='%{instCount}' etc...>
I would highly recommend taking some time to figure out the framework you're working in, even a minor reading of the documentation (and a basic understanding of how JSP works) will be highly beneficial, and avoid questions like this.

struts 2 Undefined attribute name "var"

I am trying to iterate over two 2D arrays Org_Positions_IdTitle and Org_Apps and print out a field but I keep getting the Attribute var invalid for tag iterator according to TLD also in my jsp page the var1 and var2 are underlined and it says on the left Undefined attribute name "var".
I would be so thankful if you can help me with that.
<s:iterator value="Org_Positions_IdTitle" var="arr1" >
<s:iterator value="Org_Apps" var="arr2" >
<s:if test="#arr1[0] == #arr2[1] ">
<s:property value="#arr1[1]" />
</s:if>
</s:iterator>
</s:iterator>
Which version of Struts 2 do you use?
If your version less then 2.1.x you should use id attribute, according to documentation: http://struts.apache.org/release/2.1.x/docs/iterator.html

How to use <s:iterator> or other struts tags to iterate part of a list

I have a list named projectList. I have dealt with the first 3 elements in the list in a different way, then I want to iterate the projectList from index=3 to the end.
How should I achieve this?
Either the solution with pure struts tags or a solution mixed with Javascript would be good.
I think this is what you need:
<s:iterator value="projectList" status="itstatus">
<s:if test="#itstatus.index > 3">
<!-- YOUR CODE -->
</s:if>
<s:else>
<!-- YOUR CODE -->
</s:else>
</s:iterator>
I can't test it right now, so maybe it has some errors. Good Luck!

struts2 <s:iterator> with <s:param> not working

I am displaying the list values with tag.At the same time i want to provide a hyperlink for the displaying values to provide action for the displaying value.I am using <s:param> for that.but the values are not passing.I am writing like below
<s:iterator status="stat" value="transactionList">
<s:url id="open" action="openTransaction">
<s:param name="transactionCode" value="<s:property value='monthName'/>"/>
</s:url>
<tr class="gradeB">
<td>
<s:a href="%{open}"><s:property value='transactionCode'/></s:a>
</td>
<td><s:property value="monthName"/></td>
<td><s:property value="transactionDesc"/></td>
</tr>
</s:iterator>
Now the transactionCode property is displaying with hyperlinks and by clicking on that the action is forwarding to openTransaction method.but the value i passed with <s:param> is not going,it is giving null. In iteration for that particular transaction code i want to pass that particular transaction code only. In struts 1.x I used display tag table decorator for this purpose, it takes the current row object.Here also i want to pass the current row values to the action. Please help me.
If you want to use <s:property> tag to put a param inside an url you have to do it like that:
<s:url ...>
<s:param name="foo"><s:property value="bar"/></s:param>
</s:url>
The documentation of <s:param> explains the difference between using this way of putting a param and your way.
Note: When you declare the param tag, the value can be defined in either a value attribute or as text between the start and end tag. Struts behaves a bit different according to these two situations. This is best illustrated using an example:
<param name="color">blue</param> <-- (A) -->
<param name="color" value="blue"/> <-- (B) -->
In the first situation (A) the value would be evaluated to the stack as a java.lang.String object. And in situation (B) the value would be evaluated to the stack as a java.lang.Object object.
For more information see WW-808.
Edit: Also remember that if you are using 2.1.x or higher, the id attribute of <s:url> is deprecated and it has been replaced with var. You can read it here.
<c:url var="search" value="/image/search.action">
<c:forEach items="${filtersMap}" var="map">
<c:param name="filtersMap.${map.key}" value="${map.value}"/>
</c:forEach>
${filtersMap} is a map param from action

s:iterator test if at certain iteration

I have searched around and cannot find an answer.
Say I have a list in session of 5 items in session. How do I tell if I am at a certain iteration in the loop of s:iterator? For example say list is [a,b,c,e,f] and I want to iterate through list and print a,b,c, then inject/print d, then print e f so on the page it looks like the list is a b c d e f.
I have been trying this:
<s:iterator value="example" status="stat">
<s:if test="#stat.count == '3'">
inject d...
</s:if>
<s:else>
s:property tag to print values
</s:else>
</s:iterator>
The status attribute you are looking for is "index", not "count". Also, I didn't know if it was a typo in the code or just in your post, but your status="stat" attribute has the equals sign inside the quotes. Try this instead:
<s:iterator value="example" status="stat">
<s:if test="#stat.index == '3'">
inject d...
</s:if>
<s:else>
s:property tag to print values
</s:else>

Resources