您好,登錄后才能下訂單哦!
分析:
通過(guò)form表單傳遞數(shù)據(jù),刪除數(shù)據(jù)根據(jù)id編號(hào)刪除。
前臺(tái)
<a class="btn btn-primary" href="javascript:void(0);" rel="external nofollow" id="delSelected">刪除選中</a> </div> <form id="form" action="${pageContext.request.contextPath}/delSelectedServlet" method="post"> <table border="1" class="table table-bordered table-hover"> <tr class="success"> <th><input type="checkbox" id="firstCb"></th> <th>編號(hào)</th> <th>姓名</th> <th>性別</th> <th>年齡</th> <th>籍貫</th> <th>QQ</th> <th>郵箱</th> <th>操作</th> </tr> <c:forEach items="${users}" var="user" varStatus="s"> <tr> <td><input type="checkbox" name="uid" value="${user.id}"></td> <td>${s.count}</td> <td>${user.name}</td> <td>${user.gender}</td> <td>${user.age}</td> <td>${user.address}</td> <td>${user.qq}</td> <td>${user.email}</td> <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/findUserServlet?id=${user.id}" rel="external nofollow" >修改</a> <a class="btn btn-default btn-sm" href="javascript:deleteUser(${user.id});" rel="external nofollow" >刪除</a></td> </tr> </c:forEach> </table> </form>
Js
<script> function deleteUser(id) { //用戶(hù)安全提示 if (confirm("您確定要?jiǎng)h除嗎?")){ //訪問(wèn)的路徑 location.href=" ${pageContext.request.contextPath}/delUserServlet?id=" rel="external nofollow" +id; } } window.οnlοad=function () { //給刪除選中按鈕添加單擊事件 document.getElementById("delSelected").οnclick=function () { if (confirm("您確定要?jiǎng)h除選中條目嗎?")) //表單提交 document.getElementById("form").submit(); } //1.獲取第一個(gè)cb document.getElementById("firstCb").οnclick=function () { //獲取下表所有的cb var cbs=document.getElementsByName("uid"); //遍歷 for (var i=0;i<cbs.length;i++){ //設(shè)置這些cbs[i]的checked狀態(tài)=firstCb.checked cbs[i].checked =this.checked; } } } </script>
后臺(tái)
Servlet
@WebServlet("/delSelectedServlet") public class DelSelectedServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //獲取所有id String[] ids=request.getParameterValues(“uid”); //調(diào)用service刪除 UserService service=new UserServiceImpl(); service.delSelectedUser(ids); //跳轉(zhuǎn)查詢(xún)Servlet response.sendRedirect(request.getContextPath()+"/userListServlet"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
Service
public void delSelectedUser(String[] ids) { //遍歷數(shù)組 for (String id:ids){ //調(diào)用dao刪除 dao.deleteUser(Integer.parseInt(id)); } }
Dao
public void updateUser(User user) { String sql=“update userms set name=?,age=?,gender=?,address=?,qq=?,email=? where id=?”; template.update(sql,user.getName(),user.getAge(),user.getGender(),user.getAddress(),user.getQq(),user.getEmail(),user.getId()); }
總結(jié)
以上所述是小編給大家介紹的java實(shí)現(xiàn)選中刪除功能的實(shí)例代碼,希望對(duì)大家有所幫助!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。