I'm looking for ideas to show only last element of iterator using struts 2
i have this code :
<s:iterator value="listuser" status="userStatus">
<tr class="<s:if test="#userStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td><s:property value="nameuser" /></td>
// and others
</s:iterator>
The Struts2 Iterator Status not only provide odd and even methods, it provides also a last and first method.
<s:iterator value="listuser" status="userStatus">
<s:if test="#userStatus.last == true ">
<td><s:property value="nameuser" /></td>
</s:if>
</s:iterator>
Something like this will do the trick :
<s:if test="#userStatus.index==listuser.size()-1">
//Show the last element here
</s:if>
[UPDATE]
As per Quaternion's comment, here's how to get it in one line :
<s:set name="lastUser" value="listuser[listuser.size()-1]"/>
Above we set the last element in lastUser and here's how to use it else-where in the same page.
<s:property value="#lastUser.name"/>
Related
Is it possible to increment a variable in struts 2?
I have a group of check boxes, each checkbox comes under different group. And I name the check boxes based on the group id and its own id from the database
//namesHead : list contains all the group Ids and names
//subHead : list contains all the subgroups id,Name and the reference of main group
<s:iterator status="status" value="namesHead">
<input type="checkbox" onclick="selectSimilarSubGroup('<s:property value="%{id}" />')" />Select All
<s:set name="itrVar" value="1"></s:set>
<s:iterator status="status1" value="subHead" >
<s:set name="var1" value="%{refer_id}"></s:set>
<s:set name="var2" value="%{id}"></s:set>
<s:if test="%{#var1==#var2}">
<s:set name="itrVar" value="%{#status1.count}"></s:set>
<input type="checkbox" multiple id="chk_grp<s:property value="%{id}"/>_<s:property value="%{#status1.count}" />" name="chk_grp" value="<s:property value="%{id_grp}"/>" />
<s:property value="%{name_grp}"/>
</s:if>
</s:iterator>
<s:hidden name="grp_count_%{id}" value="%{#itrVar}" />
</s:iterator>
I think its because the iterator is skipping the initial counts in the second loop. How can i modify the code to get that expected output. Or is there any way to increment a variable inside the page itself?
Mmm... sub-iterator status should reset on every iteration of the parent iterator, with both "two separate lists" or "a list inside another list"...
Something is messing up the counter. Try removing the <s:set line inside the <s:if, just for debugging, and ignoring the <s:hidden for now:
<s:iterator status="status" value="namesHead">
<input type="checkbox" onclick="selectSimilarSubGroup('<s:property value="%{id}" />')" />Select All
<s:set name="itrVar" value="1"></s:set>
<s:iterator status="status1" value="subHead" >
<s:set name="var1" value="%{refer_id}"></s:set>
<s:set name="var2" value="%{id}"></s:set>
<s:if test="%{#var1==#var2}">
<!-- Nothing here -->
<input type="checkbox" multiple id="chk_grp<s:property value="%{id}"/>_<s:property value="%{#status1.count}" />" name="chk_grp" value="<s:property value="%{id_grp}"/>" />
<s:property value="%{name_grp}"/>
</s:if>
</s:iterator>
<!-- Nothing here -->
</s:iterator>
What do you see ?
I am trying to test:
<s:if test="%{selectedLanguage == <s:property/>}">
Where selectedLanguage comes from the action, and s:property is the current object.
In a iterator like this:
<s:iterator value="languages">
<s:if test="%{selectedLanguage == <s:property/>}">
<option id="<s:property/>" selected><s:property/></option>
</s:if>
<s:else>
<option id="<s:property/>"><s:property/></option>
</s:else>
</s:iterator>
Obviously,it doesnt work. How can I solve?
Without fixing things, just use the var attribute to give the object of iteration a name:
<s:iterator value="languages" var="language">
<s:if test="%{selectedLanguage == #language}">
<option id="<s:property/>" selected><s:property/></option>
</s:if>
<s:else>
<option id="<s:property/>"><s:property/></option>
</s:else>
</s:iterator>
You could also just use #top to get the top of the stack, but I think naming it is more communicative.
I'd recommend setting a selected attribute based on #language or just use the select tag.
Instead of just displaying list values using <s:property/>, i need to display it in textarea.The reason why i am doing this is <s:property/> does not have "name" attribute in order to submit the values to the action class. Is there any way to do that.I wrote down piece fo code below to make it clear so that you can understand my requirement though it is wrong.Please help me.
<s:iterator value="map" status="stat">
<s:textarea value="<s:property/>" />
</s:iterator>
As Jagan said, assume that map is java.util.List
<s:iterator value="map" status="stat">
<s:textarea value="%{top}" />
</s:iterator>
or
<s:iterator value="map" status="stat">
<s:textarea>
<s:param name="value">
<s:property/>
</s:param>
</s:textarea>
</s:iterator>
or
<s:iterator value="map" status="stat">
<s:textarea>
<s:param name="value">
${top}
</s:param>
</s:textarea>
</s:iterator>
#Jagan : if it is map how to do this ?
Assume map is java.util.Map
<s:iterator value="map" status="stat">
<s:textarea value="%{key}" />
<!-- or -->
<s:textarea value="%{value}" />
</s:iterator>
IteratorComponent
if (value == null && begin == null && end == null) {
value = "top";
}
top is not a attribute of iterator and not a keyword of OGNL. (I may be wrong)
top : top of Stack / element of the current iteration
e.g.
<s:property value="top" />
or
<s:property value="[0].top" />
Iterator tag examples of Struts2 Cookbook have better explanation
I have the following code snippet
<s:iterator status="stat" value="masterAccountList">
<tr>
<td><s:property value="name"/></td>
<td><s:property value="status"/></td>
<s:set name="DrStat" id="DrStat" value="<s:property value='status'/>"/>
<td><s:if test='DrStat.contains("Out")'>
Dr. Is Available
</s:if>
<s:else>
Dr. Is not Available
</s:else>
</td>
</tr>
</s:iterator>
I need to check the status if it contains a keyword and display text accordingly. When I try this, I always get 'Not Available' status.
I'm not even sure what the set returns, how can I see that?
Shouldn't <s:text name="DrStat" /> print the value?
The solution:
<s:if test="%{DrStat.contains('Out')}">
Work fine. Thanks Dustin.
In my Struts2 application I am generating a textual report (in jsp) using iterator tag like
<table>
<tr>
<td>ID</td>
<td>PROOF</td>
<td>DELETE</td>
</tr>
<s:iterator value="listOfVOClassObjects">
<tr>
<td><s:property value="requestId" /></td>
<td><s:property value="requestChecker" /></td>
<td><s:property value="requestProof" /></td>
<td><s:checkbox name="deleteStatus" onclick="submit()"/></td>
</tr>
</s:iterator>
</table>
When user click checkbox page submits and control goes to action class and I need at the same time values of the corresponding row that user has checked should set in setter methods written in VO class so that I can get all these values in my action class.
I tried this by writing a hidden field for every value under iterator tag like
<s:hidden name=" requestId" />
<s:hidden name=" requestChecker" />
<s:hidden name=" requestProof" />
but its not returning the values of corresponding row but the values of all rows separated by commas.
I also tried this by writing these hidden fields outside the iterator tag and that is returning null for every filed.
Please help.
You could update the submit() method to get the correct values and pass them to the action.
In your javascript function, you would set the current values then submit. So you'd have something like this:
<form method="post" action="myAction">
<script>
function submitform(requestId, requestChecker, requestProof) {
document.findElementById('requestId').value = requestId;
document.findElementById('requestChecker').value = requestChecker;
document.findElementById('requestProof').value = requestProof;
document.forms[0].submit();
}
</script>
<s:hidden id="requestId" name="requestId" />
<s:hidden id="requestChecker" name="requestChecker" />
<s:hidden id="requestProof" name="requestProof" />
<table>
<tr>
<td>ID</td>
<td>PROOF</td>
<td>DELETE</td>
</tr>
<s:iterator value="listOfVOClassObjects">
<tr>
<td><s:property value="requestId" /></td>
<td><s:property value="requestChecker" /></td>
<td><s:property value="requestProof" /></td>
<td><s:checkbox name="deleteStatus" onclick="submitForm('<s:property value="requestId" />', '<s:property value="requestChecker" />', '<s:property value="requestProof" />');"/></td>
</tr>
</s:iterator>
</table>
</form>
It depends on what you are doing. If you can recreate the List on the server side then all you need to do is POST the index of the list that you want to get a handle on. For example:
<s:iterator value="myList" status="row">
...
<s:checkbox onclick="deleteRow(%{#row.index})"/>
</s:iterator>
<script>
function deleteRow(index){
location.href=delete?listIndex=index
}
</script>
You would have a setter for listIndex to "receive" the value. This is using a GET, if you want a POST you can do that and make the listIndex a hidden field.
BUT, if you can't recreate the list on the server because the user has modified the list somehow in the browser and you need to preserve that, then you need Struts2 to create the list based on the POST'd parameters. You do that with some special syntax.
<s:iterator value="myList" status="row">
<s:hidden name="myList[%{#row.index}].requestId"/>
<s:hidden name="myList[%{#row.index}].requestChecker"/>
<s:hidden name="myList[%{#row.index}].requestProof"/>
</s:iterator>
This creates fields with names like myList[0].requestId ... myList[5].requestProof. If you have a setter for myList in your action and you POST these parameters to the action, Struts2 will build the List and populate the properties of the objects in the List. You can use Generics List myList to tell Struts2 what kinds of "things" are in your List. You can also look at Type Conversion documentation for Struts2 if Generics doesn't work for you.