How do you iterate through a list of objects? - struts2

I have a User class that has a String username in it. I have a list of users that I'm trying to display in a table using
<s:iterator value="users" id="list">
<tr>
<td><s:property value="#list.username" /></td>
<td></td>
<td></td>
<td></td>
</tr>
</s:iterator>
The rows are being displayed the right number of times, so it's iterating through my list properly. However, I don't know how to access the username property to display it. Obviously what I have above isn't correct... Any ideas?

First, in Struts2 2.1.x the id attribute is deprecated, var should be used instead (ref)
I think the # is misused there. Besides, "list" seems a bad name for what is to be assigned in each iteration... I think "user" is more appropiate.
IIRC, the syntax is
<s:iterator value="users" var="user">
... <s:property value="#user.username" />
</s:iterator>
Further, you don't need to assign the current item in the iterator for such a simple case. THis should also work:
<s:iterator value="users">
... <s:property value="username" />
</s:iterator>
Also you might want to try this:
<s:iterator value="users">
... <s:property /> <!-- this outputs the full object, may be useful for debugging -->
</s:iterator>
UPDATE: I corrected the bit about the #, it was ok.

You can do like this
<s:iterator value="users" >
<tr>
<td><s:property value="username" /></td>
<td></td>
<td></td>
<td></td>
</tr>
Observe that no need of #list there.
The other way is
<s:iterator value="users" var="user">
<tr>
<td><s:property value="#user.username" /></td>
<td></td>
<td></td>
<td></td>
</tr>
insetead of id give it as var.Since
we don’t specify a scope for var, this new user reference exists in
the default “action” scope—the ActionContext. As you can
see, we then reference it with the # operator.

You can use JSTL with Struts. It has a <c:forEach> tag in its core library that will allow you to iterate through a list or any other collection easily.

<s:iterator value="users" var="eachUser">
<div>
<s:property value="#eachUser.username">
</div>
</s:iterator>
Check this for more such examples

Struts 1.x takes care of your inner properties like this.
< logic:iterate id="user" name="userList">
< bean:write property="${userName}"/ >
< /logic:iterate>
I guess as per ur example u can have. You may not need "#list." again in the value attribute
< s:iterator value="users" id="list">
< s:property value="userName" />
< / s:iterator>

Related

struts2 iterating dropdown(s:select) inside <td> tag

I am iterating a list object in jsp file which is coming from action class.
Using table,tr,td tags in order to dispay value in tabular format as follows.
<s:iterator value="userList" >
<tr>
<td><s:property value="name" /> </td>
<td><s:property value="gender" /> </td>
<td><s:select name="country" list="countryList" value="%{country}" /> </td>
<td><s:property value="aboutYou"/> </td>
<td><s:property value="malingList /> </td>
<tr>
</s:iterator>
I need to display country drop down with selected value. Which is displaying but not in a proper format. Name and gender is showing in first line, country drop down showing in second line and aboutYou and mailing list showing in third line.
I tried to display drop down value inside tag but not displaying.
What i need to display is all five fields in one single line(tr)
Requesting you please help me.
Thanks in advance.

how to get value of selected link value from list of links?

In my jsp page I have the following code and it can successfully go to the action class once the link was clicked.
My concern is that I need to know in my action class what is the value of the selected link from the list.
<s:iterator value="mediaLendingList" >
<tr>
<td><s:url action="addMediaLending" var="urlTag" />
<a href="<s:property value="#urlTag" />" ><s:property /></a>
</td>
</tr>
</s:iterator>
Can anyone give me an idea on how to do this? Thanks in advance.
Use <s:param> tag to put parameters to <s:url> tag and top keyword to get current element of the iteration.
<s:iterator value="mediaLendingList">
<tr>
<td>
<s:url action="addMediaLending" var="urlTag">
<s:param name="variableName" value="top" />
</s:url>
<s:a href="%{urlTag}"><s:property /></s:a>
</td>
</tr>
</s:iterator>

Struts 2 select tag - Using the tag (with the same list) multiple times on a page does not work

