一、首页布局
<%@page import="com.zking.goods.entity.Goods"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.goods.biz.IGoodsBiz"%>
<%@page import="com.zking.goods.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.goods.entity.Users"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 通过session默认保存一个用户 -->
<%
Users users = new Users(1,"zz","123",1,1000);
//存储到session
session.setAttribute("users", users);
%>
<h3>zz购物商城首页</h3>
<% Users u = (Users)session.getAttribute("users"); %>
<p>欢迎您!<%=u.getCname() %></p>
<table border ="1" width = "70%">
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>描述</th>
<th>图片</th>
<th>操作</th>
</tr>
<%
//调用biz层显示所有的数据
IGoodsBiz igb = new GoodsBizImpl();
//调用查询所有的方法
List<Goods> listGoods = igb.queryGoodsAll();
for(Goods goods:listGoods){
%>
<tr>
<td><%=goods.getGid() %></td>
<td><%=goods.getGname() %></td>
<td><%=goods.getGprice() %></td>
<td><%=goods.getGinfo() %></td>
<td><img src = "<%=goods.getGpath()%>"/></td>
<td>
<button onclick="addCart(<%=goods.getGid()%>)">加入购物车</button>
</td>
</tr>
<%
}
%>
</table>
<script type="text/javascript">
//加入购物车的点击事件
function addCart(cid){
//alert(cid)
location.href = "doShopping.jsp?cid="+cid;
}
</script>
</body>
</html>
二、加入购物车
<%@page import="com.ys.cart.entity.Cart"%>
<%@page import="com.ys.cart.entity.Goods"%>
<%@page import="com.ys.cart.biz.impl.GoodsBizImpl"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 加入购物车的do处理页面 -->
<!--
购物车流程:
1.Goods---显示商品信息
2.Cart ---购物车中的信息
加入购物车 不是将Goods进行存储 而是存储的是Cart实体
-->
<%
//将点击的商品存储到购物车容器中(session)
//设置编码
request.setCharacterEncoding("utf-8");
//获取商品编号
String id = request.getParameter("gid");
int gid = 0;
if (null != id) {
gid = Integer.valueOf(id);
}
//拿到cid 根据cid获取商品的其它信息
Goods goods = new GoodsBizImpl().getGoodsByCid(gid);
//数量默认一件
//总价格 商品的单价 * 数量(1件)
//将上面获取的信息封装到Cart实体中
Cart cart = new Cart();
cart.setGoods(goods);
cart.setCcount(1);
cart.setCtotal();
//1.获取购物车
List<Cart> listCarts = (List<Cart>) session.getAttribute("listCarts");
//2.判断非空
boolean flag = true;
if (null == listCarts) {//说明用户时第一次将商品加入购物车
//创建购物车
listCarts = new ArrayList<Cart>();
} else {//否则 购物车中存在商品
//遍历所有的购物车中的商品 看该购物车中是否存在刚才点击的cart实体封装
for (Cart c : listCarts) {
//判断传递的cid与当前listCarts中的每一个cid进行匹配
if (gid == c.getGoods().getGid()) {//说明购物车中存在该商品
flag = false;
//修改数量
c.setCcount(c.getCcount() + 1);
//修改总价格
c.setCtotal();
}
}
}
//判断flag标记
if (flag == true) {//说明购物车中没有该商品
listCarts.add(cart);
}
//重新保存购物车
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp");
%>
三、删除
<%
String id = request.getParameter("gid");
int cid = 0;
if(null!=id){
cid = Integer.valueOf(id);
}
//获取购物车
List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
System.out.println(listCarts);
/* for(int i = 0;i<listCarts.size();i++){
if(cid == listCarts.get(i).getGoods().getGid()){
listCarts.remove(i);
}
} */
//迭代器的方式进行删除
Iterator<Cart> its = listCarts.iterator();
while(its.hasNext()){
Cart cc = its.next();
//its.remove();
if(cc.getGoods().getGid() == cid){
its.remove();
}
}
session.setAttribute("listCarts", listCarts);
response.sendRedirect("Cart.jsp");
%>
四、修改
<%
String id1 = request.getParameter("gid");
String id2 = request.getParameter("ccount");
int gid = 0;
if(id1!=null){
gid = Integer.valueOf(id1);
}
int ccount = 0;
if(id2!=null){
ccount = Integer.valueOf(id2);
}
//获取购物车
List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
for(Cart c:listCarts){
if(c.getGoods().getGid() == gid){
c.setCcount(ccount);
c.setCtotal();
}
}
session.setAttribute("listCarts", listCarts);
response.sendRedirect("Cart.jsp");
%>