如何使用 Struts2 为 JSP 页面使用多个按钮(每行一个) [英] How to use multiple buttons (one on each line) for JSP page using Struts2

查看:28
本文介绍了如何使用 Struts2 为 JSP 页面使用多个按钮(每行一个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不知道如何为我的问题命名,但我有一个 JSP 页面,其中有一个表格显示数据库中的元素,我希望每一行都有一个按钮来删除或编辑该特定行.这是我生成表格的 JSP 页面部分(表格和按钮生成正常)

I don't really know how to title my question, but I have a JSP page with a table displaying elements from a database, and I want to have a button for each row to either delete or edit that particular row. Here is the part of my JSP page where I generate the table (the table and buttons are generated fine)

<style type="text/css">
  table { empty-cells: show; }
</style>
<table border="1">
<tr>
<th>Action</th>
  <s:iterator value="columnNames" id="name">
    <th> <s:property value="name" /> </th>
  </s:iterator>
</tr>
 <s:iterator value="%{table}" id="row">
 <tr>
   <td>
   <table><tr><td>
   <s:form action="edit" namespace="/." theme="simple">
   <s:submit value="Edit" name="edit" />
   </s:form></td>
   <td>
   <s:form action="remove" namespace="/." theme="simple">
   <s:submit value="Remove" name="remove" />
   </s:form></td></tr>
   </table></td>
     <s:iterator value="%{#row}" id="cell">
          <td><s:property value="%{#cell}"/></td>
     </s:iterator>
 </tr>
 </s:iterator>
</table>

我如何获得它,以便当我单击某一行上的特定按钮时,我的程序将知道它应该对哪一行执行(编辑/删除)操作?抱歉,我对 Struts2 还是很陌生……

How would I get it so that when I click on a particular button on a certain row, that my program will know which row it should perform the action on (edit/delete)? Sorry, I'm still pretty new to Struts2 still...

推荐答案

我不知道为什么你有一个只为按钮嵌套的表格......也许是为了布局.我建议让您的每个顶级都有一个表单,其中包含另一个标识行的属性和两个提交按钮.

I'm not sure why you have a table nested for just the buttons... perhaps it's for layout. I would suggest making each of your top-level have a form with another attribute identifying the row, and two submit buttons.

例如像这样(未经测试)

e.g. something like this (untested)

<s:form theme="simple">
    <s:hidden key="rowID" />
    <s:submit action="remove" value="Remove"/>
    <s:submit action="edit" value="Edit"/>
</s:form>

您可以有一个表单,每个表单包含多个操作.只需在唯一标识您将要操作的行的行中放置一些内容即可.

You can have a single form with multiple actions for each. Just put something in the row that uniquely identifies the row that you'll be acting upon.

因此,当提交时会发生什么,rowID 将包含在请求中并作为参数发送到您的特定操作(setRowID()).只需从您的原始数据中挑选可以唯一标识它的内容即可.

So, what'll happen is that when this is submitted, the rowID will be included in the request and sent to your specific action as a parameter to the setter (setRowID()). Just pick something from whatever your original data is that uniquely identifies it.

这篇关于如何使用 Struts2 为 JSP 页面使用多个按钮(每行一个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