网上商城第二天笔记

一:最新和最热商品

1.servlet层
新建IndexServlet  /index
ProductService service = new ProductService();
//准备热门商品--List<Product>
List<Product> hotProductList=service.findHotProductList();


   
//准备最新商品--List<Product>
List<Product> newProductList=service.findNewProductList();








request.setAttribute("hotProductList",hotProductList);
request.setAttribute("newProductList",newProductList);
//转发
request.getRequestDispatcher("/index.jsp").forward(request,response);
2.domain层
public class Product{
private String pid;
private String pname;
private double market_price;
private double shop_price;
private String pimage;
private Date pdate;
private int is_hot;
private String pdesc;
private int pflag;
private Category category;

getter 和 setter


}


public class Category{
private String cid;
private String cname;
getter和setter



}
3.service层
提供对应的findHotProductList()和findNewProductList()
调用Productdao


4.dao层
//hotProductList
String sql="select * from product where is_hot=? limit ?,?";
return runner.query(sql,new BeanListHandler<Product>Product.class),1,0,9);
//newProductList
String sql="select * from product order by pdate desc limit ?,?";
return runner.query(sql,new BeanListHandler<Product>(Product.class),1,0,9);


5.index.jsp
引入标签库 <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefic="c" %>
最热商品
<c:forEach items="${hotProductList}" var="hotPro">
<img src="${pageContext.request.contextPath}/${hotPro.pimage}/>
${hotPro.pname}
${hotPro.shop_price}




</c:forEach>
最新商品
同上






6.web.xml
default.jsp中
<body>
<%
response.sendRedirect(request.getContextPath()+"/index");
%>
</body>


二:使用Ajax显示类别菜单



1.servlet层中
IndexServlet
//准备分类数据
List<Category> categoryList = service.findAllCategory();
2.service层
...
3.dao层
select * from category;




4.request.setAttribute("categoryList",categoryList);
5.header.jsp中
<c:forEach>

</c:forEach>
这样只能首页有分类数据
----------------------------------------------------------------
1.使用Ajax在header.jsp中
<script type="text/javascript">
$(function(){
var content = "";
$.post(
"${pageContext.request.contextPath}/categoryList",
function(data){
//[{"cid":"xxx","cname":"xxx"},{},{}]
//动态创建<li><a href="#">${category.cname}</a></li>
for(var i=0;i<data.length;i++){
content+="<li><a href='#'>"+data[i].cname+"</a></li>";
}
//将拼接好的li放置到ul中
$("#categoryUl").html(content);
},
"json"
);
});
</script>




2.CategoryServlet中:
ProductService service = new ProductService();
//准备分类数据
List<Category> categoryList=service.findAllCategory();
Gson gson = new Gson();
String json = gson.toJson(categoryList);


response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(json);


//导入gson.jar包


三:使用redis缓存显示类别菜单

1.CategoryListServlet中
//先从缓存中查询categoryList,如果有直接使用没有从数据库读
导入包pool,jedis两个包,配置文件,和工具类jEdisPoolUtils
//1.获得jedis对象 连接redis数据库
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
if(categoryListJson==null){
System.out.println("缓存没有数据");
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson",categoryListJson);
}

四:分页显示某种类别的商品列表

1.CategoryListServlet中
//先从缓存中查询categoryList,如果有直接使用没有从数据库读
导入包pool,jedis两个包,配置文件,和工具类jEdisPoolUtils
//1.获得jedis对象 连接redis数据库
Jedis jedis = JedisPoolUtils.getJedis();
String categoryListJson = jedis.get("categoryListJson");
if(categoryListJson==null){
System.out.println("缓存没有数据");
List<Category> categoryList = service.findAllCategory();
Gson gson = new Gson();
categoryListJson = gson.toJson(categoryList);
jedis.set("categoryListJson",categoryListJson);
}


五:显示商品的详细信息


1.product_list.jsp中
<a href="${pageContext.request.contextPath}/productInfo?pid=${pro.pid}">
2.servlet中
新建ProductInfoServlet /productInfo
//获得要查询的商品的pid
String pid = request.getParameter("pid");


ProductService service = new ProductService();
Product product = service.findProductByPid(pid);


request.setAttribute("pageBean",pageBean);
request.setAttribute("cid", cid);
request.getRequestDispatcher("/product_info.jsp").forward(request,response);
3.service中 
调dao
4.dao中
select * from product where pid=?;
5.product_info.jsp中
 src="${pageContext.request.contextPath}/${product.pimage}"
 ${product.pname}
 ${product.pid}
 ${product.shop_price}
 ${product.market_price}
 


六:浏览历史记录分析

1.ProductInfoServlet中
//获得要查询的商品的pid
String pid = request.getParameter("pid");
//获得商品类别
String cid = request.getParameter("cid");
//获得当前页
String currentPage = request.getParameter("currentPage");

ProductService service = new ProductService();
Product product = service.findProductByPid(pid);

request.setAttribute("product", product);
request.setAttribute("cid", cid);
request.setAttribute("currentPage", currentPage);
request.getRequestDispatcher("/product_info.jsp").forward(request,response);

2.product_info.jsp中
返回列表href
${pageContext.request.contextPath}/productListByCid?cid=${cid}&currentPage=${currentPage}
3存pid到cookie再写给客户端,拼接pid

七:浏览历史记录的代码实现

1.ProductInfoServlet中
//获得客户端携带的cookie 名字为pids的cookie
String pids = pid;


Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie : cookies){
if("pids".equals(cookie.getName())){
pids = cookie.getValue();//1-3-2
//将pids拆成一个数组
String[] split = pids.split("-");//{3,1,2}
List<String> asList = Arrays.asList(split);//[3,1,2]
LinkedList<String> list = new LinkedList<String>(asList);
//判断集合中是否存在当前pid
if(list.contains(pid)){
//包含在当前查看商品的pid
list.remove(pid);
list.addFirst(pid);
}else{
//不包含
list.addFirst(pid);
}
//将[3,1,2]转成3-1-2字符串
StringBuffer sb = new StringBuffer();
for(int i=0;i<list.size()&&i<7;i++){
sb.append(list.get(i));
sb.append("-");
}
//去掉3-1-2-后的-
pids = sb.substring(0,sb.length()-1);

}
}
}
Cookie cookie_pids = new Cookie("pids",pids);
response.addCookie(cookie_pids);
2.ProductListByCidServlet中


//定义一个记录历史商品信息的集合
List<Product> historyProductList = new ArrayList<Product>();
//获得客户端携带名字叫pids的cookie


Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie:cookies){
if("pids".equals(cookie.getName())){
String pids = cookie.getValue();//3-2-1
String[] split = pids.split("-");
for(String pid:split){
Product pro = service.findProductByPid(pid);
historyProductList.add(pro);
}
}
}
}
//将历史记录的集合放到域中
request.setAttribute("historyProductList",historyProductList);
//转发
3.product_list.jsp中
<c:forEach items="${historyProductList}" var="historyPro">
img src="${pageContext.request.contextPath}/${historyPro.pimage}"
</c:forEach>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值