delete user from a list using ajax and refresh list only using struts 2

依然范特西╮ 提交于 2019-12-05 01:03:13

问题


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:


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



来源:https://stackoverflow.com/questions/15461490/delete-user-from-a-list-using-ajax-and-refresh-list-only-using-struts-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!