💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] # 步骤 1 : 先运行,看到效果,再学习 先将完整的项目(向老师要相关资料),配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 # 步骤 2 : 模仿和排错 在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。 模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较**正确答案** ( 可运行项目 ) 和自己的代码,来定位问题所在。 采用这种方式,**学习有效果,排错有效率**,可以较为明显地提升学习速度,跨过学习路上的各个槛。 # 步骤 3 : 页面截图 重启tomcat,通过访问地址 `http://127.0.0.1:8080/tmall_j2ee/admin_product_list?cid=12` 可以看到产品管理的界面 注: 这cid=12是分类的id,根据你的实际运行情况,采取不同的id值 ![](https://box.kancloud.cn/bf36791a01a32e09a66025b48239cb03_1052x277.png) # 步骤 4 : 在分类管理页面上添加产品管理的超链 接下来讲, 接着上一步**属性管理可运行项目**做了哪些工作以支持产品管理的操作。 首先,在listCategory.jsp里,加上产品管理这一列 ![](https://box.kancloud.cn/8dd06c2346ca5835e58bb6612b4309c2_137x313.png) ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@include file="../include/admin/adminHeader.jsp"%> <%@include file="../include/admin/adminNavigator.jsp"%> <script> $(function(){ $("#addForm").submit(function(){ if(!checkEmpty("name","分类名称")) return false; if(!checkEmpty("categoryPic","分类图片")) return false; return true; }); }); </script> <title>分类管理</title> <div class="workingArea"> <h1 class="label label-info" >分类管理</h1> <br> <br> <div class="listDataTableDiv"> <table class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr class="success"> <th>ID</th> <th>图片</th> <th>分类名称</th> <th>属性管理</th> <th>产品管理</th> <th>编辑</th> <th>删除</th> </tr> </thead> <tbody> <c:forEach items="${thecs}" var="c"> <tr> <td>${c.id}</td> <td><img height="40px" src="img/category/${c.id}.jpg"></td> <td>${c.name}</td> <td><a href="admin_property_list?cid=${c.id}"><span class="glyphicon glyphicon-th-list"></span></a></td> <td><a href="admin_product_list?cid=${c.id}"><span class="glyphicon glyphicon-shopping-cart"></span></a></td> <td><a href="admin_category_edit?id=${c.id}"><span class="glyphicon glyphicon-edit"></span></a></td> <td><a deleteLink="true" href="admin_category_delete?id=${c.id}"><span class=" glyphicon glyphicon-trash"></span></a></td> </tr> </c:forEach> </tbody> </table> </div> <div class="pageDiv"> <%@include file="../include/admin/adminPage.jsp" %> </div> <div class="panel panel-warning addDiv"> <div class="panel-heading">新增分类</div> <div class="panel-body"> <form method="post" id="addForm" action="admin_category_add" enctype="multipart/form-data"> <table class="addTable"> <tr> <td>分类名称</td> <td><input id="name" name="name" type="text" class="form-control"></td> </tr> <tr> <td>分类圖片</td> <td> <input id="categoryPic" accept="image/*" type="file" name="filepath" /> </td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <button type="submit" class="btn btn-success">提 交</button> </td> </tr> </table> </form> </div> </div> </div> <%@include file="../include/admin/adminFooter.jsp"%> ``` # 步骤 5: 修改实体类Product.java 1\. 与数据库相关字段一一对应的基本属性 2\. 与Category的多对一关系 3\. firstProductImage这个属性,用于显示这个产品的默认图片,所以需要查询产品单个图片。 > 产品管理需要用到默认图片 ![](https://box.kancloud.cn/422eaa21569d8a3eb4f9a16c1ffffa4d_1247x107.png) ``` package com.dodoke.bean; import java.util.Date; /******************************************************************************* * javaBeans t_product --> TProduct <table explanation> * * @author 2019-01-17 08:35:28 * */ public class Product { // field /** ID **/ private int id; /** 名称 **/ private String name; /** 子标题 **/ private String subTitle; /** 原价格 **/ private float originalPrice; /** 促销价 **/ private float promotePrice; /** 仓库 **/ private int stock; /** 创建时间 **/ private Date createDate; /** 所属分类 **/ private Category category; private ProductImage firstProductImage; // method public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSubTitle() { return subTitle; } public void setSubTitle(String subTitle) { this.subTitle = subTitle; } public float getOriginalPrice() { return originalPrice; } public void setOriginalPrice(float originalPrice) { this.originalPrice = originalPrice; } public float getPromotePrice() { return promotePrice; } public void setPromotePrice(float promotePrice) { this.promotePrice = promotePrice; } public int getStock() { return stock; } public void setStock(int stock) { this.stock = stock; } public Date getCreateDate() { return createDate; } public void setCreateDate(Date createDate) { this.createDate = createDate; } public Category getCategory() { return category; } public void setCategory(Category category) { this.category = category; } public ProductImage getFirstProductImage() { return firstProductImage; } public void setFirstProductImage(ProductImage firstProductImage) { this.firstProductImage = firstProductImage; } @Override public String toString() { return "Product [id=" + id + ", name=" + name + ", subTitle=" + subTitle + ", originalPrice=" + originalPrice + ", promotePrice=" + promotePrice + ", stock=" + stock + ", createDate=" + createDate + ", category=" + category + "]"; } } ``` # 步骤 6: 产品图片ProductImage 1\. 基础属性的getter、setter 2\. **与Product的多对一关系** ``` package com.dodoke.bean; /******************************************************************************* * javaBeans t_product_image --> TProductImage <table explanation> * * @author 2019-01-17 08:35:28 * */ public class ProductImage { // field /** ID **/ private int id; /** 类型 **/ private String type; /** 所属产品 **/ private Product product; // method public int getId() { return id; } public void setId(int id) { this.id = id; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } @Override public String toString() { return "ProductImage [id=" + id + ", type=" + type + ", product=" + product + "]"; } } ``` # 步骤 7: ProductDao ProductDao:: ``` package com.dodoke.dao.inter; import java.util.List; import com.dodoke.bean.Product; public interface ProductDao { /** * 增加 * * @param product */ public void add(Product product); /** * 删除 * * @param id */ public void delete(int id); /** * 修改 * * @param product */ public void update(Product product); /** * 根据id获取 * * @param id * @return */ public Product get(int id); /** * 某个分类下的产品分页查询 * * @param cid * @param start * @param count * @return */ public List<Product> list(int cid, int start, int count); /** * 查询某个分类下的所有产品 * * @return */ public List<Product> list(int cid); /** * 获取某个分类下的产品总数 * * @return */ public int getTotal(int cid); } ``` # 步骤 8: ProductImageDao 两种静态属性分别表示单个图片和详情图片 ``` public static final String type\_single = "type\_single"; public static final String type\_detail = "type\_detail"; ``` ``` package com.dodoke.dao.inter; import java.util.List; import com.dodoke.bean.Product; import com.dodoke.bean.ProductImage; public interface ProductImageDao { public static final String type_single = "type_single"; public static final String type_detail = "type_detail"; /** * 增加 * * @param productImage */ public void add(ProductImage productImage); /** * 删除 * * @param id */ public void delete(int id); /** * 修改 * * @param productImage */ public void update(ProductImage productImage); /** * 根据id获取 * * @param id * @return */ public ProductImage get(int id); /** * 某个产品下的产品图片分页查询 * * @param product * @param start * @param count * @param type * 单个图片/详情图片 * @return */ public List<ProductImage> list(Product product, String type, int start, int count); /** * 查询某个产品下的所有产品图片 * * @return */ public List<ProductImage> list(Product product, String type); /** * 获取某个产品下的产品图片总数 * * @return */ public int getTotal(int cid); } ``` # 步骤 9: ProductImageDaoImpl ``` package com.dodoke.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.dodoke.bean.Product; import com.dodoke.bean.ProductImage; import com.dodoke.dao.inter.ProductImageDao; import com.dodoke.util.DBUtil; public class ProductImageDaoImpl implements ProductImageDao { private ProductDaoImpl productDao = new ProductDaoImpl(); @Override public void add(ProductImage bean) { String sql = "insert into t_product_image(type,product_id) values(?,?)"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { ps.setString(1, bean.getType()); ps.setInt(2, bean.getProduct().getId()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { int id = rs.getInt(1); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } } @Override public void delete(int id) { String sql = "delete from t_product_image where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } @Override public void update(ProductImage productImage) { // TODO Auto-generated method stub } @Override public ProductImage get(int id) { ProductImage bean = new ProductImage(); String sql = "select * from t_product_image where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { int pid = rs.getInt("product_id"); String type = rs.getString("type"); Product product = productDao.get(pid); bean.setProduct(product); bean.setType(type); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } return bean; } @Override public List<ProductImage> list(Product p, String type, int start, int count) { List<ProductImage> beans = new ArrayList<ProductImage>(); String sql = "select * from t_product_image where product_id =? and type =? order by id desc limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, p.getId()); ps.setString(2, type); ps.setInt(3, start); ps.setInt(4, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { ProductImage bean = new ProductImage(); int id = rs.getInt("id"); bean.setProduct(p); bean.setType(type); bean.setId(id); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } @Override public List<ProductImage> list(Product p, String type) { return list(p, type, 0, Short.MAX_VALUE); } @Override public int getTotal(int cid) { return 0; } } ``` # 步骤 10: ProductDaoImpl类 一个产品有多个图片,但是只有一个主图片,把第一个图片设置为主图片 ``` public void setFirstProductImage(Product p) ``` ProductDaoImpl: ``` package com.dodoke.dao.impl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.dodoke.bean.Category; import com.dodoke.bean.Product; import com.dodoke.bean.ProductImage; import com.dodoke.dao.inter.ProductDao; import com.dodoke.dao.inter.ProductImageDao; import com.dodoke.util.DBUtil; import com.dodoke.util.DateUtil; public class ProductDaoImpl implements ProductDao { private CategoryDaoImpl categoryDao = new CategoryDaoImpl(); @Override public void add(Product bean) { String sql = "insert into t_product(name,sub_title,original_price,promote_price,stock,create_date,category_id) values(?,?,?,?,?,?,?)"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);) { ps.setString(1, bean.getName()); ps.setString(2, bean.getSubTitle()); ps.setFloat(3, bean.getOriginalPrice()); ps.setFloat(4, bean.getPromotePrice()); ps.setInt(5, bean.getStock()); ps.setTimestamp(6, DateUtil.d2t(bean.getCreateDate())); ps.setInt(7, bean.getCategory().getId()); ps.executeUpdate(); ResultSet rs = ps.getGeneratedKeys(); if (rs.next()) { int id = rs.getInt(1); bean.setId(id); } } catch (SQLException e) { e.printStackTrace(); } } @Override public void delete(int id) { String sql = "delete from t_product where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, id); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } @Override public void update(Product bean) { String sql = "update t_product set name= ?, sub_title=?, original_price=?,promote_price=?,stock=?, category_id = ?, create_date=? where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, bean.getName()); ps.setString(2, bean.getSubTitle()); ps.setFloat(3, bean.getOriginalPrice()); ps.setFloat(4, bean.getPromotePrice()); ps.setInt(5, bean.getStock()); ps.setInt(6, bean.getCategory().getId()); ps.setTimestamp(7, DateUtil.d2t(bean.getCreateDate())); ps.setInt(8, bean.getId()); ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } @Override public Product get(int id) { Product bean = new Product(); String sql = "select * from t_product where id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { String name = rs.getString("name"); String subTitle = rs.getString("sub_title"); float originalPrice = rs.getFloat("original_price"); float promotePrice = rs.getFloat("promote_price"); int stock = rs.getInt("stock"); int cid = rs.getInt("category_id"); Date createDate = DateUtil.t2d(rs.getTimestamp("create_date")); bean.setName(name); bean.setSubTitle(subTitle); bean.setOriginalPrice(originalPrice); bean.setPromotePrice(promotePrice); bean.setStock(stock); Category category = categoryDao.get(cid); bean.setCategory(category); bean.setCreateDate(createDate); bean.setId(id); setFirstProductImage(bean); } } catch (SQLException e) { e.printStackTrace(); } return bean; } public void setFirstProductImage(Product p) { List<ProductImage> pis = new ProductImageDaoImpl().list(p, ProductImageDao.type_single); if (!pis.isEmpty()) { p.setFirstProductImage(pis.get(0)); } } @Override public List<Product> list(int cid) { return list(cid, 0, Short.MAX_VALUE); } @Override public List<Product> list(int cid, int start, int count) { List<Product> beans = new ArrayList<Product>(); Category category = categoryDao.get(cid); String sql = "select * from t_product where category_id = ? order by id desc limit ?,? "; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, cid); ps.setInt(2, start); ps.setInt(3, count); ResultSet rs = ps.executeQuery(); while (rs.next()) { Product bean = new Product(); int id = rs.getInt("id"); String name = rs.getString("name"); String subTitle = rs.getString("sub_title"); float originalPrice = rs.getFloat("original_price"); float promotePrice = rs.getFloat("promote_price"); int stock = rs.getInt("stock"); Date createDate = DateUtil.t2d(rs.getTimestamp("create_date")); bean.setName(name); bean.setSubTitle(subTitle); bean.setOriginalPrice(originalPrice); bean.setPromotePrice(promotePrice); bean.setStock(stock); bean.setCreateDate(createDate); bean.setId(id); bean.setCategory(category); setFirstProductImage(bean); beans.add(bean); } } catch (SQLException e) { e.printStackTrace(); } return beans; } @Override public int getTotal(int cid) { int total = 0; String sql = "select count(*) from t_product where category_id = ?"; try (Connection c = DBUtil.getConnection(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, cid); ResultSet rs = ps.executeQuery(); while (rs.next()) { total = rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); } return total; } } ``` # 步骤 11: ProductServlet类 ``` package com.dodoke.controller; import java.util.List; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dodoke.bean.Category; import com.dodoke.bean.Product; import com.dodoke.util.Page; /** * Servlet implementation class ProductServlet */ @WebServlet("/ProductServlet") public class ProductServlet extends BaseBackServlet { private static final long serialVersionUID = 1L; @Override public String add(HttpServletRequest request, HttpServletResponse response) { int cid = Integer.parseInt(request.getParameter("cid")); Category c = categoryDao.get(cid); String name = request.getParameter("name"); String subTitle = request.getParameter("subTitle"); float originalPrice = Float.parseFloat(request.getParameter("originalPrice")); float promotePrice = Float.parseFloat(request.getParameter("promotePrice")); int stock = Integer.parseInt(request.getParameter("stock")); Product p = new Product(); p.setCategory(c); p.setName(name); p.setSubTitle(subTitle); p.setOriginalPrice(originalPrice); p.setPromotePrice(promotePrice); p.setStock(stock); productDao.add(p); return "@admin_product_list?cid=" + cid; } @Override public String delete(HttpServletRequest request, HttpServletResponse response) { int id = Integer.parseInt(request.getParameter("id")); Product p = productDao.get(id); productDao.delete(id); return "@admin_product_list?cid=" + p.getCategory().getId(); } @Override public String edit(HttpServletRequest request, HttpServletResponse response) { int id = Integer.parseInt(request.getParameter("id")); Product p = productDao.get(id); request.setAttribute("p", p); return "admin/editProduct.jsp"; } @Override public String update(HttpServletRequest request, HttpServletResponse response) { int cid = Integer.parseInt(request.getParameter("cid")); Category c = categoryDao.get(cid); int id = Integer.parseInt(request.getParameter("id")); int stock = Integer.parseInt(request.getParameter("stock")); float originalPrice = Float.parseFloat(request.getParameter("originalPrice")); float promotePrice = Float.parseFloat(request.getParameter("promotePrice")); String subTitle = request.getParameter("subTitle"); String name = request.getParameter("name"); Product p = new Product(); p.setName(name); p.setSubTitle(subTitle); p.setOriginalPrice(originalPrice); p.setPromotePrice(promotePrice); p.setStock(stock); p.setId(id); p.setCategory(c); productDao.update(p); return "@admin_product_list?cid=" + p.getCategory().getId(); } @Override public String list(HttpServletRequest request, HttpServletResponse response, Page page) { int cid = Integer.parseInt(request.getParameter("cid")); Category c = categoryDao.get(cid); List<Product> ps = productDao.list(cid, page.getStart(), page.getCount()); int total = productDao.getTotal(cid); page.setTotal(total); page.setParam("&cid=" + c.getId()); request.setAttribute("ps", ps); request.setAttribute("c", c); request.setAttribute("page", page); return "admin/listProduct.jsp"; } } ``` # 步骤12: listProduct.jsp和editProduct.jsp listProduct.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@include file="../include/admin/adminHeader.jsp"%> <%@include file="../include/admin/adminNavigator.jsp"%> <script> $(function() { $("#addForm").submit(function() { if (!checkEmpty("name", "产品名称")) return false; // if (!checkEmpty("subTitle", "小标题")) // return false; if (!checkNumber("originalPrice", "原价格")) return false; if (!checkNumber("promotePrice", "优惠价格")) return false; if (!checkInt("stock", "库存")) return false; return true; }); }); </script> <title>产品管理</title> <div class="workingArea"> <ol class="breadcrumb"> <li><a href="admin_category_list">所有分类</a></li> <li><a href="admin_product_list?cid=${c.id}">${c.name}</a></li> <li class="active">产品管理</li> </ol> <div class="listDataTableDiv"> <table class="table table-striped table-bordered table-hover table-condensed"> <thead> <tr class="success"> <th>ID</th> <th>图片</th> <th>产品名称</th> <th>产品小标题</th> <th width="53px">原价格</th> <th width="80px">优惠价格</th> <th width="80px">库存数量</th> <th width="80px">图片管理</th> <th width="80px">设置属性</th> <th width="42px">编辑</th> <th width="42px">删除</th> </tr> </thead> <tbody> <c:forEach items="${ps}" var="p"> <tr> <td>${p.id}</td> <td> <c:if test="${!empty p.firstProductImage}"> <img width="40px" src="img/productSingle/${p.firstProductImage.id}.jpg"> </c:if> </td> <td>${p.name}</td> <td>${p.subTitle}</td> <td>${p.originalPrice}</td> <td>${p.promotePrice}</td> <td>${p.stock}</td> <td><a href="admin_productImage_list?pid=${p.id}"><span class="glyphicon glyphicon-picture"></span></a></td> <td><a href="admin_propertyValue_editPropertyValue?id=${p.id}"><span class="glyphicon glyphicon-th-list"></span></a></td> <td><a href="admin_product_edit?id=${p.id}"><span class="glyphicon glyphicon-edit"></span></a></td> <td><a deleteLink="true" href="admin_product_delete?id=${p.id}"><span class=" glyphicon glyphicon-trash"></span></a></td> </tr> </c:forEach> </tbody> </table> </div> <div class="pageDiv"> <%@include file="../include/admin/adminPage.jsp"%> </div> <div class="panel panel-warning addDiv"> <div class="panel-heading">新增产品</div> <div class="panel-body"> <form method="post" id="addForm" action="admin_product_add"> <table class="addTable"> <tr> <td>产品名称</td> <td><input id="name" name="name" type="text" class="form-control"></td> </tr> <tr> <td>产品小标题</td> <td><input id="subTitle" name="subTitle" type="text" class="form-control"></td> </tr> <tr> <td>原价格</td> <td><input id="originalPrice" value="99.98" name="originalPrice" type="text" class="form-control"></td> </tr> <tr> <td>优惠价格</td> <td><input id="promotePrice" value="19.98" name="promotePrice" type="text" class="form-control"></td> </tr> <tr> <td>库存</td> <td><input id="stock" value="99" name="stock" type="text" class="form-control"></td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <input type="hidden" name="cid" value="${c.id}"> <button type="submit" class="btn btn-success">提 交</button> </td> </tr> </table> </form> </div> </div> </div> <%@include file="../include/admin/adminFooter.jsp"%> ``` editProduct.jsp ``` <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@include file="../include/admin/adminHeader.jsp"%> <%@include file="../include/admin/adminNavigator.jsp"%> <title>编辑产品</title> <script> $(function() { $("#editForm").submit(function() { if (!checkEmpty("name", "产品名称")) return false; // if (!checkEmpty("subTitle", "小标题")) // return false; if (!checkNumber("originalPrice", "原价格")) return false; if (!checkNumber("promotePrice", "优惠价格")) return false; if (!checkInt("stock", "库存")) return false; return true; }); }); </script> <div class="workingArea"> <ol class="breadcrumb"> <li><a href="admin_category_list">所有分类</a></li> <li><a href="admin_product_list?cid=${p.category.id}">${p.category.name}</a></li> <li class="active">${p.name}</li> <li class="active">编辑产品</li> </ol> <div class="panel panel-warning editDiv"> <div class="panel-heading">编辑产品</div> <div class="panel-body"> <form method="post" id="editForm" action="admin_product_update"> <table class="editTable"> <tr> <td>产品名称</td> <td><input id="name" name="name" value="${p.name}" type="text" class="form-control"></td> </tr> <tr> <td>产品小标题</td> <td><input id="subTitle" name="subTitle" type="text" value="${p.subTitle}" class="form-control"></td> </tr> <tr> <td>原价格</td> <td><input id="originalPrice" value="${p.originalPrice}" name="originalPrice" type="text" class="form-control"></td> </tr> <tr> <td>优惠价格</td> <td><input id="promotePrice" value="${p.promotePrice}" name="promotePrice" type="text" class="form-control"></td> </tr> <tr> <td>库存</td> <td><input id="stock" value="${p.stock}" name="stock" type="text" class="form-control"></td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <input type="hidden" name="id" value="${p.id}"> <input type="hidden" name="cid" value="${p.category.id}"> <button type="submit" class="btn btn-success">提 交</button></td> </tr> </table> </form> </div> </div> </div> ``` # 步骤 13 : 查询功能讲解 查询访问的是ProductServlet的list()方法 1\. 获取分类 cid 2\. 基于cid,获取当前分类下的产品集合 3\. 获取当前分类下的产品总数,并且设置给分页page对象 4\. 拼接字符串"&cid="+c.getId(),设置给page对象的Param值。 因为产品分页都是基于当前分类下的分页,所以分页的时候需要传递这个cid 5\. 把产品集合设置到 request的 "ps" 属性上 6\. 把分类对象设置到 request的 "c" 属性上 7\. 把分页对象设置到 request的 "page" 对象上 8\. 服务端跳转到admin/listProduct.jsp页面 9\. 在listProduct.jsp页面上使用c:forEach 遍历ps集合,并显示 ProductServlet片段: ``` @Override public String list(HttpServletRequest request, HttpServletResponse response, Page page) { int cid = Integer.parseInt(request.getParameter("cid")); Category c = categoryDao.get(cid); List<Product> ps = productDao.list(cid, page.getStart(), page.getCount()); int total = productDao.getTotal(cid); page.setTotal(total); page.setParam("&cid=" + c.getId()); request.setAttribute("ps", ps); request.setAttribute("c", c); request.setAttribute("page", page); return "admin/listProduct.jsp"; } ``` listProduct.jsp: ``` <c:forEach items="${ps}" var="p"> <tr> <td>${p.id}</td> <td> <c:if test="${!empty p.firstProductImage}"> <img width="40px" src="img/productSingle/${p.firstProductImage.id}.jpg"> </c:if> </td> <td>${p.name}</td> <td>${p.subTitle}</td> <td>${p.originalPrice}</td> <td>${p.promotePrice}</td> <td>${p.stock}</td> <td><a href="admin_productImage_list?pid=${p.id}"><span class="glyphicon glyphicon-picture"></span></a></td> <td><a href="admin_product_editPropertyValue?id=${p.id}"><span class="glyphicon glyphicon-th-list"></span></a></td> <td><a href="admin_product_edit?id=${p.id}"><span class="glyphicon glyphicon-edit"></span></a></td> <td><a deleteLink="true" href="admin_product_delete?id=${p.id}"><span class=" glyphicon glyphicon-trash"></span></a></td> </tr> </c:forEach> ``` # 步骤 14 : 增加功能讲解 1\. 在listProduct.jsp提交数据的时候,除了提交产品名称,小标题,原价格,优惠价格,库存外还会提交cid 2\. 在ProductServlet中根据获取到的cid,name,subTitle,等参数,创建新的Product对象,并插入到数据库 3\. 客户端跳转到admin\_product\_list,并带上参数cid ![](images/3802.png) listProduct.jsp ``` <form method="post" id="addForm" action="admin_product_add"> <table class="addTable"> <tr> <td>产品名称</td> <td><input id="name" name="name" type="text" class="form-control"></td> </tr> <tr> <td>产品小标题</td> <td><input id="subTitle" name="subTitle" type="text" class="form-control"></td> </tr> <tr> <td>原价格</td> <td><input id="originalPrice" value="99.98" name="originalPrice" type="text" class="form-control"></td> </tr> <tr> <td>优惠价格</td> <td><input id="promotePrice" value="19.98" name="promotePrice" type="text" class="form-control"></td> </tr> <tr> <td>库存</td> <td><input id="stock" value="99" name="stock" type="text" class="form-control"></td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <input type="hidden" name="cid" value="${c.id}"> <button type="submit" class="btn btn-success">提 交</button> </td> </tr> </table> </form> ``` ProductServlet ``` @Override public String add(HttpServletRequest request, HttpServletResponse response) { int cid = Integer.parseInt(request.getParameter("cid")); Category c = categoryDao.get(cid); String name = request.getParameter("name"); String subTitle = request.getParameter("subTitle"); float originalPrice = Float.parseFloat(request.getParameter("originalPrice")); float promotePrice = Float.parseFloat(request.getParameter("promotePrice")); int stock = Integer.parseInt(request.getParameter("stock")); Product p = new Product(); p.setCategory(c); p.setName(name); p.setSubTitle(subTitle); p.setOriginalPrice(originalPrice); p.setPromotePrice(promotePrice); p.setStock(stock); productDao.add(p); return "@admin_product_list?cid=" + cid; } ``` # 步骤 15 : 编辑功能讲解 1\. 在ProductServlet的edit方法中,根据id获取Product对象 2\. 把Product对象放在request的 "p" 属性中 3\. 服务端跳转到admin/editProduct.jsp 4\. 在editProduct.jsp中显示属性名称 5\. 在editProduct.jsp中隐式提供id和cid ProductServlet ``` @Override public String edit(HttpServletRequest request, HttpServletResponse response) { int id = Integer.parseInt(request.getParameter("id")); Product p = productDao.get(id); request.setAttribute("p", p); return "admin/editProduct.jsp"; } ``` editProduct ``` <form method="post" id="editForm" action="admin_product_update"> <table class="editTable"> <tr> <td>产品名称</td> <td><input id="name" name="name" value="${p.name}" type="text" class="form-control"></td> </tr> <tr> <td>产品小标题</td> <td><input id="subTitle" name="subTitle" type="text" value="${p.subTitle}" class="form-control"></td> </tr> <tr> <td>原价格</td> <td><input id="originalPrice" value="${p.originalPrice}" name="originalPrice" type="text" class="form-control"></td> </tr> <tr> <td>优惠价格</td> <td><input id="promotePrice" value="${p.promotePrice}" name="promotePrice" type="text" class="form-control"></td> </tr> <tr> <td>库存</td> <td><input id="stock" value="${p.stock}" name="stock" type="text" class="form-control"></td> </tr> <tr class="submitTR"> <td colspan="2" align="center"> <input type="hidden" name="id" value="${p.id}"> <input type="hidden" name="cid" value="${p.category.id}"> <button type="submit" class="btn btn-success">提 交</button></td> </tr> </table> </form> ``` # 步骤 16: 修改功能讲解 1\. 在ProductServlet的update方法中获取cid,id, name,subTitle,price等参数 2\. 根据这些参数创建Product对象 3\. 借助productDAO更新这个对象到数据库 4\. 客户端跳转到admin\_product\_list,并带上参数cid ![](images/3804.png) ``` public String update(HttpServletRequest request, HttpServletResponse response) { int cid = Integer.parseInt(request.getParameter("cid")); Category c = categoryDao.get(cid); int id = Integer.parseInt(request.getParameter("id")); int stock = Integer.parseInt(request.getParameter("stock")); float originalPrice = Float.parseFloat(request.getParameter("originalPrice")); float promotePrice = Float.parseFloat(request.getParameter("promotePrice")); String subTitle = request.getParameter("subTitle"); String name = request.getParameter("name"); Product p = new Product(); p.setName(name); p.setSubTitle(subTitle); p.setOriginalPrice(originalPrice); p.setPromotePrice(promotePrice); p.setStock(stock); p.setId(id); p.setCategory(c); productDao.update(p); return "@admin_product_list?cid=" + p.getCategory().getId(); } ``` # 步骤 17 : 删除功能讲解 1\. 在ProductServlet的delete方法中获取id 2\. 根据id获取Product对象 3\. 借助productDAO删除这个对象对应的数据 4\. 客户端跳转到admin\_product\_list,并带上参数cid ``` public String delete(HttpServletRequest request, HttpServletResponse response) { int id = Integer.parseInt(request.getParameter("id")); Product p = productDao.get(id); productDao.delete(id); return "@admin_product_list?cid=" + p.getCategory().getId(); } ```