我是阿福,公眾號「阿福聊編程」作者,一個在后端技術路上摸盤滾打的程序員,在進階的路上,共勉!文章已收錄在 JavaSharing 中,包含Java技術文章,面試指南,資源分享。
前台實現
表單實現
首先定義全選框的的id屬性id="summaryBox"
<th width="30"><input id="summaryBox" type="checkbox"></th>
然后定義一個數據單選框的class屬性 class="itemBox"
,說明:adminId屬性是HTML標簽本身並沒有的屬性,是我們強行設置的。
<th width="30"><input adminId="${item.id}" class="itemBox" type="checkbox"></th>
整個表單文件
<table class="table table-bordered">
<thead>
<tr>
<th width="30">#</th>
<th width="30"><input id="summaryBox" type="checkbox"></th>
<th>賬號</th>
<th>名稱</th>
<th>郵箱地址</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
<c:if test="${empty requestScope['PAGEINFO-ADMIN'].list}">
<tr>
<td style="text-align: center" colspan="6">抱歉,沒有用戶查詢的數據!!!!</td>
</tr>
</c:if>
<c:if test="${! empty requestScope['PAGEINFO-ADMIN'].list}">
<c:forEach items="${requestScope['PAGEINFO-ADMIN'].list}" var="item"
varStatus="myStatus">
<tr>
<td>${myStatus.count}</td>
<th width="30"><input adminId="${item.id}" class="itemBox" type="checkbox"></th>
<td>${item.loginAcct}</td>
<td>${item.userName}</td>
<td>${item.email}</td>
<td>
<button type="button" class="btn btn-success btn-xs"><i
class=" glyphicon glyphicon-check"></i></button>
<a href="admin/toupdateadmin.action?adminId=${item.id}" type="button" class="btn btn-primary btn-xs"><i
class=" glyphicon glyphicon-pencil"></i></a>
<button type="button" adminId="${item.id}"
class="btn btn-danger btn-xs uniqueRemoveBtn"><i
class=" glyphicon glyphicon-remove"></i></button>
</td>
</tr>
</c:forEach>
</c:if>
</tbody>
<tfoot>
<tr>
<td colspan="6" align="center">
<div id="Pagination" class="pagination"><!-- 這里顯示分頁 --></div>
</td>
</tr>
</tfoot>
</table>
jQuery代碼
// 全選/全不選功能
$("#summaryBox").click(function () {
$(".itemBox").prop("checked", this.checked);
});
前后台交互jQuery代碼實現
給批量刪除按鈕標記id
//批量刪除按鈕實現
<button type="button" id="batchAdmin" name="batchAdmin" class="btn btn-danger"
style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 批量刪除
</button>
//封裝統一的Ajax響應結果
// 給批量刪除按鈕綁定單擊響應函數
$("#batchAdmin").click(function () {
var adminIdArray = new Array();
var loginAcct;
$(".itemBox:checked").each(
function () {
// this.adminId拿不到值,原因是:this作為DOM對象無法讀取HTML標簽本身沒有的屬性
// 將this轉換為jQuery對象調用attr()函數就能夠拿到值
var adminId = $(this).attr("adminId");
// 調用數組對象的push()方法將數據存入數組
loginAcct = $(this).parents("tr").children("td:eq(2)").text();
adminIdArray.push(adminId);
});
batchRemoveAdmin(adminIdArray, loginAcct);
});
function batchRemoveAdmin(adminIdArray, loginAcct) {
// 將JSON數組轉換為JSON字符串
var adminListId = JSON.stringify(adminIdArray);
if (adminIdArray.length == 0) {
alert("請勾選需要刪除的記錄!");
return;
}
var confirmResult = confirm("你確認要刪除" + loginAcct + "用戶嗎?");
if (!confirmResult) {
return;
}
$.ajax({
"url": "admin/batchAdminList.action",
"type": "post",
"contentType": "application/json;charset=UTF-8",
"data": adminListId,
"dataType": "json",
"success": function (response) {
var result = response.result;
if (result == 'SUCCESS') {
window.location.href = "admin/queryAdmin.action?pageNum=${requestScope['PAGEINFO-ADMIN'].pageNum}&keyword=${param.keyword}";
}
if (result == 'FAILED') {
alert(response.message);
result;
}
},
"error": function (response) {
alert(response.message);
return;
}
});
}
后台實現
AdminController
@ResponseBody
@RequestMapping(value = "/batchAdminList", method = RequestMethod.POST)
public ResultEntity<String> batchAdminList(@RequestBody List<Integer> adminListId) {
try {
adminService.batchAdminList(adminListId);
return ResultEntity.successWithoutData();
} catch (Exception e) {
return ResultEntity.failed(null, e.getMessage());
}
}
說明:
@RequestBody主要用來接收前端傳遞給后端的json字符串中的數據的(請求體中的數據的);GET方式無請求體,所以使用@RequestBody接收數據時,前端不能使用GET方式提交數據,而是用POST方式進行提交。在后端的同一個接收方法里,@RequestBody與@RequestParam()可以同時使用,@RequestBody最多只能有一個,而@RequestParam()可以有多個。
注:一個請求,只有一個RequestBody;一個請求,可以有多個RequestParam。
注:當同時使用@RequestParam()和@RequestBody時,@RequestParam()指定的參數可以是普通元素、 數組、集合、對象等等(即:當,@RequestBody 與@RequestParam()可以同時使用時,原SpringMVC接收參數的機制不變,只不過RequestBody 接收的是請求體里面的數據;而RequestParam接收的是key-value
里面的參數,所以它會被切面進行處理從而可以用普通元素、數組、集合、對象等接收)。
即:如果參數時放在請求體中,傳入后台的話,那么后台要用@RequestBody才能接收到;如果不是放在請求體中的話,那么后台接收前台傳過來的參數時,要用@RequestParam來接收,或則形參前什么也不寫也能接收。
說明@ResponseBody是作用在方法上的,@ResponseBody 表示該方法的返回結果直接寫入 HTTP response Body 中,一般在異步獲取數據時使用【也就是AJAX】,在使用 @RequestMapping后,返回值通常解析為跳轉路徑,但是加上 @ResponseBody 后返回結果不會被解析為跳轉路徑,而是直接寫入 HTTP response Body 中。 比如異步獲取 json 數據,加上 @ResponseBody 后,會直接返回 json 數據。@RequestBody 將 HTTP 請求正文插入方法中,使用適合的 HttpMessageConverter 將請求體寫入某個對象。
統一響應的實體
package com.zfcoding.common;
/**
* @author 13394
*/
public class ResultEntity<T> {
public static final String SUCCESS = "SUCCESS";
public static final String FAILED = "FAILED";
public static final String NO_MESSAGE = "NO_MESSAGE";
public static final String NO_DATA = "NO_DATA";
// 方便返回成功結果(不攜帶查詢結果情況)
public static ResultEntity<String> successWithoutData() {
return new ResultEntity<String>(SUCCESS, NO_MESSAGE, NO_DATA);
}
// 方便返回成功結果(攜帶查詢結果情況)
public static <E> ResultEntity<E> successWithoutData(E data) {
return new ResultEntity<E>(SUCCESS, NO_MESSAGE, data);
}
// 方便返回失敗結果
public static <E> ResultEntity<E> failed(E data, String message) {
return new ResultEntity<E>(FAILED, message, data);
}
private String result;
private String message;
private T data;
public ResultEntity() {
}
public ResultEntity(String result, String message, T data) {
super();
this.result = result;
this.message = message;
this.data = data;
}
@Override
public String toString() {
return "ResultEntity [result=" + result + ", message=" + message + ", data=" + data + "]";
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
AdminService
public void batchAdminList(List<Integer> list) {
adminMapper.batchAdminList(list);
}
AdminMapper及AdminMapper.xml
void batchAdminList(List<Integer> list);
<delete id="batchAdminList" parameterType="java.util.List">
delete from t_admin where id in
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</delete>
到這里批量刪除的功能就實現了。
文章參考:https://blog.csdn.net/justry_deng/article/details/80972817