一、商品界面绑定数据:
简略界面代码:
<!-- 通过C标签判断是否在域对象中存在数据 -->
<c:if test="${empty listGoods}">
<jsp:forward page="GOODSListServlet"></jsp:forward>
</c:if>
<h2 style="text-align: center">我的购物商场首页</h2>
<hr/>
<p>欢迎您!大佬!——${users.yname}</p>
<%-- <center> --%>
<table border="1" width="100%">
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品简介</th>
<th>商品图片</th>
<th>操作</th>
</tr>
<!-- 通过C标签遍历数据 -->
<c:forEach items="${listGoods}" var="goods">
<tr>
<td>${goods.gid}</td>
<td>${goods.gname}</td>
<td>${goods.gprice}</td>
<td>${goods.ginfo}</td>
<td><img src="${goods.gpath}"/></td>
<td><button onclick="addCart(${goods.gid})">加入购物车</button></td>
</tr>
</c:forEach>
</table>
<%-- </center> --%>
servlet代码:
//1.设置字符集编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//2.获取参数 省略
//3.封装 调用biz判断
IGoodsBiz igb=new GoodsBiz();
List<Goods> listGoods=igb.getAll();
//System.out.println(listGoods);
//数据保存在域对象中
//获取域对象 session
HttpSession session=request.getSession();
session.setAttribute("listGoods", listGoods);
//获取当前项目的绝对路径
String path=request.getServletContext().getContextPath();//
//4.响应
response.sendRedirect("index.jsp");
二、点击加入购物车:
我的购物车界面代码:
<h1>我的购物车</h1>
<p>
<a href="index.jsp">返回首页</a>
</p>
<hr />
<table border="1" width="50%">
<tr>
<th>购物车编号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品图片</th>
<th>商品数量</th>
<th>商品总价</th>
<th>操作</th>
</tr>
<!-- 通过C标签遍历数据 -->
<c:forEach items="${listCarts}" var="cart">
<tr>
<td>${cart.cid}</td>
<td>${cart.g.gname}</td>
<td>${cart.g.gprice}</td>
<td><img src="${cart.g.gpath}" /></td>
<td>${cart.ccount}</td>
<td>${cart.ctotal}</td>
<td><button onclick="sc()?cid=${cart.cid}">删除</button>
<button>修改</button></td>
</tr>
</c:forEach>
</table>
<p style="text-align: rigth">
<button style="background: pink;color:white;font-weight:bold;width:100px;height:50px;border-radius: 100%;cursor: pointer;" onclick="buy()">结算</button>
</p>
servlet代码:
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//2.获取location带过来的gid
String id=request.getParameter("gid");
int gid=0;
if(null!=id) {
gid=Integer.valueOf(id);
}
//数据封装
Cart c=new Cart();
//将gid Userid ccount ctotal 封装到cart实体对象中
IGoodsBiz igb=new GoodsBiz();
Goods g=igb.getGoods(gid);
//Userid uuid
HttpSession session=request.getSession();
Yongy y=(Yongy)session.getAttribute("users");
//System.out.println(y);
c.setG(g);
c.setY(y);
c.setCcount(1);
c.setCtotal();
//要想将封装好的购物车信息 保存到数据库中
//先获取当前这个登陆的用户的购物车
ICartBiz icb=new CartBiz();
List<Cart> listCarts=icb.getCartByUsersid(y.getYid());
//如果购物车为空 List<Cart> listCarts=null;
boolean f=true;
//如果该用户是第一次进入 构建一个新的购物车 实例化
if(null==listCarts) {
listCarts=new ArrayList<Cart>();
}else {//非第一次
//遍历所有的商品
for(Cart cart:listCarts) {
//判断当前加入的与购物车中已有的进行匹配
if(cart.getG().getGid()==c.getG().getGid()) {
//不需要重复添加 修改数量及总价格即可
cart.setCcount(cart.getCcount()+1);
cart.setCtotal();
//进行一个数据库的更新
icb.editCartByCidAndUserid(cart);
f=false;
break;
}
}
}
//问题:加入购物车 不是加入集合 而是加入数据库
//listCarts.add(c);
if(f) {
icb.addCart(c);
}
//加入后数据要继续更新
listCarts=icb.getCartByUsersid(y.getYid());
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp");
}