【问题标题】:How to get values of the rows of a table that are filled in the jsp in the Action class in Struts 2?Struts 2中Action类的jsp中如何获取填充的表格行的值?
【发布时间】:2013-04-10 03:03:18
【问题描述】:

我有以下 jsp,其中我可以选择添加和删除表格的行,并且在表格的每一行中,我必须填写选项并提交表单。

在动作类中,我如何检索我在 JSP 中填写的行的值,因为我在表格中填写了不止一行?

这是我的 JSP:

<table id="fragmentTable" style="height: 100%; width: 100%">

    <tr class="tableRow" style='height: 5%;'>
      <td class='' style='position: absolute; left: 75%;'>
    <button value="Add Row" type="button" onclick="return addRow1();">Add Row</button>
    <button value="Delete Row" type="button" onclick="return deleteRow          ('dataTable')">Delete Row</button>
      </td>
    </tr>

     <tr>
    <td colspan="2"><br><br>
    <table id="dataTable" border="1" width=100% height=30% class='blueBorder'>
    <thead>
    <tr class='rowHeader'>
          <th width='14%'>Select</th>
          <th width='14%'>Opening Brace</th>
          <th width='14%'>Parameter</th>
          <th width='14%'>Relational Operator</th>
          <th width='14%'>Value</th>
          <th width='15%'>Closing Brace</th>
          <th width='15%'>Logical Operator</th>
    </tr>
    </thead>

    <tbody>


        <tr class='rowData' id=1>
<td>
<s:checkbox theme="simple" id="sel" name="GRF_FRAGID" value="false" /></td>
<td ><s:select headerKey="-1" headerValue="Select" list="#{'1':'(', '2':'((','3':'(((','4':'((((','5':'((((('}" name="GRF_OPNGBRCES" /></td>
<td>
<s:select label="Mode" headerKey="-1" headerValue="Select" list="#{'1':'=', '2':'!='}"
    name="GRF_OP" />
</td>
<td>
<s:textfield name="Country"/>
</td>

</tr>
</tbody>
</table>

【问题讨论】:

  • 您有 2 个选项,要么保留已更改的行,要么将整个数据提交到您的操作类。

标签: jsp struts2 struts struts-1 struts2-jquery


【解决方案1】:

我不完全理解您的页面,但您需要使用迭代器(如 @NidhishKrishnan 建议的那样)并通过其 IteratorStatus 对象指向索引:

<s:iterator value="yourlist" status="ctr" >
  <tr>
    <td><s:textfield name="yourlist[%{#ctr.index}].country"/></td>
  </tr>
 </s:iterator>

country 首字母小写。

如果您需要使用 javascript 生成新行,则必须生成与 &lt;s:textfield/&gt; 标记“输出”的 HTML 相同的 HTML(单击浏览器的 View Source,并了解它是如何生成的)。

这将在目标操作(必须具有相同的Listsetter)上创建一个包含页面中所有值的新列表。

无需单独对一行进行操作,您可以使用此更新(例如)一篇帖子中的 10 行。


编辑

回答您的评论,如果您没有任何初始值,则不涉及迭代:您可以使用与添加新行相同的语法将第一行定义为列表的第一个元素。

不需要从列表开始,唯一的要求是将列表格式的 HTML 元素发布到 destination 列表中。

this article 为例,了解您需要创建哪些 javascript 和 HTML。

【讨论】:

  • 最初列表为空时如何输入迭代器?
  • 我看过这个链接,但我需要生成下拉菜单,不知道如何使用他们的 javascript 来完成
  • 作为一个提示,从一些非常基本但有效的东西开始(一个输入字段,添加行,编辑行,删除行等),然后才增加复杂性。对于选择,您可以在 javascript 中使用 HTML &lt;select&gt; 标记和 &lt;s:iterator&gt; 围绕 &lt;option&gt; 元素生成选择。您可以在页面加载时执行此操作,并将选择的“模板”保存在 javascript 中,您将通过引用其 javascript var 将其附加到每个新行。
猜你喜欢
  • 2013-05-07
  • 1970-01-01
  • 2016-08-29
  • 2013-06-11
  • 1970-01-01
  • 2013-01-23
  • 2014-03-30
  • 2013-09-06
  • 1970-01-01
相关资源
最近更新 更多