How to perform an action from a dynamically created submit button in Struts2
I am creating A table in my jsp using data from my action and an iterator method.each table Row contains a publish (submit) button. I want a different action to be performed on clicking each publish button. I also want to use the data contained in that particular row. How do I accompl开发者_如何学Pythonish this?
As you didnt mention on which condition you are deciding the action i assume you are doing it based on some value in a particular table row. You have two choices
- use
<input type="button" value="publish" onclick="someFunc()">
and in someFunc you decide which action to call and change the form's action attribute according to that. - submit your form and then decide in ypur action class which action to call, then redirect(chain) to that action
As for submitting different values based on the button clicked, you must have some property in your rows which is unique for each row. Suppose that property is myUniqueVal
<s:form name="myform" method="post" action="myaction">
<s:hidden name="whatever_1"/>
<s:hidden name="whatever_2"/>
..
</s:form>
<table>
...
<s:iterator value="yourList">
<tr>
<td><s:property value="whatever_1"/></td>
<td><s:property value="whatever_2"/></td>
.....
<s:hidden name="whatever_1" id="%{myUniqueVal}1stValueToSubmit" value="%{whatever_1}"/>
<s:hidden name="whatever_2" id="%{myUniqueVal}2ndValueToSubmit" value="%{whatever_2}"/>
.....
<input type="button" value="publish" onclick="submitform('<s:property value="myUniqueVal"/>')">
</tr>
</s:iterator>
</table>
JavaScript
function submitform(uniqueval){
var myform=document.forms[0];
myform.whatever_1.value=document.getElementById(uniqueval+"1stValueToSubmit").value;
myform.whatever_2.value=document.getElementById(uniqueval+"2ndValueToSubmit").value;
//if you are going to use the first soltuion you can choose your action here ased on the above values
//myform.action="";
document.forms[0].submit();
}
You havent mentioned if for each row, you want to display, will have a unique identifier or not? Let us assume your unique identifier fields are - firstName and lastName - from Action
So you can you can iterate through your list as follows:
<s:iterator value="listFromAction">
<tr>
<s:set var="id1" value="%{firstName}" scope="request" />
<s:set var="id2" value="%{lastName}" scope="request" />
<td>
<input type="button" name="Click" value="Click"
onClick="callAction('<%=request.getAttribute("id1")%>',
'<%=request.getAttribute("id2") %>')"/>
</td>
</tr>
</s:iterator>
In the iteration, we have also added the button which calls a javascript function.
Now in this javascript function, you could call appropriate action class, depending upon the values of the row selected.
<script language="javaScript">
function callAction(id1,id2)
{
document.form.firstName.value=id1;
document.form.lastName.value=id2;
if(id1 == 'Troy' && id2=='Roy')
{ document.form.action="submit.do";}
document.form.submit();
}
Define variables firstName and lastName in your jsp as hidden variables:
<s:hidden name="firstName" />
<s:hidden name="lastName" />
精彩评论