You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Reddy, Ramachandra (IS Consultant)" <Ra...@consultantemail.com> on 2008/06/26 18:44:28 UTC

How to read data from nested list elements

Hi Folks,
I have particular requirement where I have nested elements and within that again some elements… So the nesting is up to 3 levels. 

Top most element is   
ModulesList= it has N modules
 Module = it has N  Parts 
Part= it has N Elements

Here is the class hieirarchy
Parent class that holds all modules.
public class FSModuleListForm extends ActionForm {

            private List mModuleList = new ArrayList();
      public List getModuleList() {return mModuleList;}
      public void setModuleList(List aModuleList) {mModuleList=aModuleList;}
      
public FSModuleForm getModule(int index) 
      {
                  return (FSModuleForm) mModuleList.get(index);
      }

public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
    {
        super.reset(aMapping, aRequest);
        moduleList = new ArrayList();
        for (int i=0; i < 25; i++)
        {
            mModuleList.add(i, new FSModuleForm());
        }
    }

}
==============================================
//Module Description 
      public class FSModuleForm extends ActionForm
        {
            Public FSModuleForm()   
            {     
              moduleId = -1;
              moduleName=null;
              moduleType=null;
              moduleLabel=null;
              partsList = new ArrayList();

              //Lets initialize the list
              for (int i= 0 ; i < 10; i++ ){
                  partsList.add(i,new FSPartForm());
              }
            }  
            public FSPartForm getPart(int index) {
                  return (FSPartForm) partsList.get(index);
            }
            public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
            {
                  partsList = new ArrayList();
                    //Lets initialize the list
                    for (int i= 0 ; i < 10; i++ ){
                        partsList.add(i,new FSPartForm());
                  }
            }
      
      }
==============================================

// PART Description
public class FSPartForm extends ActionForm {
      
      
      public FSPartForm()
       {
              partId=-1;
              sortOrder=0;
              partName=null;
              partLabel=null;
              partType=null;
              optionalFlag=null;
              partFootnote=null;
              elementList= new ArrayList();
            //Lets initialize the list
              for (int i= 0 ; i < 20; i++ ){
                  elementList.add(i,new FSElementForm());
              }
       }   

      public FSElementForm getElement(int index) 
      {
            
                  return (FSElementForm) mElementList.get(index);
      }
      public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
                  for (int i= 0 ; i < 50; i++ ){
                        elementList.add(i,new FSElementForm());
                    }
      }

}

==============================================
//Elemet Description.
public FSElementForm extends ActionForm {
 public FSElementForm()
      {
      
           elementId=-1;
           elementName=null;
           elementType=null;
           optionalFlag=null;
           defaultValue=null;
           sortOrder=0;
      }
      
}

Here is my JSP

      <logic:iterate id="module" name="FSModuleListForm" property="moduleList">
                        <tr>
                              <tr >  <!—Module info →
                                          <bean:write name="module" property="moduleName" /> <tr>
                                                Desc:<html:text name="module" property="moduleDesc"   indexed="true" />
                                                Label:<html:text name="module" property="moduleLabel"  indexed="true" />
                                    <html:hidden name="module" property="moduleId"        indexed="true" />
                              </tr>
                              <logic:present id="part" name="module" property="partsList">
                                    <logic:iterate id="part" name="module" property="partsList">
                                          <tr>
                                                <tr><bean:write name="part" property="partName" /></tr>
                                                <tr>Label:<html:text name="part" property="partLabel"    indexed="true" /></tr>
                                                <html:hidden name="part" property="partId" indexed="true" />
                                          </tr>
                                                <!—code to get each element will go here →
                                    </logic:iterate>
                              </logic:present>
                        </tr>
                        <hr>
                  </logic:iterate>


ISSUE:
1. Should I extend actionform for all classes or just the top most class is good (in my eg  FSModuleListForm) ?
2. I am able to display data on screen but when I submit only the module information gets populated
Not the parts associated to each module. Though indexing  value displayed looks good.
            
Html generated some what looks like 
            InvestMent Objective1:
                        Desc:  <input type="text" name="module[0].moduleDesc" value="InvestMent Objective – first ">
                        Label:<input type="text" name="module[0].moduleLabel" value="InvestMent Objective First">
                        <input type="hidden" name="module[0].moduleId" value="1">
                                    This is Investment Objective1 Part NAME
                                                            Label:<input type="text" name="part[0].partLabel" value="This is Investment Objective1 Part Label">
                                                                        <input type="text" name="part[0].partId" value="1">

----------------------------------------------------------------------
            InvestMent Objective2:
                        Desc:  <input type="text" name="module[0].moduleDesc" value="InvestMent Objective – Second ">
                        Label:<input type="text" name="module[0].moduleLabel" value="InvestMent Objective second">
                        <input type="hidden" name="module[0].moduleId" value="2">
                                                This is Investment Objective2 Part NAME
                                                            Label:<input type="text" name="part[0].partLabel" value="This is Investment Objective2 Part Label">
                                                            <input type="text" name="part[0].partId" value="2">




Can some one please help me on this issue or is this not possible in Struts ?

Thanks in advance. 







Warm Regards
Ramachandra Reddy



RE: How to read data from nested list elements

Posted by "Reddy, Ramachandra (IS Consultant)" <Ra...@consultantemail.com>.

Guys found the solution 
Check the usage of nested tags that can handle any levels.

