JavaWeb.购物车项目二
前言:这一篇文章是在上一篇的基础上优化了购物车,增加了购物车界面和商品删除,商品数量加减,清空购物车等功能。目录购物车界面代码(car.jsp)商品添加购物车代码(doAddCar.jsp)从购物车删除商品的代码(doDelCar.jsp)主页面代码(index.jsp)修改商品数量代码(doUpdCar.jsp)清空(结算)购物车代码(doClear.jsp)退出登陆代码(doExit.jsp)
·
前言:这一篇文章是在上一篇的基础上优化了购物车,增加了购物车界面和商品删除,商品数量加减,清空购物车等功能。
目录
购物车界面代码(car.jsp)
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(4)::before,
small {
content: "$";
}
* {
outline: none !important;
}
td,
th {
text-align: center;
}
input {
text-align: center;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临胡阿玛购物车🛒</h1>
<p>尊贵的xxx</p>
<p>
<a href="doExit.jsp" class="btn btn-warning">退出登陆</a>
</p>
</div>
</div>
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品个数</th>
<th>商品总价</th>
<th>操作</th>
</tr>
<%
int sum=0;
List<CarItem> car = (List<CarItem>)session.getAttribute("car");
for (CarItem carItem : car){
//每个商品都有自己的价格
sum+=carItem.getSum();
%>
<tr>
<td style="line-height: 30.5px;"><%=carItem.getGoods().getId()%></td>
<td style="line-height: 30.5px;"><%=carItem.getGoods().getName() %></td>
<td>
<div class="input-group" style="width: 120px;margin: auto;">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&type=0" class="btn btn-default" type="button">-</a>
</span>
<input type="number" onblur="location.href='doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&count='+this.value" value="<%=carItem.getCount() %>" type="text" class="form-control">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=<%=carItem.getGoods().getId() %>&type=1" class="btn btn-default" type="button">+</a>
</span>
</div>
</td>
<td style="line-height: 30.5px;"><%=carItem.getSum() %></td>
<td style="line-height: 30.5px;">
<a href="doDelCar.jsp?id=<%=carItem.getGoods().getId() %>" class="btn btn-primary">删除</a>
</td>
</tr>
<%
}
%>
</table>
<h1 class="alert alert-info">
当前购物车总价
<small><%=sum %></small>
<a href="doClear.jsp" class="btn btn-danger">点我结算</a>
</h1>
</body></html>
商品添加购物车代码(doAddCar.jsp)
<%@page import="java.util.List"%>
<%@page import="com.zking.biz.impl.GoodsBizlmpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.vo.CarItem"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//拿购物车
List<CarItem> car=(List<CarItem>)session.getAttribute("car");
IGoodsBiz goodsBiz=new GoodsBizlmpl();
//添加购物车界面
String str= request.getParameter("id");
int id=-1;
if(str!=null){
id=Integer.parseInt(str);
}
//2-1.判断该商品是否存在
boolean f=true;
for (CarItem item : car) {
// item.getGoods().getId() 条目的商品id
if(id==item.getGoods().getId()){
//商品应该是已经被添加购物车了 [购物车中某个条目的商品id和你需要添加的商品id相同了]
item.setCount(item.getCount()+1);//数量+1
item.setSum(item.getCount()*item.getGoods().getPrice());
f=false;
break;
}
}
//只要判断f是否发生了改变
if(f){
//2-2.生成一个CarItem [如果购物车没有该商品]
CarItem carItem=new CarItem();
//设置对应的商品数据
carItem.setGoods(goodsBiz.getOne(id));
out.print(goodsBiz.getOne(id));
//商品加车的数量
carItem.setCount(1);
//carItem.getCount() 商品加车的数量
//carItem.getGoods().getPrice() 商品的单价
//加车数量*商品单价
carItem.setSum(carItem.getCount()*carItem.getGoods().getPrice());
//将 购物条目[carItem] 绑定到 购物车[car] 中
car.add(carItem);
}
//更新购物车
session.setAttribute("car", car);
//跳回首页
response.sendRedirect("index.jsp");
%>
从购物车删除商品的代码(doDelCar.jsp)
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
List<CarItem> car = (List<CarItem>)session.getAttribute("car");
//商品的id
Integer id = Integer.parseInt(request.getParameter("id"));
//删除购物车中对应的商品
for(CarItem c : car){
if(c.getGoods().getId()==id){
car.remove(c);
break;
}
}
/**
for(int i=0;i<car.size();i++){
if(car.get(i).getGoods().getId()==id){
car.remove(car.get(i));
break;
}
}
**/
//更新购物车
session.setAttribute("car", car);
//跳回首页
response.sendRedirect("car.jsp");
%>
主页面代码(index.jsp)
<%@page import="com.zking.pojo.Goods"%>
<%@page import="com.zking.biz.impl.GoodsBizlmpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(3)::before{
content: "$";
}
</style>
</head>
<body>
<%
Object obj= session.getAttribute("user");
if(obj==null){
response.sendRedirect("login.jsp");
return;
}
%>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临嘿嘿嘿SuperMarket</h1>
<p>尊贵的<%=((User)obj).getAccount() %></p>
<p><a href="car.jsp" class="btn btn-primary">🛒</a></p>
</div>
</div>
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品描述</th>
<th>操作</th>
</tr>
<%
IGoodsBiz goodsBiz=new GoodsBizlmpl();
for(Goods goods : goodsBiz.getAll()){
%>
<tr>
<td><%=goods.getId() %></td>
<td><%=goods.getName() %></td>
<td><%=goods.getPrice() %></td>
<td><%=goods.getInfo() %></td>
<td>
<div class="btn-group btn-group-xs">
<a href="doAddCar.jsp?id=<%=goods.getId()%>" class="btn btn-primary">添加购物车</a>
</div>
</td>
</tr>
<%
}
%>
</table>
</body></html>
修改商品数量代码(doUpdCar.jsp)
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
List<CarItem> car = (List<CarItem>)session.getAttribute("car");
//商品的id
Integer id = Integer.parseInt(request.getParameter("id"));
//类型
String type = request.getParameter("type");
//查出对应的条目
CarItem i = null;
//修改购物车商品的数量
for(CarItem c : car){
if(c.getGoods().getId()==id){
i=c;
break;
}
}
if(type!=null){
i.setCount(i.getCount()+(type.equals("0")?-1:1));
}
else{
i.setCount((int)Double.parseDouble(request.getParameter("count")==null?"1":request.getParameter("count")));
}
i.setCount(i.getCount()>0?i.getCount():1);
i.setSum(i.getCount()*i.getGoods().getPrice());
//更新购物车
session.setAttribute("car", car);
//跳回首页
response.sendRedirect("car.jsp");
%>
清空(结算)购物车代码(doClear.jsp)
<%@page import="java.util.List"%>
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//清空购物车
//方法一
//session.setAttribute("car",new ArrayList<CarItem>() );
//方法二
List<CarItem> car = (List<CarItem>)session.getAttribute("car");
car.clear();
//更新购物车
session.setAttribute("car", car);
//跳回首页
response.sendRedirect("car.jsp");
%>
退出登陆代码(doExit.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//让session失效
session.invalidate();
response.sendRedirect("index.jsp");
%>
运行界面
今天就到这结束了,拜拜(o゚v゚)ノ。
更多推荐
已为社区贡献4条内容
所有评论(0)