JavaWeb基础系列(八)商城后台增删改查

本文详细介绍了如何在JavaWeb环境下实现商城后台的商品管理功能,包括商品列表展示、添加商品(商品分类选择)、删除商品以及商品信息的修改。各个步骤涵盖了从Web层到Dao层的完整流程,并提供了Jsp页面的关键代码示例和运行结果截图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、商品列表的展示

Web层:

@WebServlet(name = "adminProductList",
        urlPatterns = {
  
  "/adminProductList"})
public class adminProductList extends HttpServlet {
   
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //传递请求到service层
        AdminProductService service = new AdminProductService();
        List<Product> productList = null;
        try {
            productList = service.findAllProduct();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //将productList放到request域
        request.setAttribute("productList", productList);
        request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
    }
}

Service层:

public class AdminProductService {
    public List<Product> findAllProduct() throws SQLException {
        //因为没有复杂业务,直接传递请求到dao层
        AdminProductDao dao = new AdminProductDao();
        return dao.findAllProduct();
    }
}

Dao层:

public class AdminProductDao {
    public List<Product> findAllProduct() throws SQLException {
        QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from product";
        List<Product> productList = runner.query(sql, new BeanListHandler<Product>(Product.class));
        return productList;
    }
}

Jsp:

<c:forEach items="${productList }" var="pro" varStatus="vs">
    <tr onmouseover="this.style.backgroundColor = 'white'"
          onmouseout="this.style.backgroundColor = '#F5FAFE';">
          <td style="CURSOR: hand; HEIGHT: 22px" align="center"
              width="18%">${vs.count }</td>
          <td style="CURSOR: hand; HEIGHT: 22px" align="center"
              width="17%">
              <img width="40" height="45" src="${pageContext.request.contextPath }/${pro.pimage }">
          </td>
          <td style="CURSOR: hand; HEIGHT: 22px" align="center"
              width="17%">${pro.pname }</td>
          <td style="CURSOR: hand; HEIGHT: 22px" align="center"
              width="17%">${pro.shop_price }</td>
          <td style="CURSOR: hand; HEIGHT: 22px" align="center"
              width="17%">${pro.is_hot==1?"是":"否" }</td>
          <td align="center" style="HEIGHT: 22px">
          <a href="${ pageContext.request.contextPath }/adminUpdateProductUI?pid=${pro.pid}">
          <img src="${pageContext.request.contextPath}/images/i_edit.gif"
                border="0" style="CURSOR: hand">
          </a></td>

          <td align="center" style="HEIGHT: 22px">
              <a href="javascript:void(0);" onclick="delProduct('${pro.pid}')">
                  <img src="${pageContext.request.contextPath}/images/i_del.gif"
                       width="16" height="16" border="0" style="CURSOR: hand">
              </a>
          </td>
      </tr>
  </c:forEach>

运行结果:
这里写图片描述

二、添加商品功能

2.1、商品分类添加进来

Jsp页面,当点击了添加按钮之后就讲商品页面添加进来:

<script type="text/javascript">
    function addProduct(){
    
    
        window.location.href = "${pageContext.request.contextPath}/adminAddProductUI";
    }
</script>

Web层:

@WebServlet(name = "adminAddProductUI",
        urlPatterns = {
  
  "/adminAddProductUI"})
public class adminAddProductUI extends HttpServlet {
   
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得所有的商品的类别数据
        AdminProductService service = new AdminProductService();
        List<Category> categoryList = null;
        try {
            categoryList = service.findAllCategory();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("categoryList", categoryList);
        request.getRequestDispatcher("/admin/product/add.jsp").forward(request, response);
    }
}

Service层:

public class AdminProductService {
    public List<Category> findAllCategory() throws SQLException {
        AdminProductDao dao = new AdminProductDao();
        return dao.findAllCategory();
    }
}

Dao层:

publ
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北极星小王子

你的鼓励是我创作的最大动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值