Shop项目--5. 显示商品详细信息,product_info.jsp
2017-11-24 22:40
441 查看
分析:
显示商品的详细信息,在product_info.jsp显示,通过pid查找数据库得到product,
该功能实现非常简单,主要为下一个功能,历史记录作为铺垫。
在product_info.jsp添加返回上一级菜单链接,携带cid 与currentPage
准备:
步骤:
1.从product_list.jsp进入
<!-- 传递currentPage是因为准备返回上一级使用的 -->
<a href="${pageContext.request.contextPath }/product?method=productInfo&pid=${product.pid}¤tPage=${pageBean.currentPage}">
2.在web层获取pid currentPage, 传递pid到service层在dao层根据pid获取商品product。
3.web层把product 和 currentPage 传到request域并转发到product_info.jsp页面
4.在product_info.jsp页面获取所需要的信息,并添加返回上一级链接,该链接即productListByCid功能,需要携带cid与currentPage
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>
<!-- 根据cid分类的商品列表 -->
<c:forEach items="${pageBean.list }" var="product">
<div class="col-md-2" style="height: 250px">
<!-- 商品详细信息入口 -->
<!-- 传递currentPage是因为准备返回上一级使用的 -->
<a href="${pageContext.request.contextPath }/product?method=productInfo&pid=${product.pid}¤tPage=${pageBean.currentPage}"> <img src="${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">商城价:¥${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;">
<!-- 上一页 -->
<!-- 判断当前页是否第一页 -->
<c:if test="${pageBean.currentPage==1}">
<li class="disabled">
<!-- 不能点击的设置 javascript:void(0);-->
<a href="javascript:void(0);" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=1}">
<li >
<a href="${pageContext.request.contextPath }/product?method=productListByCid&cid=${cid}¤tPage=${pageBean.currentPage-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<!-- 分页显示代码 -->
<!-- 1.遍历,显示所有分页 -->
<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
<!--2.判断是否当前页 -->
<c:if test="${pageBean.currentPage==page }">
<li class="active"><a href="#">${page }</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=page }">
<!--3. 跳转到另外一页,需要携带cid与page -->
<li><a href="${pageContext.request.contextPath}/product?method=productListByCid&cid=${cid}¤tPage=${page}">${page }</a></li>
</c:if>
</c:forEach>
<!-- 下一页 -->
<!-- 判断是否最后一页 -->
<c:if test="${pageBean.currentPage==pageBean.totalPage }">
<li class="disabled">
<a href="javascript:" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
<li>
<a href="${pageContext.request.contextPath }/product?method=productListByCid&cid=${cid}¤tPage=${pageBean.currentPage+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<!-- <li class="disabled"><a href="#" aria-label="Previous"><span
aria-hidden="true">«</span></a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#">8</a></li>
<li><a href="#">9</a></li>
<li><a href="#" aria-label="Next"> <span aria-hidden="true">»</span>
</a></li> -->
</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>
ProductServlet
package com.itheima.web.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.itheima.domain.Category;
import com.itheima.domain.PageBean;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import com.itheima.utils.JedisPoolUtils;
import redis.clients.jedis.Jedis;
public class ProductServlet extends BaseServlet {
//商品详细信息页面
public void productInfo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//获取pid
String pid = request.getParameter("pid");
//获取当前页
String currentPage = request.getParameter("currentPage");
//传递到service层获取product
ProductService service = new ProductService();
Product product = service.findProductByPid(pid);
//传到request域并转发
request.setAttribute("product", product);
//传递当前页是准备返回上一级使用的
request.setAttribute("currentPage", currentPage);
request.getRequestDispatcher("/product_info.jsp").forward(request, response);
}
//根据商品分类的cid获取商品,分页显示功能
public void productListByCid(HttpServletRe
4000
quest request,HttpServletResponse response) throws ServletException, IOException {
//获取商品分类的cid
String cid = request.getParameter("cid");
//获取当前页,如果没有当前页,设置为第一页
String currentPageStr = request.getParameter("currentPage");
if(currentPageStr==null) {
currentPageStr ="1";
}
//当前页,与当前显示数
int currentPage = Integer.parseInt(currentPageStr);
int currentCount =12;
//传递到service层,pageBean
ProductService service = new ProductService();
PageBean<Product> pageBean = service.findPageBeanByCid(cid,currentPage,currentCount);
//传递pageBean 与 cid 等待分页下一次执行该功能时候使用
request.setAttribute("cid", cid);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
//获取商品的分类列表
public void category(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
ProductService service = new ProductService();
//先从缓存中查询是否有categoryList,没有再从数据库中查询
//1.获得jedis对象连接redis数据库
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
//2.判断categoryList是否为空
if(categoryListJson==null) {
System.out.println("缓存没有数据,查找数据库");
//准备分类数据
List<Category> categoryList = service.findCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
}
//有中文,所以要解决乱码问题
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);
}
//首页动态获取最新商品和热门商品
public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
ProductService service = new ProductService();
//获取热门商品
List<Product> hotProList = service.findHotProduct();
//获取最新商品
List<Product> newProList = service.findNewProduct();
//传递到request域,并转发到index.jsp
request.setAttribute("hotProList", hotProList);
request.setAttribute("newProList", newProList);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
ProductService
ProductDao
package com.itheima.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 com.itheima.domain.Category;
import com.itheima.domain.Product;
import com.itheima.utils.DataSourceUtils;
public class ProductDao {
// 获取热门商品
public List<Product> findHotProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where is_hot=? limit ?,?";
List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), 1,0,9);
return query;
}
//获取最新商品
public List<Product> findNewProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
//此处注意使用order by pdate desc 时间最大开始选择
String sql ="select * from product order by pdate desc limit ?,?";
List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), 0,9);
return query;
}
//获取商品分类列表
public List<Category> findCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="select * from category";
List<Category> query = runner.query(sql, new BeanListHandler<Category>(Category.class));
return query;
}
// 根据商品分类的cid获取商品
public List<Product> findProductListByCid(String cid, int index, int currentPage) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where cid=? limit ?,?";
List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), cid,index,currentPage);
return query;
}
//根据cid查找商品总条数
public int findtotalCount(String cid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="select count(*) from product where cid=?";
Long query = (Long) runner.query(sql, new ScalarHandler(), cid);
//Long转为int
return query.intValue();
}
//根据pid查找商品
public Product findProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pid=?";
Product query = runner.query(sql, new BeanHandler<Product>(Product.class), pid);
return query;
}
}
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">首页 ></a> <a href="./蔬菜分类.htm">蔬菜 ></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="${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>编号:751</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> 收藏商品
</div>
<!-- 返回上一级 后台把当前页已经存过来-->
<a href="${pageContext.request.contextPath }/product?method=productListByCid&cid=${product.cid}¤tPage=${currentPage}">返回上一页</a>
</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="${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>
显示商品的详细信息,在product_info.jsp显示,通过pid查找数据库得到product,
该功能实现非常简单,主要为下一个功能,历史记录作为铺垫。
在product_info.jsp添加返回上一级菜单链接,携带cid 与currentPage
准备:
步骤:
1.从product_list.jsp进入
<!-- 传递currentPage是因为准备返回上一级使用的 -->
<a href="${pageContext.request.contextPath }/product?method=productInfo&pid=${product.pid}¤tPage=${pageBean.currentPage}">
2.在web层获取pid currentPage, 传递pid到service层在dao层根据pid获取商品product。
3.web层把product 和 currentPage 传到request域并转发到product_info.jsp页面
4.在product_info.jsp页面获取所需要的信息,并添加返回上一级链接,该链接即productListByCid功能,需要携带cid与currentPage
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>
<!-- 根据cid分类的商品列表 -->
<c:forEach items="${pageBean.list }" var="product">
<div class="col-md-2" style="height: 250px">
<!-- 商品详细信息入口 -->
<!-- 传递currentPage是因为准备返回上一级使用的 -->
<a href="${pageContext.request.contextPath }/product?method=productInfo&pid=${product.pid}¤tPage=${pageBean.currentPage}"> <img src="${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">商城价:¥${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;">
<!-- 上一页 -->
<!-- 判断当前页是否第一页 -->
<c:if test="${pageBean.currentPage==1}">
<li class="disabled">
<!-- 不能点击的设置 javascript:void(0);-->
<a href="javascript:void(0);" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=1}">
<li >
<a href="${pageContext.request.contextPath }/product?method=productListByCid&cid=${cid}¤tPage=${pageBean.currentPage-1}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
</c:if>
<!-- 分页显示代码 -->
<!-- 1.遍历,显示所有分页 -->
<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
<!--2.判断是否当前页 -->
<c:if test="${pageBean.currentPage==page }">
<li class="active"><a href="#">${page }</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=page }">
<!--3. 跳转到另外一页,需要携带cid与page -->
<li><a href="${pageContext.request.contextPath}/product?method=productListByCid&cid=${cid}¤tPage=${page}">${page }</a></li>
</c:if>
</c:forEach>
<!-- 下一页 -->
<!-- 判断是否最后一页 -->
<c:if test="${pageBean.currentPage==pageBean.totalPage }">
<li class="disabled">
<a href="javascript:" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
<li>
<a href="${pageContext.request.contextPath }/product?method=productListByCid&cid=${cid}¤tPage=${pageBean.currentPage+1}" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</c:if>
<!-- <li class="disabled"><a href="#" aria-label="Previous"><span
aria-hidden="true">«</span></a></li>
<li class="active"><a href="#">1</a></li>
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">5</a></li>
<li><a href="#">6</a></li>
<li><a href="#">7</a></li>
<li><a href="#">8</a></li>
<li><a href="#">9</a></li>
<li><a href="#" aria-label="Next"> <span aria-hidden="true">»</span>
</a></li> -->
</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>
ProductServlet
package com.itheima.web.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.itheima.domain.Category;
import com.itheima.domain.PageBean;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import com.itheima.utils.JedisPoolUtils;
import redis.clients.jedis.Jedis;
public class ProductServlet extends BaseServlet {
//商品详细信息页面
public void productInfo(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//获取pid
String pid = request.getParameter("pid");
//获取当前页
String currentPage = request.getParameter("currentPage");
//传递到service层获取product
ProductService service = new ProductService();
Product product = service.findProductByPid(pid);
//传到request域并转发
request.setAttribute("product", product);
//传递当前页是准备返回上一级使用的
request.setAttribute("currentPage", currentPage);
request.getRequestDispatcher("/product_info.jsp").forward(request, response);
}
//根据商品分类的cid获取商品,分页显示功能
public void productListByCid(HttpServletRe
4000
quest request,HttpServletResponse response) throws ServletException, IOException {
//获取商品分类的cid
String cid = request.getParameter("cid");
//获取当前页,如果没有当前页,设置为第一页
String currentPageStr = request.getParameter("currentPage");
if(currentPageStr==null) {
currentPageStr ="1";
}
//当前页,与当前显示数
int currentPage = Integer.parseInt(currentPageStr);
int currentCount =12;
//传递到service层,pageBean
ProductService service = new ProductService();
PageBean<Product> pageBean = service.findPageBeanByCid(cid,currentPage,currentCount);
//传递pageBean 与 cid 等待分页下一次执行该功能时候使用
request.setAttribute("cid", cid);
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
//获取商品的分类列表
public void category(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
ProductService service = new ProductService();
//先从缓存中查询是否有categoryList,没有再从数据库中查询
//1.获得jedis对象连接redis数据库
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
//2.判断categoryList是否为空
if(categoryListJson==null) {
System.out.println("缓存没有数据,查找数据库");
//准备分类数据
List<Category> categoryList = service.findCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson", categoryListJson);
}
//有中文,所以要解决乱码问题
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(categoryListJson);
}
//首页动态获取最新商品和热门商品
public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
ProductService service = new ProductService();
//获取热门商品
List<Product> hotProList = service.findHotProduct();
//获取最新商品
List<Product> newProList = service.findNewProduct();
//传递到request域,并转发到index.jsp
request.setAttribute("hotProList", hotProList);
request.setAttribute("newProList", newProList);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
}
ProductService
package com.itheima.service; import java.sql.SQLException; import java.util.List; import com.itheima.dao.ProductDao; import com.itheima.domain.Category; import com.itheima.domain.PageBean; import com.itheima.domain.Product; public class ProductService { //获取热门商品 public List<Product> findHotProduct() { ProductDao dao = new ProductDao(); List<Product> hotProList = null; try { hotProList = dao.findHotProduct(); } catch (SQLException e) { e.printStackTrace(); } return hotProList; } //获取最新商品 public List<Product> findNewProduct() { ProductDao dao = new ProductDao(); List<Product> newProList = null; try { newProList = dao.findNewProduct(); } catch (SQLException e) { e.printStackTrace(); } return newProList; } //获取商品列表 public List<Category> findCategory() { ProductDao dao = new ProductDao(); List<Category> categoryList = null; try { categoryList = dao.findCategory(); } catch (SQLException e) { e.printStackTrace(); } return categoryList; } //根据cid获取pagebean public PageBean findPageBeanByCid(String cid, int currentPage, int currentCount) { ProductDao dao = new ProductDao(); PageBean<Product> pageBean = new PageBean<Product>(); //当前页 pageBean.setCurrentPage(currentPage); //当前页显示条数 pageBean.setCurrentCount(currentCount); //总共条数 int totalCount = 0; try { totalCount = dao.findtotalCount(cid); } catch (SQLException e) { e.printStackTrace(); } pageBean.setTotalCount(totalCount); //总共页数 // math.ceil向上取整 该方法内的是double类型 所以乘以1.0 int totalPage = (int) Math.ceil(1.0*totalCount/currentCount); pageBean.setTotalPage(totalPage); //商品列表,计算从多少角标开始取 int index = (currentPage-1)*currentCount; List<Product> productList = null; try { productList = dao.findProductListByCid(cid,index,currentCount); } catch (SQLException e) { e.printStackTrace(); } pageBean.setList(productList); return pageBean; } //根据pid查找商品 public Product findProductByPid(String pid) { ProductDao dao = new ProductDao(); Product product =null; try { product = dao.findProductByPid(pid); } catch (SQLException e) { e.printStackTrace(); } return product; } }
ProductDao
package com.itheima.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 com.itheima.domain.Category;
import com.itheima.domain.Product;
import com.itheima.utils.DataSourceUtils;
public class ProductDao {
// 获取热门商品
public List<Product> findHotProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where is_hot=? limit ?,?";
List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), 1,0,9);
return query;
}
//获取最新商品
public List<Product> findNewProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
//此处注意使用order by pdate desc 时间最大开始选择
String sql ="select * from product order by pdate desc limit ?,?";
List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), 0,9);
return query;
}
//获取商品分类列表
public List<Category> findCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="select * from category";
List<Category> query = runner.query(sql, new BeanListHandler<Category>(Category.class));
return query;
}
// 根据商品分类的cid获取商品
public List<Product> findProductListByCid(String cid, int index, int currentPage) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where cid=? limit ?,?";
List<Product> query = runner.query(sql, new BeanListHandler<Product>(Product.class), cid,index,currentPage);
return query;
}
//根据cid查找商品总条数
public int findtotalCount(String cid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="select count(*) from product where cid=?";
Long query = (Long) runner.query(sql, new ScalarHandler(), cid);
//Long转为int
return query.intValue();
}
//根据pid查找商品
public Product findProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pid=?";
Product query = runner.query(sql, new BeanHandler<Product>(Product.class), pid);
return query;
}
}
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">首页 ></a> <a href="./蔬菜分类.htm">蔬菜 ></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="${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>编号:751</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> 收藏商品
</div>
<!-- 返回上一级 后台把当前页已经存过来-->
<a href="${pageContext.request.contextPath }/product?method=productListByCid&cid=${product.cid}¤tPage=${currentPage}">返回上一页</a>
</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="${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>
相关文章推荐
- Shop项目--4. 分页显示商品的,与分页跳转,上一页,下一页product_list.jsp
- Shop项目--6. 商品的浏览历史记录。product.list.jsp
- 案例22-显示商品的详细信息
- JSP浏览商品信息项目学习笔记
- 商品详情页显示商品的详细信息
- Shop项目后台--1.商品添加页面的类别ajax显示
- 商品浏览系统之商品详细信息显示页面实现
- Shop项目--12. 显示用户订单列表order_list.jsp
- 自定义DevExpress GridView Group时显示项目详细信息
- ECSHOP全站调用购物车显示商品图片,价格,数量,总价等详细信息
- 2013.11.28 Base64编码后的图片信息在JSP页面中的显示[JFinal框架项目]
- 求帮助——网页错误详细信息jsp代码中datetimepicker控件不能显示
- Struct2或者SSH项目中,在JSP页面显示Action中注册的错误信息
- 利用JDBC显示数据库详细信息---JSP实现
- 用jsp实现一个简单的购物车web应用系统。实现的添加购物商品,删除购物商品并且显示购物车信息。
- 利用JDBC显示数据库详细信息---JSP实现
- Struct2或者SSH项目中,在JSP页面显示Action中注册的错误信息
- ECSHOP全站调用购物车显示商品图片,价格,数量,总价等详细信息
- JSP第六次课:数据库访问显示商品信息
- Python爬虫小项目(1):抓取转转网西安二手商品的详细信息并导入mongo,绘制图表,慢更