Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

java - How to use multiple buttons (one on each line) for JSP page using 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...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...