'Call a method in action class from a jsp page in struts2
I have written following code to pass a variable artpsMstId to a javascript function and then update the record stored against that variable:
In JSP:
<td>
<a href="javascript:fnUpdate('<s:property value="artpsMstId" />');">testupdate</a>
</td>
<script type="text/javascript">
function fnUpdate(id)
{
<s:url action='updateServiceDetails'></s:url>
document.forms[0].artpsMstId.value=id;
document.forms[0].submit();
}
</script>
In struts.xml:
<action name="updateServiceDetails" class="com.stp.portal.view.SearchServicePortlet" method="updateServiceDetails">
<result name="success">/WEB-INF/view/ServiceSubmitPage.jsp</result>
</action>
The I have defined the function updateServiceDetails in the SearchServicePortlet.java.
But the flow is not coming to the SearchServicePortlet.java. would really appreciate if anyone could help me...Thanks
----EDITED-----
Here is what basically I want to do
<s:form action="updateServiceDetails" method="POST" theme="simple" >
<tr>
<td>
FirstName
</td>
<td>
LastName
</td>
<td>
Edit
</td>
</tr>
<s:iterator value="resultList" >
<tr>
<td align="center">
<s:textfield name="firstName" />
</td>
<td align="center">
<s:textfield name="lastName" />
</td>
<td>
Edit
</td>
<td align="center">
<a href="javascript:fnUpdate('<s:property value="artpsMstId" />');">testupdate</a>
</td>
</tr>
</s:iterator>
</s:form>
Above code displays a list with first names and last names with an edit option for each row. Now I want to call the function fnUpdate() in a javascript so that
I can edit and save each row separately. This is what I basically need to do.
Solution 1:[1]
Instead of having form over entire table, just have seperate form and on click on link in row of table, call javascript function to submit that seperate form,
Try below code, in jsp,
<form name="updateServiceDetailForm">
<input type="hidden" name="artpsMstId"/>
</form>
<table>
<tr>
<td>
FirstName
</td>
<td>
LastName
</td>
<td>
Edit
</td>
</tr>
<s:iterator value="resultList" >
<tr>
<td align="center">
<s:textfield name="firstName" />
</td>
<td align="center">
<s:textfield name="lastName" />
</td>
<td>
Edit
</td>
<td align="center">
<a href="javascript:fnUpdate('%{artpsMstId}');">testupdate</a>
</td>
</tr>
</table>
in javascript,
function fnUpdate(artpsMstId) {
document.forms.updateServiceDetailForm.action = "updateServiceDetails.action";
document.forms.updateServiceDetailForm.method = "post";
document.forms.updateServiceDetailForm.artpsMstId.value = artpsMstId;
document.forms.updateServiceDetailForm.submit();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pritesh Shah |
