Search code examples
struts-1

How can i get the dynamic number of textboxes values from jsp in struts?


I am having number of textboxes, where user will enter comments in indivddual textbox. I need to fetch all the textboxes values and pass it to action class. Please help me to move forward.

This is my jsp code:

   <layout:collectionItem  title="Comments">
        <layout:text readonly="true" property="comments"
        ondblclick="javascript:enableText(this);" name="comments"
        layout="false" />
   </layout:collectionItem>

This is my action class code:

    DynaActionForm dynaForm = (DynaActionForm) form;
    TablesVo tablesVo = new TablesVo();

    tablesVo.setComments((String) dynaForm.get("comments"));
    System.out.println("Comments:>>>>>>>>>>>>>>>>>>>>>>>"
                + (String) dynaForm.get("comments"));

This is my struts config code:

  <form-bean name="projectForm" type="org.apache.struts.action.DynaActionForm">

  <action-mappings>

  <action path="/Projects" type="com.rntbci.ptm.client.action.PTMManageAction"
            name="projectForm" scope="session" parameter="reqCode">
            <forward name="display_projects" path="tile.ptm.projects" />
            <forward name="display_columns" path="tile.ptm.columns" /> 
  </action>

  </action-mappings>

I am struggling to solve this problem for the past four days. Please do me a favour.


Solution

  • Give same name attribute to all your textboxes

    <input type="text" name="comment"/>
    <input type="text" name="comment"/>
    ...
    <input type="text" name="comment"/>
    

    Now in your action class instead of declaring a string variable declare a List variable

    private List<String> comment; //with getter/setter
    

    You can now iterate over this list to read the comments

    Iterator<String> it = comment.iterator();
    while(it.hasNext()){
       System.out.println("\n comment: "+it.next());
    }