简体   繁体   中英

struts 2 save dynamic table object

With s2 iterator tag, I can display table without problem. and I have two button to add row and delete row with Javascript. Below is the snippet of jsp page

<s:iterator value="entities" status="outerStat">
  <tr>
    <TD><input type="checkbox" name="chk"/></TD>
    <TD>
      <table width="100%" cellspacing="4" cellpadding="0" border='0'>
        <s:textfield name="entities[%{#outerStat.index}].entityName" value="%{entityName}"/>
      </table>
    </TD>
    <TD>
      <table width="100%" cellspacing="4" cellpadding="0" border='0'>
        <s:select  list="entityTypes" value="%{entityType}"/>
      </table>
    </TD>
    <TD>
      <table width="100%" cellspacing="4" cellpadding="0" border='0'>
        <s:textarea  name="entities[%{#outerStat.index}].sqlStmt" cols="120" rows="4"  maxlength="4000" value="%{sqlStmt}"/>
      </table>
    </TD>
  </tr>
</s:iterator>

My question is , everytime I click AddRow button, how can I generate a object holder for that row in server side? how does the server side know that how many rows are added in cient side?

As you've shown it, you wouldn't create a server-side object for each row as you click the button. You're creating a form--the server side object wouldn't be created/persisted until the form is submitted. If you need to guarantee order, you'd need to keep a JavaScript counter for the "name" attribute's array notation.

You could create a server-side object on the click by making an Ajax call, creating the object, and returning the HTML along with the item's ID or whatever, but it's not clear from your question if that's what you actually want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM