kedood
asked on
Iterator in struts 2 -iterating over a Transfer Object that has a collection of another T.O.
I am trying to iterate over a transfer object that is for example for User Information. It has name, city, state, zip and then has mulitple phone numbers. I put these multiple phone numbers in a collection of PhoneTO. I am trying to figure out how to us the struts 2 tags to write out these phone numbers.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Just to correct any tone:). I meant to say ...thanks for the answer and then ask a question. I can't type as fast as I think.
No problem. You need to put the object in the ActionContext to allow it to be accessed. See
http://struts.apache.org/2.x/struts2-core/apidocs/org/apache/struts2/components/IteratorComponent.html
Making it editable could be quite tricky. I'd need to ponder that
http://struts.apache.org/2.x/struts2-core/apidocs/org/apache/struts2/components/IteratorComponent.html
Making it editable could be quite tricky. I'd need to ponder that
ASKER
I have actually looked at that link several times and I am not sure what it means. I have tried it several ways.
<s:bean name="org.apache.struts2.e xample.Ite ratorExamp le" id="it">
<s:param name="day" value="'foo'"/>
<s:param name="day" value="'bar'"/>
</s:bean>
I thought this meant I would put the to and the param would by the list name...like this:
<s:bean name="com.mycomp.myproj.to .UserInfoT O" id="user">
<s:param name="phoneList" value="phoneList"/>
</s:bean>
Is day with the value of "foo" and "bar" suppose to represent the value that is being iterated?
Now, making the assumption that I am correct above (which I know I must not be), I add this to actually display my phone numbers:
<s:iterator value="#user.phoneList">
<tr>
<td style="width: 17%";>
<p>Phone info is: <s:property/></p>
</td>
</tr>
</s:iterator>
I am guessing this will not work because I also do not specify that my phoneList is a list of phoneNumberTO's?
Thanks very much for your help so far.
<s:bean name="org.apache.struts2.e
<s:param name="day" value="'foo'"/>
<s:param name="day" value="'bar'"/>
</s:bean>
I thought this meant I would put the to and the param would by the list name...like this:
<s:bean name="com.mycomp.myproj.to
<s:param name="phoneList" value="phoneList"/>
</s:bean>
Is day with the value of "foo" and "bar" suppose to represent the value that is being iterated?
Now, making the assumption that I am correct above (which I know I must not be), I add this to actually display my phone numbers:
<s:iterator value="#user.phoneList">
<tr>
<td style="width: 17%";>
<p>Phone info is: <s:property/></p>
</td>
</tr>
</s:iterator>
I am guessing this will not work because I also do not specify that my phoneList is a list of phoneNumberTO's?
Thanks very much for your help so far.
>>Is day with the value of "foo" and "bar" suppose to represent the value that is being iterated?
No, they are properties being set on that bean. I think having 'day' twice is an error, as there would be no point in setting the same property twice. You would use your own bean containing the collection you want to iterate. Maybe no need for parameters
Looking at your other q's...
No, they are properties being set on that bean. I think having 'day' twice is an error, as there would be no point in setting the same property twice. You would use your own bean containing the collection you want to iterate. Maybe no need for parameters
Looking at your other q's...
ASKER
I have now changed the form/action to set the arraylist of phonenumbers so that I can access them. I thought by referencing the form in my .jsp I would have access to this list but it does not seem to be working. I am attaching some of the source code...to hopefully give better insight.
My form/action named CWAChangePhoneNumberA has:
setPhoneList(cwaAccountInf oTO.getPho neList());
My jsp page:
<%@ page import="com.mudnebr.cwa.to .CWAUserIn foTO"%>
<%@ page import="com.mudnebr.cwa.to .CWAAccoun tInfoTO"%>
<%@ page import="com.mudnebr.cwa.to .CWAPhoneN umberTO"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link href="../jsps/css/cwa.css" rel="stylesheet" type="text/css" />
<!-- To trestore textboxes color -->
<script type="text/javascript"><!- -
if(window.attachEvent)
window.attachEvent("onload ",setListe ners);
function setListeners(){
inputList = document.getElementsByTagN ame("INPUT ");
for(i=0;i<inputList.length ;i++)
{
inputList[i].attachEvent(" onproperty change",re storeStyle s);
inputList[i].style.backgro undColor = "";
}
selectList = document.getElementsByTagN ame("SELEC T");
for(i=0;i<selectList.lengt h;i++)
{
selectList[i].attachEvent( "onpropert ychange",r estoreStyl es);
selectList[i].style.backgr oundColor = "";
}
}
function restoreStyles(){
if(event.srcElement.style. background Color != "")
event.srcElement.style.bac kgroundCol or = "";
}//-->
</script>
<title>Change Phone Number</title>
</head>
<center>
<body>
<table>
<tr><td align = "center"> <font size="2" color="red"><s:property value="message"/></font></ td></TR>
<tr><td align = "center"> </td</tr>
</table>
<s:form action="CWAChangePhoneNumb erA" method="post" theme="simple">
<table border="1" width="20%" id="table1" cellpadding=0 cellspacing=0 bgcolor="#FFFFF0">
<tr bgcolor="#0000FF">
<td align=center bgcolor="#000000" ><font color="#FFFFFF">
<b>Change Phone Number</b></font></td>
</tr>
<tr>
<td align="center">
<table border="0" width="45%" id="table2">
<tr>
<td width="12" class="tdLabel_1">Phone Number</td>
</tr>
<s:iterator value="#phoneList">
<tr>
<td style="width: 17%";>
<p>Phone info is: <s:property/></p>
</td>
</tr>
</s:iterator>
<tr><td> </td></tr>
<tr><td colspan="2" align="center" >
<s:hidden name="changePhoneNbr" value="Yes" />
<s:hidden name="accountNbr" value="<%=request.getParam eter("acco untNbr")%> " />
<%-- <s:reset value="Cancel" /> --%>
<INPUT TYPE="button" value="Cancel" onClick="javascript: self.close ()">
<s:submit name="submit" value="Submit" />
</td></tr>
<tr><td> </td></tr>
</table>
</td>
</tr>
</table>
</s:form>
</body>
</center>
</html>
My form/action named CWAChangePhoneNumberA has:
setPhoneList(cwaAccountInf
My jsp page:
<%@ page import="com.mudnebr.cwa.to
<%@ page import="com.mudnebr.cwa.to
<%@ page import="com.mudnebr.cwa.to
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<link href="../jsps/css/cwa.css"
<!-- To trestore textboxes color -->
<script type="text/javascript"><!-
if(window.attachEvent)
window.attachEvent("onload
function setListeners(){
inputList = document.getElementsByTagN
for(i=0;i<inputList.length
{
inputList[i].attachEvent("
inputList[i].style.backgro
}
selectList = document.getElementsByTagN
for(i=0;i<selectList.lengt
{
selectList[i].attachEvent(
selectList[i].style.backgr
}
}
function restoreStyles(){
if(event.srcElement.style.
event.srcElement.style.bac
}//-->
</script>
<title>Change Phone Number</title>
</head>
<center>
<body>
<table>
<tr><td align = "center"> <font size="2" color="red"><s:property value="message"/></font></
<tr><td align = "center"> </td</tr>
</table>
<s:form action="CWAChangePhoneNumb
<table border="1" width="20%" id="table1" cellpadding=0 cellspacing=0 bgcolor="#FFFFF0">
<tr bgcolor="#0000FF">
<td align=center bgcolor="#000000" ><font color="#FFFFFF">
<b>Change Phone Number</b></font></td>
</tr>
<tr>
<td align="center">
<table border="0" width="45%" id="table2">
<tr>
<td width="12" class="tdLabel_1">Phone Number</td>
</tr>
<s:iterator value="#phoneList">
<tr>
<td style="width: 17%";>
<p>Phone info is: <s:property/></p>
</td>
</tr>
</s:iterator>
<tr><td> </td></tr>
<tr><td colspan="2" align="center" >
<s:hidden name="changePhoneNbr" value="Yes" />
<s:hidden name="accountNbr" value="<%=request.getParam
<%-- <s:reset value="Cancel" /> --%>
<INPUT TYPE="button" value="Cancel" onClick="javascript: self.close ()">
<s:submit name="submit" value="Submit" />
</td></tr>
<tr><td> </td></tr>
</table>
</td>
</tr>
</table>
</s:form>
</body>
</center>
</html>
ASKER
Got it:
<th>ID</th>
<th>Area Code</th>
<th>Phone Number</th>
</tr>
<s:iterator id="p" value="phoneList" status="CWAPhoneNumberTO">
<tr>
<td>
<s:property value="%{id}" />
</td>
<td>
<s:textfield label="Area Code" name="phoneList(%{id}).are aCode" value="%{areaCode}" theme="simple" />
</td>
<td>
<s:textfield label="Phone Number" name="phoneList(%{id}).pho neNbr" value="%{phoneNbr}" theme="simple"/>
</td>
</tr>
</s:iterator>
Thanks for your help.
<th>ID</th>
<th>Area Code</th>
<th>Phone Number</th>
</tr>
<s:iterator id="p" value="phoneList" status="CWAPhoneNumberTO">
<tr>
<td>
<s:property value="%{id}" />
</td>
<td>
<s:textfield label="Area Code" name="phoneList(%{id}).are
</td>
<td>
<s:textfield label="Phone Number" name="phoneList(%{id}).pho
</td>
</tr>
</s:iterator>
Thanks for your help.
ASKER