博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
案例4-修改商品
阅读量:4882 次
发布时间:2019-06-11

本文共 17308 字,大约阅读时间需要 57 分钟。

1 完成商品信息的回显

1 修改list.jsp

<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
商品列表
序号 商品图片 商品名称 商品价格 是否热门 编辑 删除
${vs.count } ${product.pname } ${product.shop_price } ${product.is_hot==1?"是":"否" }

2 web层AdminEditProductServlet

package www.test.web;import java.io.IOException;import java.sql.SQLException;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import www.test.domain.Category;import www.test.domain.Product;import www.test.service.AdminProductService;public class AdminEditProductServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // 获取数据        String pid = request.getParameter("pid");//        System.out.println(pid);        // 传递数据到service层        AdminProductService service = new AdminProductService();        Product product = null;        try {            product = service.editProduct(pid);        } catch (SQLException e) {                        e.printStackTrace();        }        //        System.out.println(product);        // 将数据传递给request域        request.setAttribute("product", product);                        //获得所有的商品的类别数据         List
categoryList = null; try { categoryList = service.findAllCategory(); } catch (SQLException e) { e.printStackTrace(); }// System.out.println(categoryList); // 将获取到的categoryList存储到request域中 request.setAttribute("categoryList", categoryList); request.getRequestDispatcher("/admin/product/edit.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

3 service层AdminProductService

package www.test.service;import java.sql.SQLException;import java.util.List;import www.test.dao.AdminProductDao;import www.test.domain.Category;import www.test.domain.Product;public class AdminProductService {    public List
findAllProduct() throws SQLException { //因为没有复杂业务 直接传递请求到dao层 AdminProductDao dao = new AdminProductDao(); return dao.findAllProduct(); } public List
findAllCategory() throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.finAllCategory(); } // 添加商品 public boolean addProduct(Product product) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.addProduct(product); } //删除数据 public boolean delProduct(String pid) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.delProduct(pid); } // 修改数据 public Product editProduct(String pid) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.editProduct(pid); } //保存修改后的数据 public boolean editProductSave(Product product) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.editProductSave(product); }}

4 dao层AdminProductDao

package www.test.dao;import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import www.test.domain.Category;import www.test.domain.Product;import www.test.utils.C3P0Utils;public class AdminProductDao {    public List
findAllProduct() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product"; List
productList = qr.query(sql, new BeanListHandler
(Product.class)); return productList; } public List
finAllCategory() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from category"; List
categoryList = qr.query(sql, new BeanListHandler
(Category.class)); return categoryList; } //添加数据 public boolean addProduct(Product product) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)"; Object[] params = {product.getPid(),product.getPname(),product.getMarket_price(),product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),product.getPdesc(),product.getPflag(),product.getCid()}; int num = qr.update(sql, params); if(num>0){ return true; }else{ return false; } } // 删除数据 public boolean delProduct(String pid) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "delete from product where pid=?"; int num = qr.update(sql, pid); if(num>0){ return true; }else{ return false; } } //修改数据 public Product editProduct(String pid) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product where pid=?"; Product product = qr.query(sql, new BeanHandler
(Product.class),pid); return product; } public boolean editProductSave(Product product) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "update product set pname=?,market_price=?,shop_price=?,pimage=?,pdate=?,is_hot=?,pdesc=?,pflag=?,cid=? where pid=?"; int num = qr.update(sql, product.getPname(),product.getMarket_price(), product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(), product.getPdesc(),product.getPflag(),product.getCid(),product.getPid()); System.out.println(num); if(num>0){ return true; }else{ return false; } }}

5 修改edit.jsp代码

<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>            
编辑商品
商品名称: 是否热门:
市场价格: 商城价格:
商品图片:
所属分类:
商品描述:
               

2 完成修改后的保存操作

1 修改edit.jsp代码

<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>            
编辑商品
商品名称: 是否热门:
市场价格: 商城价格:
商品图片:
所属分类:
商品描述:
               

2 web层AdminEditProductSaveServlet

