4

my list is like:-

UserName   action
=================
abcd       delete
1234       delete

my jsp code is like:-

<table>
    <tr>
        <th>UserName</th>
        <th>Action</th>
    </tr>
    <s:iterator value="list">
        <tr>
            <td><s:property value="name" /></td>
            <td><a href="<s:url action='deleteUser'/>">delete</a></td>
        </tr>
    </s:iterator>
</table>

how to call an action using ajax that delete a user from the list and refresh the list

MohanaRao SV
  • 1,117
  • 1
  • 8
  • 22
Sandeep vashisth
  • 1,040
  • 7
  • 20
  • 40

1 Answers1

6

for a simple ajax refresh functionality i would go in this way

first a div containing the list like this

<div id="results">
<s:include page="ListUser.jsp">
</div>

The ListUser.jsp will contain my list of users to refresh and display

<table>
    <tr>
        <th>UserName</th>
        <th>Action</th>
    </tr>
    <s:iterator value="list">
        <tr>
            <td><s:property value="name" /></td>
            <td><a class=""linkDelete" href="<s:url action='deleteUser'/>">delete</a></td>
        </tr>
    </s:iterator>
</table>

a simple jquery ajax request would look like this

$("a.linkDelete").click(function(e) {
  //this line will prevent the default form submission on click of link
  e.preventDefault();
  //fire the ajax request on this object referring to the clicked anchor link element
  $(this).ajax({
  url: "DeleteAction.action",
  cache: false
}).done(function( html ) {
  $("#results").append(html);
});
});

The DeleteAction.action is struts.xml will look like this

<action name="DeleteAction" method="deleteUser">
<result>/WEB-INF/jsp/ListUser.jsp</result>
</action>

If you have other action also which has a result link to take you to the result page then following will be another entry in struts.xml

<action name="ResultAction" method="goToResultPage">
<result>/WEB-INF/jsp/Result.jsp</result>
</action>

Your Result.jsp will contain the div with id results.

cheers :)

Code2Interface
  • 495
  • 2
  • 7
  • impressive i agree almost but suppose i have link(calling action) to come on page which contains
    and it must have all available results. so how should i configure this action in struts.xml
    – Sandeep vashisth Mar 22 '13 at 10:52
  • simply call the action of result page on click of that link and submit the form – Code2Interface Mar 22 '13 at 11:24
  • suppose i have welcome.jsp(have link(resultAction) to go to result page) -->--> result.jsp (have list of user with delete link(DeleteAction)). i want to how to configure both of the action in struts.xml – Sandeep vashisth Mar 22 '13 at 11:25