案例22-显示商品的详细信息

Posted jepson6669

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了案例22-显示商品的详细信息相关的知识,希望对你有一定的参考价值。

1 product_list.jsp代码修改

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />

<style>
body {
    margin-top: 20px;
    margin: 0 auto;
    width: 100%;
}

.carousel-inner .item img {
    width: 100%;
    height: 300px;
}
</style>
</head>

<body>


    <!-- 引入header.jsp -->
    <jsp:include page="/header.jsp"></jsp:include>


    <div class="row" style="width: 1210px; margin: 0 auto;">
        <div class="col-md-12">
            <ol class="breadcrumb">
                <li><a href="#">首页</a></li>
            </ol>
        </div>
        <c:forEach items="${pageBean.list }" var="product">
            <div class="col-md-2" style="height: 250px">
                <a href="${pageContext.request.contextPath }/productInfo?pid=${product.pid}"> <img src="${pageContext.request.contextPath }/${product.pimage}"
                    width="170" height="170" style="display: inline-block;">
                </a>
                <p>
                    <a href="${pageContext.request.contextPath }/productInfo?pid=${product.pid}" style=‘color: green‘>${product.pname }</a>
                </p>
                <p>
                    <font color="#FF0000">商城价:&yen;${product.shop_price }</font>
                </p>
            </div>
        </c:forEach>
    </div>

    <!--分页 -->
    <div style="width: 380px; margin: 0 auto; margin-top: 50px;">
        <ul class="pagination" style="text-align: center; margin-top: 10px;">
            
                    
            
            <!-- 2 上一页 -->
                <!--判断当前页是否是第一页  -->
            <c:if test="${pageBean.currentPage==1 }">
                <li class="disabled">
                    <a href="javascript:void(0);" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
            </c:if>
            <c:if test="${pageBean.currentPage!=1 }">
                <li>
                    <a href="${pageContext.request.contextPath }/productListByCid?cid=${cid}&currentPage=${pageBean.currentPage-1 }" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>
            </c:if>
            
            
            
            
            <!-- 1 显示每一页 -->        
            <c:forEach begin="1" end="${pageBean.totalPage }" var="page">
                <!-- 判断是否是当前页 -->
                <c:if test="${page==pageBean.currentPage }">
                    <li class="active"><a href="javascript:void(0);">${page }</a></li>
                </c:if> 
                <c:if test="${page!=pageBean.currentPage }">
                    <li><a href="${pageContext.request.contextPath }/productListByCid?cid=${cid}&currentPage=${page }">${page }</a></li>
                </c:if>
            </c:forEach>
            
            
            
            <!-- 3 下一页 -->
                <!--判断当前页是否是第一页  -->
            <c:if test="${pageBean.currentPage==pageBean.totalPage }">
                <li class="disabled">
                    <a href="javascript:void(0);" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>
            <c:if test="${pageBean.currentPage!=pageBean.totalPage }">
                <li>
                    <a href="${pageContext.request.contextPath }/productListByCid?cid=${cid}&currentPage=${pageBean.currentPage+1 }" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>

            
        </ul>
    </div>
    <!-- 分页结束 -->

    <!--商品浏览记录-->
    <div
        style="width: 1210px; margin: 0 auto; padding: 0 9px; border: 1px solid #ddd; border-top: 2px solid #999; height: 246px;">

        <h4 style="width: 50%; float: left; font: 14px/30px 微软雅黑">浏览记录</h4>
        <div style="width: 50%; float: right; text-align: right;">
            <a href="">more</a>
        </div>
        <div style="clear: both;"></div>

        <div style="overflow: hidden;">

            <ul style="list-style: none;">
                <li
                    style="width: 150px; height: 216; float: left; margin: 0 8px 0 0; padding: 0 18px 15px; text-align: center;"><img
                    src="products/1/cs10001.jpg" width="130px" height="130px" /></li>
            </ul>

        </div>
    </div>


    <!-- 引入footer.jsp -->
    <jsp:include page="/footer.jsp"></jsp:include>

</body>

</html>

2 web层

package www.test.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

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.service.ProductService;

public class ProductInfoServlet extends HttpServlet {

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

        //获取pid
        String pid =request.getParameter("pid");
        
        //传递给service层并调取service层的方法
        ProductService service = new ProductService();
        Product product = null;
        try {
            product = service.findProductByPid(pid);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        
        
        //存储到request域中
        request.setAttribute("product", product);
          //转发
        request.getRequestDispatcher("/product_info.jsp").forward(request, response);
    }

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

}

