JavaWeb尚硅⾕⽹上书城项⽬总结(下)JavaWeb尚硅⾕⽹上书城项⽬总结(下)
第七阶段:购物车(Session版本)
7.1 提取购物车模型
1. 购物车模块分析:
2. 购物车商品项
public class CartItem {
private Integer id;
private String name;
private Integer count;
private BigDecimal price;
private BigDecimal totalPrice;
}
3. 购物车对象
public class Cart {
private Map<Integer,CartItem> items =new HashMap<Integer,CartItem>();
/
**
* 添加商品项
*/
public void addItem(CartItem cartItem){
CartItem item = (Id());
if(item == null){
//之前没添加过此商品
items.Id(),cartItem);
}else{
//已经添加过的情况
item.Count()+Count());
item.Price().multiply(new Count())));
item.Price().multiply(new Count())));
}
}
/**
* 删除商品项
*/
public void deleteItem(Integer id){
}
/**
* 清空商品项
*/
public void clear(){
items.clear();
}
/**
* 修改商品数量
* @param id
* @param count
*/
public void updateCount(Integer id,Integer count){
CartItem cartItem = (id);
if(cartItem != null){
cartItem.setCount(count);//修改商品数量
cartItem.Price().multiply(new Count())));
}
}
//得到购物车总数量
public Integer getTotalCount(){
Integer totalCount =0;
for(Map.Entry<Integer,CartItem> entry : Set()){
totalCount += Value().getCount();
}
return totalCount;
}
//得到购物车总价格
public BigDecimal getTotalPrice(){
BigDecimal totalPrice =new BigDecimal(0);
for(Map.Entry<Integer,CartItem> entry : Set()){
totalPrice = totalPrice.Value().getTotalPrice());
}
return totalPrice;
}
}
7.2 加⼊购物车——如何跳回添加商品的页⾯(重点)
1. CartServlet 程序中的代码:
/**
* 加⼊购物车
* @param req
* @param resp
* @throws ServletException
* @throws IOException
*/
protected void addItem(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException { // 获取请求的参数商品编号
Integer id = WebUtils.Parameter("id"),0);
// 调⽤bookService.queryBookById(id):Book得到图书的信息
Book book = bookService.queryBookById(id);
// 把图书信息,转换成为CartItem商品项
CartItem cartItem =new Id(), Name(),1, Price(), Price());
// 调⽤Cart.addItem(CartItem);添加商品项
Cart cart =(Session().getAttribute("cart");
if(cart == null){
cart =new Cart();
}
cart.addItem(cartItem);
//将最后⼀个添加的商品名称加到session中
//重定向回之前的页⾯
String referer = Header("Referer");
resp.sendRedirect(referer);
}
2. index.jsp 页⾯ js 的代码:
//加⼊购物车操作
$(".addCartItem").click(function(){
var bookId = $(this).attr("bookId");
$.getJSON("localhost:8080/07_book/cartServlet","action=ajaxAddItem&id="+bookId,function(data){
$("#cartTotalCount").text("您的购物车中有"+alCount+"件商品");
$("#cartLastName").text(data.lastName);
})
//之前使⽤同步的⽅式处理
//在事件相应的function函数中,有⼀个this对象,这个this对象,是当前正在响应事件的dom对象
// location.href = "localhost:8080/07_book/cartServlet?action=addItem&id="+bookId;
});
重点:如何跳回添加商品的页⾯
HTTP 请求的头信息⾥⾯,Referer字段实际上告诉了服务器,⽤户在访问当前资源之前的位置
//重定向回之前的页⾯
String referer = Header("Referer");
resp.sendRedirect(referer);
7.3 购物车的展⽰(购物车存在session中)
<%@ taglib prefix="c" uri="java.sun/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
<%--静态包含 base标签,css样式,jQuery--%>
<%@ include file="/pages/common/head.jsp" %>
</head>
<body>
<div id="header">
<img class="logo_img"alt=""src="static/img/logo.gif">
<span class="wel_word">购物车</span>
<%@ include file="/pages/common/login_success_menu.jsp"%>
</div>
<div id="main">
<table>
<tr>
<td>商品名称</td>
<td>数量</td>
<td>单价</td>
<td>⾦额</td>
<td>操作</td>
</tr>
<if test="${empty sessionScope.cart.items}">
<%--如果购物车为空的情况--%>
<tr>
<td colspan="5"><a href="index.jsp">亲,您的购物车为空</a></td>
</tr>
</if>
<if test="${not empty sessionScope.cart.items}">
<%--如果购物车⾮空的情况--%>
<forEach items="${sessionScope.cart.items}"var="entry">
<tr>
<td>${entry.value.name}</td>
<td><input class="updateCount"type="text"bookId="${entry.value.id}"
value="${unt}" /></td>
<td>${entry.value.price}</td>
java和jsp
<td>${alPrice}</td>
<td><a class="deleteItem"href="cartServlet?action=deleteItem&id=${entry.value.id}">删除</a></td>
</tr>
</forEach>
</if>
</table>
<%--如果购物车⾮空才输出页⾯的内容--%>
<if test="${not empty sessionScope.cart.items}">
<div class="cart_info">
<span class="cart_span">购物车中共有<span class="b_count">${alCount}</span>件商品</span> <span class="cart_span">总⾦额<span class="b_price">${alPrice}</span>元</span>
<span class="clearCart"><a href="cartServlet?action=clear">清空购物车</a></span>
<span class="cart_span"><a href="pages/cart/checkout.jsp">去结账</a></span>
</div>
</if>
</div>
<%--静态包含页脚内容--%>
<%@ include file="/pages/common/footer.jsp" %>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。