删除问题!!!!紧急
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | <%@ 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 > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 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" )); } } |
1 2 3 4 5 6 7 8 9 | 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 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