老师你好。点击图书分类删除按钮
老师,我在图书分类中点击删除效果是出来了,但是没想明白怎么实现的,我不是删除之后保存到session作用域去么,怎么在列表显示的时候还是成功删除了,是和静态常量有关么
package com.imooc.servlet;
import java.io.IOException;
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.imooc.domain.Category;
import com.imooc.service.impl.CategoryServiceImpl;
/**
* Servlet implementation class AddCategory
*/
@WebServlet("/addCategory")
public class AddCategory 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 {
//获取id号和分类名称
String categoryId =request.getParameter("categoryId");
String categoryName = request.getParameter("categoryName");
System.out.println(categoryId);
System.out.println(categoryName);
//创建对象保存
CategoryServiceImpl c = new CategoryServiceImpl();
c.addCatgory(categoryId, categoryName);
//保存到session作用域
request.getSession().setAttribute("categoryDb",c.getCategorydb());
System.out.println(c.getCategorydb().size());
System.out.println(request.getContextPath());
response.sendRedirect(request.getContextPath()+"/categoryList.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.imooc.servlet;
import java.io.IOException;
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.imooc.domain.Category;
import com.imooc.service.impl.CategoryServiceImpl;
/**
* Servlet implementation class DeleteCategory
*/
@WebServlet("/deleteCategory")
public class DeleteCategory 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 {
//获得id
String categoryId = request.getParameter("categoryId");
//删chu
CategoryServiceImpl c = new CategoryServiceImpl();
c.deleteCatgory(categoryId);
request.getRequestDispatcher("/categoryList.jsp").forward(request, response);;
}
/**
* @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);
}
}<%@ 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>
<meta charset="UTF-8">
<title>图书后台管理</title>
<link rel="stylesheet" href="css/index.css">
<link rel="stylesheet" href="css/bootstrap.min.css">
</head>
<body>
<header>
<div class="container">
<nav>
<a href="${pageContext.request.contextPath }/bookList.jsp" >图书信息管理</a>
</nav>
<nav>
<a href="${pageContext.request.contextPath }/categoryList.jsp" >分类管理</a>
</nav>
</div>
</header>
<section class="banner">
<div class="container">
<div>
<h1>图书管理系统</h1>
<p>图书分类管理</p>
</div>
</div>
</section>
<section class="main">
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<th>序号</th>
<th>分类编号</th>
<th>分类名称</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${categoryDb}" var="c" varStatus="idx">
<tr>
<td>${idx.index + 1}</td>
<td>${c.id}</td>
<td>${c.flname}</td>
<td><a href="${pageContext.request.contextPath }/deleteCategory?categoryId=${idx.index}">删除</a></td>
<!--在循环显示数据时,此处的ca0001可以用EL表达式进行替换-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</section>
<section class="page">
<div class="container">
<div id="fatie">
<a href="${pageContext.request.contextPath }/addCategory.jsp"><button>新建</button></a>
</div>
</div>
</section>
<footer>
copy@慕课网
</footer>
</body>
</html>
<%@ 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>
<meta charset="UTF-8">
<title>新建图书分类</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="">
图书分类管理
</a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, XXX!</h1>
<p>请小心地新增图书分类,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3><small>新建</small></h3>
</div>
<form class="form-horizontal" action="${pageContext.request.contextPath }/addCategory" method="post">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">分类ID :</label>
<div class="col-sm-8">
<input name="categoryId" class="form-control" id="categoryId">
</div>
</div>
<div class="form-group">
<label for="name" class="col-sm-2 control-label">分类名称 :</label>
<div class="col-sm-8">
<input name="categoryName" class="form-control" id="categoryName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center" >
copy@imooc
</footer>
</body>
</html>16
收起
正在回答
1回答
同学你好!
你的想法正确。seesion中存入的数据其实是静态常量地址。所以静态常量的值改变之后seesion中读取出来的数据也就改变了
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星