如何使用 Struts2 在 JSP 页面中使用多个按钮(每行一个)
我真的不知道如何为我的问题命名,但我有一个 JSP 页面,其中有一个显示数据库中的元素的表,并且我希望每行都有一个按钮来删除或编辑该特定行。这是我的 JSP 页面的一部分,我在其中生成表格(表格和按钮都生成得很好)
<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 还很陌生......
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>
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么你有一个只嵌套按钮的表格......也许是为了布局。我建议让每个顶层都有一个表单,其中包含另一个标识行的属性和两个提交按钮。
例如,类似这样的内容(未经测试)
您可以拥有一个表单,每个表单包含多个操作。只需在行中放置一些唯一标识您将要执行操作的行的内容即可。
因此,当提交时,rowID 将包含在请求中,并作为 setRowID() 的参数发送到您的特定操作。只需从您的原始数据中选择一些唯一标识它的东西即可。
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)
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.
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.