3 service层

package www.test.service;

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

import www.test.dao.ProductDao;
import www.test.domain.Category;
import www.test.domain.Product;
import www.test.vo.PageBean;

public class ProductService {

    // 获取热门商品
    public List<Product> findHotProductList() throws SQLException {

        ProductDao dao = new ProductDao();
        return dao.findHotProductList();
    }

    // 获取最新商品商品
    public List<Product> findNewProductList() throws SQLException {

        ProductDao dao = new ProductDao();
        return dao.findNewProductList();
    }

    //获取商品分类
    public List<Category> findAllCategory() throws SQLException {
        ProductDao dao = new ProductDao();
        List<Category> categoryList = dao.findAllCategory();
        return categoryList;
    }

    //封装一个pageBean并返回给web层
    public PageBean<Product> findProductListByCid(String cid,int currentPage ,int currentCount) {
        
        ProductDao dao = new ProductDao();
        
        //封装一个pageBean并返回给web层
        PageBean<Product> pageBean = new PageBean<Product>();
        // 1 封装当前页
        pageBean.setCurrentPage(currentPage);
        
        //2 封装每页显示的条数
        pageBean.setCurrentCount(currentCount);
        
        //3 封装总条数
        int totalCount = 0;
        try {
            totalCount = dao.getTotalCount(cid);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        pageBean.setTotalCount(totalCount);
        
        //4 封装总页数
        int totalPage = (int) Math.ceil(1.0*totalCount/currentCount);
        pageBean.setTotalPage(totalPage);
        
        //5 当前页显示的数据
        //select * from product where cid =? limit ?,?;
        //当前页与起始页面的关系
        List<Product> list = null;
        int index = (currentPage-1)*currentCount;
        try {
            list = dao.findProductListByPage(cid,index,currentCount);
        } catch (SQLException e) {
            
            e.printStackTrace();
        }
        pageBean.setList(list);
        
        
        return pageBean;
    }

    
    //根据pid查询商品
    public Product findProductByPid(String pid) throws SQLException {
        ProductDao dao = new ProductDao();
        Product product = dao.findProductByPid(pid);
        return product;
    }

}

4 dao层

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 org.apache.commons.dbutils.handlers.ScalarHandler;

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

public class ProductDao {

    //获取热门商品
    public List<Product> findHotProductList() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from product where is_hot=? limit ?,?";
        List<Product> hotProductList = qr.query(sql, new BeanListHandler<Product>(Product.class), 1,0,9);
        return hotProductList;
    }
    
    //获取最新商品
    public List<Product> findNewProductList() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from product order by pdate desc limit ?,?";
        List<Product> newProductList = qr.query(sql, new BeanListHandler<Product>(Product.class), 0,9);
        return newProductList;
    }

    //获取商品分类
    public List<Category> findAllCategory() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from category";
        List<Category> categoryList = qr.query(sql, new BeanListHandler<Category>(Category.class));
        return categoryList;
    }

    //获取商品的总条数
    public int getTotalCount(String cid) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select count(*) from product where cid =?";
        Long totalCount = (Long) qr.query(sql, new ScalarHandler(),cid);
        return totalCount.intValue();
    }

    //获取分页显示的商品
    public List<Product> findProductListByPage(String cid, int index, int currentCount) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from product where cid=? limit ?,?";
        List<Product> list = qr.query(sql, new BeanListHandler<Product>(Product.class), cid,index,currentCount);
        return list;
    }

    //根据pid查询商品
    public Product findProductByPid(String pid) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
        String sql = "select * from product where pid=?";
        return qr.query(sql, new BeanHandler<Product>(Product.class), pid);
    }

}

5 product_info.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />

<style>
body {
    margin-top: 20px;
    margin: 0 auto;
}

.carousel-inner .item img {
    width: 100%;
    height: 300px;
}
</style>
</head>

