删除问题!!!!紧急
<%@ page import="java.util.Map" %> <%@ page import="java.util.List" %> <%@ page import="java.util.HashMap" %> <%@ page import="com.syc.service.CourseServiceImpl" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <script src="<%=basePath%>/js/jquery-3.2.1.js"></script> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>管理员查询</title> </head> <body> <center> <h1>管理员查询</h1> <hr> <table cellspacing="0px" cellpadding="0px" border="1px" width="600px"> <thead> <tr> <th>用户名</th> <th>密码</th> <th>类型</th> <th>操作</th> </tr> </thead> <tbody id="cont"> <c:forEach items="${userList}" var="user"> <tr> <td>${user.username}</td> <td>${user.password}</td> <td> ${user.type} </td> <td><a id="delete" data-value="${user.username}" onclick="shanchu(this)" >删除</a></td> </tr> </c:forEach> </tbody> </table> </center> </body> <script type="text/javascript"> function shanchu(e){ // function refresh () { var value=e.getAttribute("data-value"); alert(value); $.ajax({ url:"<%=basePath%>/DeleteServlet", type:"post", data:{ username:value }, dataType:"json", success:function (result) { var list = eval(result); console.log(list); var content = null; if(list != null){ for(var i in list){ var username = list[i].username; var password = list[i].password; var type = list[i].type; content = content + "<tr><td>" + username + "</td><td>" + password + "</td><td>" + type + "</td></tr>"; } $("#cont").remove(); $("#cont").html(content); } } }); } </script> </html>
package com.syc.servlet; import com.syc.service.CourseServiceImpl; import org.json.JSONArray; import org.json.JSONObject; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; import java.util.Map; @WebServlet(name = "DeleteServlet") public class DeleteServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("删除启动"); CourseServiceImpl courseServiceImpl = new CourseServiceImpl(); List<Map<String,Object>> userList = courseServiceImpl.getAllUsers(); System.out.println("1"+userList); String username = request.getParameter("username"); System.out.println(username); courseServiceImpl.delUser(username); System.out.println("删除完毕"); System.out.println("2"+userList); JSONArray jsonArray = new JSONArray(userList); response.getOutputStream().write(jsonArray.toString().getBytes("utf-8")); } }
public void delUser(String username) { for(Map<String,Object> map:usersTable){ if(username.equals(map.get("username"))){ usersTable.remove(map); break; } } }
报错及代码如上,可以删除管理员但是得重新刷新下页面。求解
0
收起
正在回答 回答被采纳积分+1
2回答
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星