I need to be able to use the same drop down list multiple times on a page. The first time i use the 'list' on the select tag, it works fine. The same list does not populate the second select tag i use it on. Here are the details.
In the action class, i populate the ArrayList containing values i need to populate the select tag.
setNames(new SomeDAO().getNames());
In the JSP
<s:select list="names"
id="nameList"
listKey="nameId"
listValue="userName"
/>
This select list populates just fine. If I use the following select tag on the same page (using the same list), it fails to print.
<s:select list="names"
id="rName"
listKey="nameId"
listValue="userName" />
If i replace the 'list' above with #{'test':'test'} (hardcoded list), the tag shows up fine. Looks like the property i set in the Action is getting cleared after the first use. Is that whats happening or am i doing something wrong? I get an 'IllegalStateException: Response already committed' error
Edit 1:
setNames() is used in the action method that deals with the JSP page. It is a simple getter function.
In the JSP, here is what i have.
<tr>
<td align="left" class="td-plain">Add New:</td>
<td class="td-plain">
<s:select list="names"
id="addNameID"
name="addUserNameID"
listKey="reinsId"
listValue="reinsName"
headerKey=""
headerValue="--User Name--"
/>
</td>
<td class="td-plain"><input id="addTreatyNumber" type="text" /></td>
<td class="td-plain"><input id="addReinsPercentage" type="text" /></td>
<td class="td-plain"><input id="addFlatDollarRetentionAmt" type="text" /></td>
<td class="td-plain">
<%if(finance){ %>
<input type="button" class="greyButton" value="Add" onclick="addReinsInfo()"/>
<%}else{ %>
None
<%} %>
</td>
</tr>
and then later down on that page, i have
<tr id='<s:property value="caseGroupId"/>:<s:property value="treatyId"/>'>
<td class="td-plain"><input type='checkbox' id='<s:property value="caseGroupId"/>:<s:property value="treatyId"/>'/></td>
<td class="td-plain">
<s:select list="names"
id="rName"
name="dName"
listKey="reinsId"
listValue="reinsName"
headerKey=""
headerValue="--User Name--"
/>
</td>
<td class="td-plain_"><s:textfield id="tNumber" value="%{treatyNumber}"/></td>
<td class="td-plain_"><s:textfield id="tPercentage" value="%{reinspercentage}"/></td>
<td class="td-plain_"><s:textfield id="rAmount" value="%{flatDollarRetentionAmt}"/></td>
<td class="td-plain"><input type="button" value="Delete" class="greyButton"/></td>
</tr>
If I change the 'list' in the second tag to
list="#{'Test':'Test'}"
the tag shows up fine. Please let me know if i can provide any further info.
Edit 2: I was able to get this to work by setting the drop down list values to the session.
As asked by #Dave and #Quaternion, post the relevant Java/JSP code.
But until then... I've noted that you are not using name attribute in Struts Selects;
while you can refer multiple times from different tags to the same source (the list attribute), to populate the Select from the same list of objects, you should instead specify a different name for each one, to define which variable (sent back to the Action) contains the selected value of which Select;
this may not be the answer to the current question but it will show up to you soon.

In s:iterator tag im having set of hyperlinks which leads to respective datas. I need to know which link is clicked

In s:iterator tag I have set of hyperlinks which leads to respective data. I need to know which link is clicked
Here my code:
<s:iterator value="datasFinal" status="reportStatus">
<tr class="<s:if test="#reportStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td align="center"><s:property value="#reportStatus.count" /></td>
<td align="center"><a href="javascript:submit('')">
<s:set name="index" value="#reportStatus.index" scope="session"/>
<s:property value="reportName" /> </a></td>
</tr>
<s:hidden name="reportName" value="%{reportName}"/>
</s:iterator>
I got the solution, thank u RAO and Dave.
<s:iterator value="datasFinal" status="reportStatus">
<tr class="<s:if test="#reportStatus.odd == true ">odd</s:if><s:else>even</s:else>">
<td align="center"><s:property value="#reportStatus.count" /></td>
<td align="center"><a href="javascript:submit('${reportName}')">
<s:set name="index" value="#reportStatus.index" scope="session"/>
<s:property value="reportName" /> </a></td>
</tr>
</s:iterator>
By submitting this to java script i got which link is clicked...

Iterator, set, if-else tags of Struts2(Getting the value of s:radio and applying if-else )

I have a jsp page in which I have two radio buttons and a select tag. Now, if the first radio button is clicked than I want to make that select tag disable but I am fail to do this , I have tried using "disabled" property. Following is the code.
Jsp Page
<table align="center">
<s:iterator value="FirstObjectList" status="AuthorTypeStatus">
<tr>
<td>
<s:radio name="radio_SelectedValue" list="{ObjectName}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
</s:iterator>
<s:if test="%{#radio_SelectedValue == 'ObjectName1'}">
<s:set name="isSelectDisabled" value="false"/>
</s:if>
<s:else>
<s:set name="isSelectDisabled" value="True"/>
</s:else>
<tr >
<td colspan="2">
Select Parent Discover Lab
</td>
</tr>
<tr>
<td>
<s:select name="select_SelectedValue" headerKey="DefaultObject" headerValue="ParentObject" list="ObjectList" listKey="ObjectListKey" listValue="ObjectListValue" disabled="%{isSelectDiabled}">
<s:iterator value="ObjectList">
</s:iterator>
</s:select>
<s:submit value="Continue">
</s:submit>
</td>
</tr>
All the getter and setter methods are fixed..is this possible without javascript?
I'm a little confused by your code block, but I think I understand what you're aiming for. Something like this should work
<tr>
<td>
<s:radio name="radio_SelectedValue" list="%{radioList}" listKey="ObjectKey" listValue="ObjectName" value="DefaultObject"/>
</td>
</tr>
<s:if test="%{radio_SelectedValue.equals('myChosenValueFromTheList')}">
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" />
</s:if><s:else>
<s:select name="select_SelectedValue" headerKey="-1" headerValue=" " list="ObjectList" listKey="ObjectList.Key" listValue="ObjectList.Value" disabled="disabled" />
</s:else>
I'm not sure what you're doing with the iterators with these tags, they handle the list propagation themselves. Simply tell the tag what list to use and struts will populate the tags appropriately. You can then do a conditional based on the value of the radio when the page renders and perform magic on the select tag as shown above.

Resources