<body>
    <!-- 引入header.jsp -->
    <jsp:include page="/header.jsp"></jsp:include>

    <div class="container">
        <div class="row">
            <div
                style="border: 1px solid #e4e4e4; width: 930px; margin-bottom: 10px; margin: 0 auto; padding: 10px; margin-bottom: 10px;">
                <a href="./index.htm">首页&nbsp;&nbsp;&gt;</a> <a href="./蔬菜分类.htm">蔬菜&nbsp;&nbsp;&gt;</a>
                <a>无公害蔬菜</a>
            </div>

            <div style="margin: 0 auto; width: 950px;">
                <div class="col-md-6">
                    <img style="opacity: 1; width: 400px; height: 350px;" title=""
                        class="medium"
                        src="${pageContext.request.contextPath }/${product.pimage}">
                </div>

                <div class="col-md-6">
                    <div>
                        <strong>${product.pname }</strong>
                    </div>
                    <div
                        style="border-bottom: 1px dotted #dddddd; width: 350px; margin: 10px 0 10px 0;">
                        <div>编号:${product.pid }</div>
                    </div>

                    <div style="margin: 10px 0 10px 0;">
                        亿家价: <strong style="color: #ef0101;">¥:${product.shop_price }元/份</strong> 参 考 价:
                        <del>${product.market_price }元/份</del>
                    </div>

                    <div style="margin: 10px 0 10px 0;">
                        促销: <a target="_blank" title="限时抢购 (2014-07-30 ~ 2015-01-01)"
                            style="background-color: #f07373;">限时抢购</a>
                    </div>

                    <div
                        style="padding: 10px; border: 1px solid #e7dbb1; width: 330px; margin: 15px 0 10px 0;; background-color: #fffee6;">
                        <div style="margin: 5px 0 10px 0;">白色</div>

                        <div
                            style="border-bottom: 1px solid #faeac7; margin-top: 20px; padding-left: 10px;">
                            购买数量: <input id="quantity" name="quantity" value="1"
                                maxlength="4" size="10" type="text">
                        </div>

                        <div style="margin: 20px 0 10px 0;; text-align: center;">
                            <a href="cart.htm"> <input
                                style="background: url(‘./images/product.gif‘) no-repeat scroll 0 -600px rgba(0, 0, 0, 0); height: 36px; width: 127px;"
                                value="加入购物车" type="button">
                            </a> &nbsp;收藏商品
                        </div>
                    </div>
                </div>
            </div>
            <div class="clear"></div>
            <div style="width: 950px; margin: 0 auto;">
                <div
                    style="background-color: #d3d3d3; width: 930px; padding: 10px 10px; margin: 10px 0 10px 0;">
                    <strong>商品介绍</strong>
                </div>

                <div>
                    <img
                        src="${pageContext.request.contextPath }/${product.pimage}">
                </div>

                <div
                    style="background-color: #d3d3d3; width: 930px; padding: 10px 10px; margin: 10px 0 10px 0;">
                    <strong>商品参数</strong>
                </div>
                <div style="margin-top: 10px; width: 900px;">
                    <table class="table table-bordered">
                        <tbody>
                            <tr class="active">
                                <th colspan="2">基本参数</th>
                            </tr>
                            <tr>
                                <th width="10%">级别</th>
                                <td width="30%">标准</td>
                            </tr>
                            <tr>
                                <th width="10%">标重</th>
                                <td>500</td>
                            </tr>
                            <tr>
                                <th width="10%">浮动</th>
                                <td>200</td>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div style="background-color: #d3d3d3; width: 900px;">
                    <table class="table table-bordered">
                        <tbody>
                            <tr class="active">
                                <th><strong>商品评论</strong></th>
                            </tr>
                            <tr class="warning">
                                <th>暂无商品评论信息 <a>[发表商品评论]</a></th>
                            </tr>
                        </tbody>
                    </table>
                </div>

                <div style="background-color: #d3d3d3; width: 900px;">
                    <table class="table table-bordered">
                        <tbody>
                            <tr class="active">
                                <th><strong>商品咨询</strong></th>
                            </tr>
                            <tr class="warning">
                                <th>暂无商品咨询信息 <a>[发表商品咨询]</a></th>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

        </div>
    </div>


    <!-- 引入footer.jsp -->
    <jsp:include page="/footer.jsp"></jsp:include>

</body>

</html>

 

以上是关于案例22-显示商品的详细信息的主要内容,如果未能解决你的问题,请参考以下文章

突出显示可展开列表中的选定项目

Android:GoogleMaps 标记详细信息片段

vbscript 各种自定义代码片段 - 有关详细信息,请参阅注释

新星计划Django基于PythonWeb的Django框架设计实现天天生鲜系统-8商品详细页面与商品分类功能实现

案例36-商品添加页面类别ajax显示

Android片段重叠