使用三层架构完成商品列表的显示

1 准备工作

1 数据库的准备

查看:使用jsp完成商品列表的动态显示

2 其它准备工作

1)搭建好三层架构

2)导入需要 jar

3)导入需要的工具类
4)导入 c3p0 连接池工具类

5)准备 javaBean Product

6)所需要的页面

2 代码实现

1 web层ProductListServlet代码实现

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.Product;
import www.test.services.ProductListService;

public class ProductListServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //传入数据到service层
        ProductListService service = new ProductListService();
        List<Product> productList =null;
        try {
            productList = service.findAllProduct();
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        for (Product product : productList) {
            System.out.println(product);
        }
        //全部商品的数据准备好了 转发给 jsp 进行数据的展示
        request.setAttribute("productlist", productList);
        //转发到product_list.jsp页面
        request.getRequestDispatcher("/product_list.jsp").forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

2 service层的ProductListService代码

package www.test.services;

import java.sql.SQLException;
import java.util.List;

import www.test.dao.ProductDao;
import www.test.domain.Product;

public class ProductListService {

    //没有复杂业务
    //传递请求到 dao 层
    public List<Product> findAllProduct() throws SQLException {
        ProductDao dao = new ProductDao();
        List<Product> productList = dao.finAllProduct();
        return productList;
    }

}

3 dao层ProductDao代码

package www.test.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import www.test.domain.Product;
import www.test.utils.C3P0Utils;


public class ProductDao {

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

}

4 product_list.jsp部分代码

<c:forEach items="${productlist }" var="product">
    <div class="col-md-2" style="height: 250px">
    <a href="product_info.htm" > <img src="${pageContext.request.contextPath }/${product.pimage}"
        width="170" height="170" style="display: inline-block;">
    </a>
    <p>
        <a href="product_info.html" style='color: green'>${product.pname }</a>
    </p>
    <p>
        <font color="#FF0000">商城价:&yen;${product.shop_price }</font>
    </p>
</div>
    
</c:forEach>

 

posted @ 2018-01-23 16:38  Jepson6669  阅读(349)  评论(0编辑  收藏  举报