新增分类点击删除后 ,全局变量categoryList并没变化,点击刷新后不应该是没删除前的数据吗
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>图书管理系统</title>
<script type="text/javascript">
</script>
</head>
<link rel="stylesheet" type="text/css" href="css/index.css">
<script type="text/javascript" src="js/index.js" defer="true"></script>
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript">
function del(s){
var s=s.id;
alert(s);
$("#table tr:not(:first)").html("");
$(function(){
$.ajax({
"url":"/ManageSystem/delcategory",
"type":"get",
"data":{id:s},
"dataType":"json",
"success":function(json){
for(var i=0;i<json.length;i++){
var s=i+1;
var vtd=$("<tr>"+"<td>"+s+"</td>"+"<td>"+json[i].cgid+ "</td>"+"<td>"+json[i].cgname+"</td>"+"<td>"+"<input type='button' onclick='del(this)' id="+json[i].cgid+" value='删除' >"+"</td>"+"</tr>")
$("#table").append(vtd);
}
},
})
})
}
</script>
<body>
<div class="head">
<input type="button" onclick="location.href='http://localhost:8080/ManageSystem/index.jsp'" value="分类管理" name="">
<input type="button" onclick="location.href='http://localhost:8080/ManageSystem/msbook.jsp'" value="图书信息管理" name="">
</div>
<div class="banner">
<span class="f1">图书管理系统</span></br>
<span class="f2">图书分类管理</span>
</div>
<div class="searche">
<input type="text" id="" name="" placeholder="请输入要查询的分类">
<input type="button" name="" value="查询">
</div>
<div class="main">
<table name="tb" id="table" >
<tr class="trname">
<td class="tdname">序号</td>
<td class="tdname">分类编号</td>
<td class="tdname">分类名称</td>
<td class="tdname">操作</td>
</tr>
<c:forEach items="${applicationScope.categoryList}" var="c" varStatus="indx" >
<tr class="trname">
<td class="tdname" name="tdnum">${indx.index+1}</td>
<td class="tdname" name="tdid">${c.cgid}</td>
<td class="tdname" name="tdname">${c.cgname}</td>
<td class="tdname" name="tddel"><input type="button" onclick="del(this)" value="删除" id="${c.cgid}"></td>
</tr>
</c:forEach>
</table>
</div>
<div class="add">
<a href="${pageContext.request.contextPath}/newclass.jsp"><input type="button" id="createbtn" name="" value="新建"></a>
</div>
</body>
</html>
package com.system.mianservlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 com.system.categoryserviceImpl.CategoryServiceImpl;
/**
* Servlet implementation class CreateClServlet
*/
@WebServlet("/createclass")
public class AddCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List categoryList=new ArrayList();
String id=request.getParameter("id");
String classname=request.getParameter("classname");
if(id=="") {
CategoryServiceImpl cGsi=new CategoryServiceImpl();
categoryList=cGsi.getCategorList();
//request.setAttribute("categoryList", categoryList);
request.getServletContext().setAttribute("categoryList", categoryList);
//request.getSession().setAttribute("categoryList", categoryList);
System.out.println(categoryList);
response.sendRedirect("/ManageSystem/index.jsp");
//request.getRequestDispatcher("/index.jsp").forward(request, response);
}else {
CategoryServiceImpl cGsi=new CategoryServiceImpl();
cGsi.addCatgory(id, classname);
categoryList=cGsi.getCategorList();
request.getServletContext().setAttribute("categoryList", categoryList);
// request.setAttribute("categoryList", categoryList);
//request.getSession().setAttribute("categoryList", categoryList);
System.out.println(categoryList);
// request.getRequestDispatcher("/index.jsp").forward(request, response);
response.sendRedirect("/ManageSystem/index.jsp");
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
package com.system.mianservlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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 com.alibaba.fastjson.JSON;
import com.system.categoryserviceImpl.CategoryServiceImpl;
/**
* Servlet implementation class DelCategory
*/
@WebServlet("/delcategory")
public class DelCategory extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id=(String)request.getParameter("id");
System.out.println(id);
List ctList=new ArrayList();
CategoryServiceImpl ctg=new CategoryServiceImpl();
ctg.deleteCatgory(id);
ctList=(List)ctg.getCategorList();
System.out.println("删除后的数据"+ctList);
// request.getServletContext().setAttribute("categoryList", ctList);
String json=JSON.toJSONString(ctList);
response.getWriter().println(json);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
正在回答 回答被采纳积分+1
同学你好,同学理解的不正确,在createclass中,是从CategoryServiceImpl类中获取数据存入servletContext中,如:
servletContext中存入的是一个对象的引用,具体指向CategoryServiceImpl类中数据,所以当CategoryServiceImpl类中的数据改变时,servletContext中的数据也发生了改变。
举例说明一下:
上边代码中,list集合添加aaa,bbb两个数据,将集合存入servletContext中,获取数据并循环输出。
然后再向集合中添加ccc数据,直接获取servletContext中的数据进行输出,结果如下:
也就是说servletContext中存放的list集合的引用,当list集合发生变化时,servletContext中的数据也发生变化。
所以刷新后获取的删除后的数据,而不是新增后未删除的数据。
祝:学习愉快~
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星