struts 2保存动态表对象
使用 s2 迭代器标签,我可以毫无问题地显示表格。我有两个按钮可以使用 Javascript 添加行和删除行。下面是 jsp 页面的片段
<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>
我的问题是,每次单击 AddRow 按钮时,如何在服务器端为该行生成对象持有者?服务器端如何知道cient端添加了多少行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所展示的,您在单击按钮时不会为每行创建一个服务器端对象。您正在创建一个表单 - 在提交表单之前不会创建/保留服务器端对象。如果需要保证顺序,则需要为“name”属性的数组表示法保留一个 JavaScript 计数器。
您可以通过进行Ajax调用、创建对象并返回HTML以及项目的ID或其他内容来创建一个服务器端对象,但从您的问题中不清楚这是否是什么你确实想做。
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.