http://www.learntechnology.net/content/struts/struts_nested.jsp


Warm Regards
Ramachandra Reddy



-----Original Message-----
From: Reddy, Ramachandra (IS Consultant) 
Sent: Thursday, June 26, 2008 12:44 PM
To: 'user@struts.apache.org'
Subject: How to read data from nested list elements


Hi Folks,
I have particular requirement where I have nested elements and within that again some elements… So the nesting is up to 3 levels. 

Top most element is   
ModulesList= it has N modules
 Module = it has N  Parts 
Part= it has N Elements

Here is the class hieirarchy
Parent class that holds all modules.
public class FSModuleListForm extends ActionForm {

            private List mModuleList = new ArrayList();
      public List getModuleList() {return mModuleList;}
      public void setModuleList(List aModuleList) {mModuleList=aModuleList;}
      
public FSModuleForm getModule(int index) 
      {
                  return (FSModuleForm) mModuleList.get(index);
      }

public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
    {
        super.reset(aMapping, aRequest);
        moduleList = new ArrayList();
        for (int i=0; i < 25; i++)
        {
            mModuleList.add(i, new FSModuleForm());
        }
    }

}
==============================================
//Module Description 
      public class FSModuleForm extends ActionForm
        {
            Public FSModuleForm()   
            {     
              moduleId = -1;
              moduleName=null;
              moduleType=null;
              moduleLabel=null;
              partsList = new ArrayList();

              //Lets initialize the list
              for (int i= 0 ; i < 10; i++ ){
                  partsList.add(i,new FSPartForm());
              }
            }  
            public FSPartForm getPart(int index) {
                  return (FSPartForm) partsList.get(index);
            }
            public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
            {
                  partsList = new ArrayList();
                    //Lets initialize the list
                    for (int i= 0 ; i < 10; i++ ){
                        partsList.add(i,new FSPartForm());
                  }
            }
      
      }
==============================================

// PART Description
public class FSPartForm extends ActionForm {
      
      
      public FSPartForm()
       {
              partId=-1;
              sortOrder=0;
              partName=null;
              partLabel=null;
              partType=null;
              optionalFlag=null;
              partFootnote=null;
              elementList= new ArrayList();
            //Lets initialize the list
              for (int i= 0 ; i < 20; i++ ){
                  elementList.add(i,new FSElementForm());
              }
       }   

      public FSElementForm getElement(int index) 
      {
            
                  return (FSElementForm) mElementList.get(index);
      }
      public void reset(ActionMapping aMapping, HttpServletRequest aRequest)
                  for (int i= 0 ; i < 50; i++ ){
                        elementList.add(i,new FSElementForm());
                    }
      }

}

==============================================
//Elemet Description.
public FSElementForm extends ActionForm {
 public FSElementForm()
      {
      
           elementId=-1;
           elementName=null;
           elementType=null;
           optionalFlag=null;
           defaultValue=null;
           sortOrder=0;
      }
      
}

Here is my JSP

      <logic:iterate id="module" name="FSModuleListForm" property="moduleList">
                        <tr>
                              <tr >  <!—Module info →
                                          <bean:write name="module" property="moduleName" /> <tr>
                                                Desc:<html:text name="module" property="moduleDesc"   indexed="true" />
                                                Label:<html:text name="module" property="moduleLabel"  indexed="true" />
                                    <html:hidden name="module" property="moduleId"        indexed="true" />
                              </tr>
                              <logic:present id="part" name="module" property="partsList">
                                    <logic:iterate id="part" name="module" property="partsList">
                                          <tr>
                                                <tr><bean:write name="part" property="partName" /></tr>
                                                <tr>Label:<html:text name="part" property="partLabel"    indexed="true" /></tr>
                                                <html:hidden name="part" property="partId" indexed="true" />
                                          </tr>
                                                <!—code to get each element will go here →
                                    </logic:iterate>
                              </logic:present>
                        </tr>
                        <hr>
                  </logic:iterate>


ISSUE:
1. Should I extend actionform for all classes or just the top most class is good (in my eg  FSModuleListForm) ?
2. I am able to display data on screen but when I submit only the module information gets populated
Not the parts associated to each module. Though indexing  value displayed looks good.
            
Html generated some what looks like 
            InvestMent Objective1:
                        Desc:  <input type="text" name="module[0].moduleDesc" value="InvestMent Objective – first ">
                        Label:<input type="text" name="module[0].moduleLabel" value="InvestMent Objective First">
                        <input type="hidden" name="module[0].moduleId" value="1">
                                    This is Investment Objective1 Part NAME
                                                            Label:<input type="text" name="part[0].partLabel" value="This is Investment Objective1 Part Label">
                                                                        <input type="text" name="part[0].partId" value="1">

----------------------------------------------------------------------
            InvestMent Objective2:
                        Desc:  <input type="text" name="module[0].moduleDesc" value="InvestMent Objective – Second ">
                        Label:<input type="text" name="module[0].moduleLabel" value="InvestMent Objective second">
                        <input type="hidden" name="module[0].moduleId" value="2">
                                                This is Investment Objective2 Part NAME
                                                            Label:<input type="text" name="part[0].partLabel" value="This is Investment Objective2 Part Label">
                                                            <input type="text" name="part[0].partId" value="2">




Can some one please help me on this issue or is this not possible in Struts ?

Thanks in advance. 







Warm Regards
Ramachandra Reddy