生活随笔
收集整理的這篇文章主要介紹了
批量删除实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
用到的知識點: jQuery prop() 方法:http://www.runoob.com/jquery/html-prop.html JavaScript push() 方法:http://www.runoob.com/jsref/jsref-push.html JavaScript join() 方法:http://www.runoob.com/jsref/jsref-join.html jQuery [name=“value”][name2="value2″] 選擇器: http://www.runoob.com/jquery/sel-multipleattribute-equal-value.html
jQuery each() 方法:http://www.runoob.com/jquery/traversing-each.html JavaScript length 屬性:http://www.runoob.com/jsref/jsref-length-array.html
commentList.jsp文件:
<%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding="utf-8"%>
<!--c標簽引入-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!--fn標簽引入,用于截取顯示字數-->
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<!--fmt標簽引入,格式化日期-->
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
<script type="text/javascript">
<!--當選中最上面那個單選框時其它的都會選中-->$(document).ready(function(){$("#checkedAll").click(function(){if($(this).prop("checked")==true){$("input[name='commentIds']").prop("checked",true);}else{$("input[name='commentIds']").prop("checked",false);}});});<!--批量刪除-->function commentsDelete(){var chk_value=[];$('input[name="commentIds"]:checked').each(function(){chk_value.push($(this).val());});alert(chk_value);if(chk_value.length==0){alert("請選擇要刪除的數據");return;}var commentIds=chk_value.join(",");if(confirm("確認要刪除這些評論嗎?")){$.post("comment?action=delete",{commentIds:commentIds},function(result){var result=eval('('+result+')');if(result.success){alert("成功刪除"+result.delNums+"條數據");window.location.href="${pageContext.request.contextPath}/comment?action=backList";}else{alert(result.errorMsg);}});}}<!--點擊后面的刪除進行單個刪除-->function commentDelete(commentId){if(confirm("確認要刪除這條評論嗎?")){$.post("comment?action=delete",{commentIds:commentId},function(result){var result=eval('('+result+')');if(result.success){alert("刪除成功!");window.location.href="${pageContext.request.contextPath}/comment?action=backList";}else{alert(result.errorMag);}});}}</script>
</head>
<body>
<div class="data_list backMain"><div class="dataHeader navi">${navCode}</div><div class="search_content" style="vertical-align: middle;padding-right: 20px;"><div style="float: left;padding-top: 10px;"><button class="btn btn-mini btn-danger" type="button" "commentsDelete()">批量刪除</button></div><div style="float: right;"><form action="${pageContext.request.contextPath}/comment?action=backList" method="post">評論日期:<input type="text" id="s_bCommentDate" name="s_bCommentDate" class="Wdate" "WdatePicker()" style="width: 100px;" value="${s_bCommentDate }"/> 到 <input type="text" id="s_aCommentDate" name="s_aCommentDate" class="Wdate" "WdatePicker()" style="width: 100px;" value="${s_aCommentDate }"/> <button class="btn btn-mini btn-primary" type="submit" style="margin-top: -8px;">查詢</button></form></div></div><div class="data_content"><table class="table table-hover table-bordered"><tr><th><input type="checkbox" id="checkedAll"/></th><th>序號</th><th>評論內容</th><th>評論主題</th><th>評論時間</th><th>操作</th></tr><c:forEach var="commentBack" items="${commentBackList }" varStatus="status"><tr><td><input type="checkbox" name="commentIds" value="${commentBack.commentId}"/></td><td>${status.index+1 }</td><td><a title="${commentBack.content }">${fn:substring(commentBack.content,0,15) }...</a></td><td><a title="${commentBack.newsTitle }" target="_blank" href="news?action=show&newsId=${commentBack.newsId }">${fn:substring(commentBack.newsTitle,0,15) }...</a></td><td><fmt:formatDate value="${commentBack.commentDate }" type="date" pattern="yyyy-MM-dd"/></td><td><button class="btn btn-mini btn-danger" type="button" "commentDelete(${commentBack.commentId})">刪除</button></td></tr></c:forEach></table></div><div class="pagination pagination-centered"><ul>${pageCode }</ul></div>
</div>
</body>
</html>
頁面效果: CommentServlet.java(請求的servlet)
package com.java1234.web;import java.io.IOException;
import java.sql.Connection;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.java1234.dao.CommentDao;
import com.java1234.model.Comment;
import com.java1234.model.PageBean;
import com.java1234.util.DbUtil;
import com.java1234.util.NavUtil;
import com.java1234.util.PageUtil;
import com.java1234.util.PropertiesUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;import net.sf.json.JSONObject;public class CommentServlet extends HttpServlet{/*** */private static final long serialVersionUID = 1L;DbUtil dbUtil=new DbUtil();CommentDao commentDao=new CommentDao();@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8");String action=request.getParameter("action");if("save".equals(action)) {commentSave(request,response);}else if("backList".equals(action)){commentBackList(request,response);}else if("delete".equals(action)) {//調用刪除方法,看這就行commentDelete(request,response);}}private void commentSave(HttpServletRequest request, HttpServletResponse response) {String newsId=request.getParameter("newsId");String content=request.getParameter("content");String userIP=request.getRemoteAddr();Comment comment=new Comment(Integer.parseInt(newsId), content, userIP);Connection con=null;try {con=dbUtil.getCon();commentDao.commentAdd(con, comment);request.getRequestDispatcher("news?action=show&newsId="+newsId).forward(request, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {dbUtil.closeCon(con);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}private void commentBackList(HttpServletRequest request, HttpServletResponse response) {String s_bCommentDate=request.getParameter("s_bCommentDate");String s_aCommentDate=request.getParameter("s_aCommentDate");String page=request.getParameter("page");HttpSession session=request.getSession();if(StringUtil.isEmpty(page)) {page="1";session.setAttribute("s_bCommentDate", s_bCommentDate);session.setAttribute("s_aCommentDate", s_aCommentDate);}else {s_bCommentDate=(String) session.getAttribute("s_bCommentDate");s_aCommentDate=(String) session.getAttribute("s_aCommentDate");}Connection con=null;try {con=dbUtil.getCon();int total=commentDao.commentCount(con, new Comment(), s_bCommentDate, s_aCommentDate);String pageCode=PageUtil.getPagation(request.getContextPath()+"/comment?action=backList", total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("backPageSize")));request.setAttribute("pageCode", pageCode);PageBean pageBean=new PageBean(Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("backPageSize")));List<Comment> commentBackList=commentDao.commentList(con, new Comment(),pageBean,s_bCommentDate,s_aCommentDate);request.setAttribute("commentBackList", commentBackList);request.setAttribute("navCode", NavUtil.genNewsManagerNavigation("新聞評論管理", "新聞評論維護"));request.setAttribute("mainPage", "/background/comment/commentList.jsp");request.getRequestDispatcher("/background/mainTemp.jsp").forward(request, response);}catch(Exception e) {e.printStackTrace();}finally {try {dbUtil.closeCon(con);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
/**
**刪除方法
**/private void commentDelete(HttpServletRequest request, HttpServletResponse response) {String commentIds=request.getParameter("commentIds");Connection con=null;try {con=dbUtil.getCon();JSONObject result=new JSONObject();//調用CommentDao中的刪除方法int delNums=commentDao.commentDelete(con, commentIds);if(delNums>0) {result.put("success", true);result.put("delNums", delNums);}else {result.put("errorMsg", "刪除失敗!");}ResponseUtil.write(result, response);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {dbUtil.closeCon(con);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
調用CommentDao.java中的commentDelete方法:
public int commentDelete(Connection con,String commentIds)throws Exception{String sql="delete from t_comment where commentId in ("+commentIds+")";PreparedStatement pstmt=con.prepareStatement(sql);return pstmt.executeUpdate();}
總結
以上是生活随笔 為你收集整理的批量删除实现 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。