?
Solved

Iterator in struts 2 -iterating over a Transfer Object that has a collection of another T.O.

Posted on 2007-10-18
10
Medium Priority
?
3,988 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:kedood
  • 5
  • 3
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 20103678
Try

<s:iterator value="phoneTOCollection">
      <p>Phone info is: <s:property/></p>
</s:iterator>

Make sure the object in the collection has a proper toString
0
 

Author Comment

by:kedood
ID: 20103926
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.
0
 

Author Comment

by:kedood
ID: 20103967
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 20104161
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
0
 

Author Comment

by:kedood
ID: 20104991
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20107285
>>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...
0
 

Author Comment

by:kedood
ID: 20131740
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>

0
 

Author Comment

by:kedood
ID: 20139327
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question