一. 删除一个商品案例
将要操作的表格
思路图
前端代码
<%@ 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>Insert title here</title> </head> <body> <button> <a href="/day13/GetAllProducts">查詢商品列表</a> </button> <br /> <br /> <br /> <table border="1px" cellspacing="0" width="100%"> <tr> <th>id</th> <th>name</th> <th>category</th> <th>pnum</th> <th>description</th> <th>描述</th> </tr> <!-- 迭代获取数据,即遍历 --> <c:forEach items="${p_list }" var="product"> <tr> <td>${product.id}</td> <td>${product.name}</td> <td>${product.price}</td> <td>${product.category}</td> <td>${product.pnum}</td> <td>${product.description}</td> <td> <a href="/day13/DeleteOneProduct?id=${product.id }">删除</a> </td> </c:forEach> </table> </body> </html>
此处删除直接使用a标签发送请求,用form表单也可以,但写起来麻烦:
后段部分代码
servlet
DeleteOneProduct
public class DeleteOneProduct extends HttpServlet { private static final long serialVersionUID = 1L; DeleteOneService deleteOneService = new DeleteOneServiceImpl(); // 删除一个商品 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); try { deleteOneService.deleteOneProduct(id); response.sendRedirect("/day13/GetAllProducts"); } catch (Exception e) { e.printStackTrace(); } } }
service层
DeleteOneService接口
public interface DeleteOneService { /** * 删除一个商品 */ public void deleteOneProduct(String id) throws Exception; }
接口实现类(DeleteOneServiceImpl)
public class DeleteOneServiceImpl implements DeleteOneService{ DeleteOneDao deleteOneDao = new DeleteOneDaoImpl(); @Override public void deleteOneProduct(String id) throws Exception { deleteOneDao.deleteOneProductFromDB(id); } }
dao层
DeleteOneDao接口
public interface DeleteOneDao { /** * 从数据库删除一个商品 */ public void deleteOneProductFromDB(String id) throws Exception; }
接口实现类(DeleteOneDaoImpl)
public class DeleteOneDaoImpl implements DeleteOneDao { // 创建数据库连接池,并放在静态代码块中 static QueryRunner runner; static { ComboPooledDataSource dataSource = new ComboPooledDataSource(); runner = new QueryRunner(dataSource); } /** * 从数据库中删除数据 */ @Override public void deleteOneProductFromDB(String id) throws SQLException { String sql = "delete from products where id= ?"; runner.update(sql, id); } }
二. 删除多个商品案例
思路:删除一个商品是前端发送删除请求(携带一个商品的id),删除多个商品则是前端发送删除请求(携带多个商品的id)
前端该如何选中多个商品呢?----->多选框,此处只能使用form表单,不然不能将多个id传至后端
前端代码
<%@ 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>Insert title here</title> </head> <body> <button> <a href="/day13/GetAllProducts">查詢商品列表</a> </button> <br /> <br /> <br /> <form action="/day13/DeleteManyProducts" method="POST"> <input type="submit" value="删除多个商品" /> <table border="1px" cellspacing="0" width="100%"> <tr> <th>id</th> <th>name</th> <th>category</th> <th>pnum</th> <th>description</th> <th>描述</th> </tr> <!-- 迭代获取数据,即遍历 --> <c:forEach items="${p_list }" var="product"> <tr> <td><input type="checkbox" name="id" value="${product.id}" /></td> <td>${product.name}</td> <td>${product.price}</td> <td>${product.category}</td> <td>${product.pnum}</td> <td>${product.description}</td> <td> <a href="/day13/DeleteOneProduct?id=${product.id }">删除</a> </td> </c:forEach> </table> </form> </body> </html>
部分截图
后段部分
DeleteManyProduct
public class DeleteManyProducts extends HttpServlet { private static final long serialVersionUID = 1L; ProductsService productsService = new ProductsServiceImpl(); protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接收参数 String[] idList = request.getParameterValues("id"); for (String id : idList) { try { productsService.deleteManyProducts(id); } catch (Exception e) { e.printStackTrace(); } } response.sendRedirect("/day13/GetAllProducts"); } }
其他部分类似删除第一个商品的案例
三 编辑(修改商品信息)
修改商品信息
(1)修改页面:添加一个修改商品的编辑页面
(2)点击编辑按钮,将当前的id发送到后台,后台根据此id查询商品
(3)将要修改的商品展示到点击编辑后跳转的页面上
(4)修改,将修改完的数据提交到后台,后台完成修改
(5)跳转到页面展示
原文地址:https://www.cnblogs.com/jj1106/p/11632581.html
时间: 2024-10-02 22:33:18