views:

262

answers:

1

Hi,

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...

+1  A: 

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.

Shawn D.
Hey Shawn,thanks for your answer, I got my app to work.
Raymond