delete user from a list using ajax and refresh lis

2020-06-21 19:30发布

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

1条回答
冷血范
2楼-- · 2020-06-21 20:12

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 :)

查看更多
登录 后发表回答