package www.test.web;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Map;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import www.test.domain.Product;import www.test.service.AdminProductService;public class AdminEditProductSaveServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {         //解决乱码问题        request.setCharacterEncoding("UTF-8");        //获取数据        Map
parameterMap = request.getParameterMap(); //使用BeanUtils进行子映射封装 Product product = new Product(); try { BeanUtils.populate(product, parameterMap); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } //此位置Product已经封装完毕----将表单的数据封装完毕 //手动设置表单中没有数据 //2)private String pimage; product.setPimage("products/1/c_0033.jpg"); //3)private String pdate;//上架日期 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String pdate = format.format(new Date()); product.setPdate(pdate); //4)private int pflag;//商品是否下载 0代表未下架 product.setPflag(0); System.out.println(product); // 3 传递数据给service层 AdminProductService service = new AdminProductService(); boolean isSuccess = true; try { isSuccess = service.editProductSave(product); } catch (SQLException e) { e.printStackTrace(); } System.out.println(isSuccess); //将处理结果存储到request域中 request.setAttribute("isSuccess", isSuccess); //跳转到列表页面 //response.sendRedirect(request.getContextPath()+"/adminProductList"); request.getRequestDispatcher("/adminProductList").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}

3 service层AdminProductService

package www.test.service;import java.sql.SQLException;import java.util.List;import www.test.dao.AdminProductDao;import www.test.domain.Category;import www.test.domain.Product;public class AdminProductService {    public List
findAllProduct() throws SQLException { //因为没有复杂业务 直接传递请求到dao层 AdminProductDao dao = new AdminProductDao(); return dao.findAllProduct(); } public List
findAllCategory() throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.finAllCategory(); } // 添加商品 public boolean addProduct(Product product) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.addProduct(product); } //删除数据 public boolean delProduct(String pid) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.delProduct(pid); } // 修改数据 public Product editProduct(String pid) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.editProduct(pid); } //保存修改后的数据 public boolean editProductSave(Product product) throws SQLException { AdminProductDao dao = new AdminProductDao(); return dao.editProductSave(product); }}

4 dao层AdminProductDao

package www.test.dao;import java.sql.SQLException;import java.util.List;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import www.test.domain.Category;import www.test.domain.Product;import www.test.utils.C3P0Utils;public class AdminProductDao {    public List
findAllProduct() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product"; List
productList = qr.query(sql, new BeanListHandler
(Product.class)); return productList; } public List
finAllCategory() throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from category"; List
categoryList = qr.query(sql, new BeanListHandler
(Category.class)); return categoryList; } //添加数据 public boolean addProduct(Product product) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)"; Object[] params = {product.getPid(),product.getPname(),product.getMarket_price(),product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),product.getPdesc(),product.getPflag(),product.getCid()}; int num = qr.update(sql, params); if(num>0){ return true; }else{ return false; } } // 删除数据 public boolean delProduct(String pid) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "delete from product where pid=?"; int num = qr.update(sql, pid); if(num>0){ return true; }else{ return false; } } //修改数据 public Product editProduct(String pid) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product where pid=?"; Product product = qr.query(sql, new BeanHandler
(Product.class),pid); return product; } //保存修改后的数据 public boolean editProductSave(Product product) throws SQLException { QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()); String sql = "update product set pname=?,market_price=?,shop_price=?,pimage=?,pdate=?,is_hot=?,pdesc=?,pflag=?,cid=? where pid=?"; int num = qr.update(sql, product.getPname(),product.getMarket_price(), product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(), product.getPdesc(),product.getPflag(),product.getCid(),product.getPid()); System.out.println(num); if(num>0){ return true; }else{ return false; } }}

3 总结

<!--enctype="multipart/form-data" 修改表单提交给servlet处理。删除form里面的enctype属性,这个属性是文件上传的意思。  --> 不删除表单中的enctype获取不到数据。

<!-- 需要提交的数据,又还不想要让用户看到就使用type="hidden" -->

    <input type="hidden" name="pid" value="${product.pid }">

 

转载于:https://www.cnblogs.com/jepson6669/p/8344096.html

你可能感兴趣的文章
关于VS项目平台的x86,x64,Any CPU以及Debug和Release的区别
查看>>
解密module_init幕后的故事
查看>>
9个移动网站优化的最佳实践
查看>>
李昌镐:苍老的青春(转载) 韩国围棋职业棋手
查看>>
JPA 使用报Named query not found错误
查看>>
FTP命令使用详解
查看>>
walmart weekly sales
查看>>
面试题07_用两个栈实现队列——剑指offer系列
查看>>
cocos2d-x3.2中加入Android手机震动
查看>>
css3处理sprite背景图压缩来解决H5网页在手机浏览器下图标模糊的问题
查看>>
温故而知新练习3
查看>>
【转】iOS应用崩溃日志分析
查看>>
EtherCAT Slave 入门教程 - 邮箱服务(1)
查看>>
java基础------抽象类
查看>>
【poj3537】 Crosses ans Crosses
查看>>
【poj1013】 Counterfeit Dollar
查看>>
Centos7 安装配置Apache+Mysql5.7+PHP7.0+phpmyadmin
查看>>
最佳调度问题
查看>>
10.04 FZSZ模拟Day1 总结
查看>>
RabbitMQ学习以及与Spring的集成(二)
查看>>