目录
一、项目后半部分文件目录的展示
二、 项目后半部分功能的编写:
1、购物车界面的显示功能
在购物车页面可以显示所有添加到购物车的商品,可以对购物车的商品进行删除:
购物车界面 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="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="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>欢迎光临you小爷购物车🛒</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>
注意点:代码中的购物车集合是存到session里的,因为从登录页面进来后就会有一个购物车,这个购物车就存在了session中
doLogin.jsp:(做登录页面,也是从这里开始拿购物车的)
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.pojo.User"%>
<%@page import="com.zking.biz.impl.UserBizImpl"%>
<%@page import="com.zking.biz.IUserBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//拿请求中的数据
request.setCharacterEncoding("utf-8");
String account=request.getParameter("account");
String password=request.getParameter("password");
//jsp表示层调用业务逻辑层
IUserBiz userBiz=new UserBizImpl();
User user=userBiz.login(new User(0,account,password));
if(user==null){undefined
//重定向到登录页面
response.sendRedirect("login.jsp");
}else{undefined
//首页需要登录数据
session.setAttribute("user", user);
//找来购物车
List<CarItem> car=new ArrayList<>();
//放到session中(把购物车给我)
session.setAttribute("car", car);
//登陆成功后,跳转到首页
response.sendRedirect("index.jsp");
}
%>
2、 修改购物车商品数量,和计算单个商品总价的功能
在购物车里,可以选择对商品的数量进行修改,每次修改后,单个商品的总价会跟着变化:
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 item:car){
if(item.getGoods().getId()==id){
i=item;
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");
%>
3、 删除购物车选项的功能代码
在购物车页面,可以对商品选项进行删除,
处理删除的代码,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 item:car){
if(item.getGoods().getId()==id){
car.remove(item);
break;
}
}
//更新购物车
session.setAttribute("car", car);
//跳回首页
response.sendRedirect("car.jsp");
%>
4、 主页的退出系统的功能
在主页可以点击退出按钮,退回到登录页面,
doExit.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.invalidate();//让session失效
response.sendRedirect("index.jsp");
%>
5、 计算购物车所有商品的总价
在购物车页面,当所有的商品数量失去焦点时,页面更新后,会计算出所有商品的总价,
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");
%>