Link to home
Start Free TrialLog in
Avatar of kedood
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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kedood
kedood

ASKER

Thanks for the answer? Howwill it know that the phonelist is from the UserInfoTO? Also, I forgot to mention that all the phone numbers need to be editable in the jsp.
Avatar of kedood

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
Avatar of kedood

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.example.IteratorExample" 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.UserInfoTO" 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.
>>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...
Avatar of kedood

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(cwaAccountInfoTO.getPhoneList());  
My jsp page:
 <%@ page import="com.mudnebr.cwa.to.CWAUserInfoTO"%>
<%@ page import="com.mudnebr.cwa.to.CWAAccountInfoTO"%>
<%@ page import="com.mudnebr.cwa.to.CWAPhoneNumberTO"%>
<%@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",setListeners);

  function setListeners(){
        inputList = document.getElementsByTagName("INPUT");
        for(i=0;i<inputList.length;i++)
            {
            inputList[i].attachEvent("onpropertychange",restoreStyles);
            inputList[i].style.backgroundColor = "";
            }
        selectList = document.getElementsByTagName("SELECT");
        for(i=0;i<selectList.length;i++)
        {
            selectList[i].attachEvent("onpropertychange",restoreStyles);
            selectList[i].style.backgroundColor = "";
        }
  }

  function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "")
      event.srcElement.style.backgroundColor = "";
  }//-->
 
</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">&nbsp;</td</tr>
               </table>  
               
              <s:form action="CWAChangePhoneNumberA" 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>&nbsp;  </td></tr>
                                                <tr><td colspan="2" align="center" > 
                                                    <s:hidden name="changePhoneNbr" value="Yes" />                                                
                                                    <s:hidden name="accountNbr" value="<%=request.getParameter("accountNbr")%>" />
                                                   <%--  <s:reset value="Cancel" /> --%>
                                                   <INPUT TYPE="button" value="Cancel" onClick="javascript: self.close ()">  
                                                   <s:submit name="submit" value="Submit" />
                                                </td></tr>
                                                <tr><td>&nbsp;  </td></tr>                                            
                              </table>
                                    </td>
                        </tr>
                            </table>                        
                        </s:form>                                          
            </body>
      </center>
</html>

Avatar of kedood

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}).areaCode" value="%{areaCode}" theme="simple" />
                </td>
                <td>
                    <s:textfield label="Phone Number" name="phoneList(%{id}).phoneNbr" value="%{phoneNbr}" theme="simple"/>
                </td>
            </tr>
        </s:iterator>

Thanks for your help.