商品的修改

一、商品的修改:

在商品页面点击修改(这边要把pid传出来)
<td><a href="/Web13_1/EditProductServlet?pid=${ p.pid }">修改</a>|<a href="">删除</a></td>

1、通过PID查到该商品:

ProductDao:
/**
     * 利用Id寻找对应的product
     * @param id
     * @return
     * @throws SQLException 
     */
    public Product findById(String id) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
        String sql = "select * from product where pid = ?";
        Product product = queryRunner.query(sql, new BeanHandler<Product>(Product.class), id);
        return product;
    }
ProductService:
/**
     * 利用ID寻找对应的product
     * @return
     * @throws SQLException 
     */
    public Product findById(String id) throws SQLException {
        ProductDao productDao = new ProductDao();
        Product product = productDao.findById(id);
        return product;
    }
EditProductServlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            //接收所要修改商品的id,
            String id = request.getParameter("pid");
            //调用业务层处理数据
            ProductService productService = new ProductService();
            Product product = productService.findById(id);
            //记得把product存到当前请求域中!!!
            request.setAttribute("product", product);
            //页面跳转
            request.getRequestDispatcher("/product/update.jsp").forward(request, response);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

2、查到该商品后跳到update.jsp进行修改,再提交到UpdateProductServle把修改保存到数据库

update.jsp
<body>
        <h1>商品修改</h1>
        <form action="${ pageContext.request.contextPath}/UpdateProductServlet" method="post">
        <!-- pid的值,修改的时候根据pid查找商品(update) -->

        <input type="hidden" name="pid" value="${ product.pid }"/>
            <table border="1px" width=500>
            <tr>
                <td>商品名称</td>
                <td><input type="text" name="pname" value="${ product.pname }"></td>
            </tr>
            <tr>
                <td>市场价格</td>
                <td><input type="text" name="market_price" value="${ product.market_price }"></td>
            </tr>
            <tr>
                <td>商城价格</td>
                <td><input type="text" name="shop_price" value="${ product.shop_price }"></td>
            </tr>
            <tr>
                <td>是否热门</td>
                <td>
                <input type="radio" name="is_hot" value="1" <c:if test="${ product.is_hot == 1 }">checked</c:if> >是
                <input type="radio" name="is_hot" value="0" <c:if test="${ product.is_hot == 0 }">checked</c:if>>否
                </td>
            </tr>
            <tr>
                <td>是否下架</td>
                <td>
                    <select name="pflag">
                        <option value="0" <c:if test="${ product.pflag == 0 }">selected</c:if> >否</option>
                        <option value="1" <c:if test="${ product.pflag ==1 }">selected</c:if> >是</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>商品描述</td>
                <td><textarea rows="10" cols="10" name="pdesc" >${ product.pdesc }</textarea></td>
            </tr>
            <tr>
                <td>商品分类</td>
                <td>
                    <select name="cid" >
                        <option value="1" <c:if test="${ product.cid == 1 }">selected</c:if> >手机数码</option>
                        <option value="2" <c:if test="${ product.cid == 2 }">selected</c:if> >电脑办公</option>
                        <option value="3" <c:if test="${ product.cid == 3 }">selected</c:if> >汽车用品</option>
                        <option value="4" <c:if test="${ product.cid == 4 }">selected</c:if> >鞋靴箱包</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="修改"></td>
            </tr>
        </table>
        </form>
  </body>
UpdateProductServlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
            //接收数据
            Map<String,String[]> map = request.getParameterMap();
            Product product = new Product();
            BeanUtils.populate(product, map);
            product.setPdate(new Date());
            //调用业务层处理数据
            ProductService productService = new ProductService();
            productService.update(product);

            request.getRequestDispatcher("/ProductFindAllServlet").forward(request, response);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
UpdateProductServlet:
/**
     * 修改数据
     * @param product
     * @throws SQLException 
     */
    public void update(Product product) throws SQLException {
        ProductDao productDao = new ProductDao();
        productDao.update(product);
    }
ProductDao:
/**
     * 修改数据
     * @param product
     * @throws SQLException 
     */
    public void update(Product product) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3P0JDBCUtils.getDataSource());
        String sql = "update product set pname =?,market_price=?,shop_price=?,pimage=?,pdate=?,is_hot=?,pdesc=?,pflag=?,cid=? where pid=? ";
        Object[] params = { product.getPname(), product.getMarket_price(), product.getShop_price(),
                product.getPimage(), product.getPdate(), product.getIs_hot(), product.getPdesc(), product.getPflag(),
                product.getCid(),product.getPid()};
        queryRunner.update(sql, params);
    }

猜你喜欢

转载自blog.csdn.net/sinat_40662281/article/details/80446077