使用這個之前,你要先懂得
- struts2的配置
- 基礎的el語法
- action的分配
- 返回值的回調
- 一定的ajax和jquery基礎
首先你要配置好web.xml,applicationContext-beans.xml,applicationContext.xml,這里不展開說
第一種方法比較簡單直接在按鈕搞個鏈接,鏈接上有id參數
首先配置struts.xml里的action
<!-- method通配符,執行方法自動對應*的內容 -->
<action name="emp-*" class="employeeAction"
method="{1}">
<!-- 顯示所有員工 -->
<result name="list">/WEB-INF/views/emp-list.jsp</result>
<!-- 刪除后重定向的所有員工列表 -->
<result name="success" type="redirect">/emp-list</result>
</action>
頁面的關鍵部分
<a href="emp-delete?id=${id }">Delete</a>
Action部分
public class EmployeeAction extends ActionSupport implements RequestAware{
private static final long serialVersionUID = 1L;
//員工增刪查改的封裝,具體代碼可以忽略
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public String list() {
request.put("employees", employeeService.getAll());
return "list";
}
private Integer id;
//自動獲取傳入id
public void setId(Integer id) {
this.id = id;
}
public String delete() {
employeeService.delete(id);
return "success";
}
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
}
點擊那個鏈接之后就會直接刪除掉那個數據并重定向到所有員工的列表
第二種方法比較高級,用到ajax
因為通常我們刪除都需要確定,并不是一點擊就刪除然后跳轉
重新配置struts.xml里的action
<!-- method通配符,執行方法自動對應*的內容 -->
<action name="emp-*" class="employeeAction"
method="{1}">
<!-- 顯示所有員工 -->
<result name="list">/WEB-INF/views/emp-list.jsp</result>
<!-- 返回ajax-success時以流的方式接收 -->
<result type="stream" name="ajax-success">
<param name="contentType">text/html</param>
<param name="inputName">inputStream</param>
</result>
</action>
頁面主要代碼
<tr>
<!-- 省略其他行 -->
<td>
<a href="emp-delete?id=${id }" class="delete">Delete</a>
<input type="hidden" value="${lastName }"/>
</td>
</tr>
要調用的js
<script type="text/javascript">
$(function(){
//1. 點擊 delete 時, 彈出 確定是要刪除 xx 的信息嗎 ? 若確定, 執行刪除, 若不確定, 則取消
$(".delete").click(function(){
var lastName = $(this).next(":hidden").val();
var flag = confirm("確定要刪除" + lastName + "的信息嗎?");
if(flag){
var $tr = $(this).parent().parent();
//刪除, 使用 ajax 的方式
var url = this.href;
var args = {"time":new Date()};
$.post(url, args, function(data){
//若 data 的返回值為 1, 則提示 刪除成功, 且把當前行刪除
if(data == "1"){
alert("刪除成功!");
$tr.remove();
}else{
//若 data 的返回值不是 1, 提示刪除失敗.
alert("刪除失敗!");
}
});
}
//取消超鏈接的默認行為
return false;
});
})
</script>
Action代碼
public class EmployeeAction extends ActionSupport implements RequestAware{
private EmployeeService employeeService;
public void setEmployeeService(EmployeeService employeeService) {
this.employeeService = employeeService;
}
public String list() {
request.put("employees", employeeService.getAll());
return "list";
}
private Integer id;
//設置傳入id
public void setId(Integer id) {
this.id = id;
}
//利用輸入流傳回,通常返回一些標記值(0或1之類的)
private InputStream inputStream;
public InputStream getInputStream() {
return inputStream;
}
public String delete() {
//成功返回1,失敗返回0
try {
employeeService.delete(id);
inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
} catch (Exception e) {
e.printStackTrace();
try {
inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
return "ajax-success";
}
private Map<String, Object> request;
@Override
public void setRequest(Map<String, Object> arg0) {
this.request = arg0;
}
}
利用ajax的話更符合現實開發的需要,用戶要先確定再執行操作,而且不用重定向,移除在當頁面的進行,更加節省流量用戶體驗也更加好