2022-11-18 20:46:59
浏览数 (1)
今天给大家讲的就是session项目,用到了昨天的那篇文章里面的一些东西。
今天就不多说了,可以看下面的思维导图。
下面是这个项目的效果图,用到了一些插件。
以下是代码
index.jsp主页面
代码语言:javascript
复制<%@page import="com.zking.entity.Goods"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.zking.dao.GoodsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"/>
<script type="text/javascript">
function gm(bid) {
//跳转到dogwc页面
//alert(bid);
location.href="dogwc.jsp?bid=" bid;
}
</script>
</head>
<body>
<table class="table table-hover">
<tr>
<td>商品编号</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品介绍</td>
<td>商品图片</td>
<td>
<span class="glyphicon glyphicon-cog"></span>
</td>
</tr>
<%
GoodsDao gd = new GoodsDao();
ArrayList<Goods> glist = gd.getAll();
for(Goods g:glist){
%>
<tr>
<td><%=g.getBid() %></td>
<td><%=g.getBname() %></td>
<td><%=g.getBprice() %></td>
<td><%=g.getBinfo() %></td>
<td>
<img alt="" src="<%=g.getBface() %>">
</td>
<td>
<button onclick="gm(<%=g.getBid() %>)" class="btn btn-success">添加购物车</button>
</td>
</tr>
<%} %>
</table>
</body>
</html>
dogwc.jsp
代码语言:javascript
复制<%@page import="com.zking.dao.GoodsDao"%>
<%@page import="com.zking.entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
//获取商品订单的数量
String number =request.getParameter("gn");
int count=1;
if(number!=null){
count=Integer.valueOf(number);
}
//接受商品的编号
int bid=Integer.valueOf(request.getParameter("bid"));
//构造小订单对象
OrderItem oi=new OrderItem();
//给属性赋值
oi.setGoods(new GoodsDao().getById(bid));
//订单中的商品数量
oi.setGnumber(1);
//订单总价
oi.setSumPrice();
//获取session中的订单集合
ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");
if(olist==null){
//创建订单集合
olist=new ArrayList<>();
}
boolean b=true;//表示默认 没有相同的订单
double SumPrice=0;
//遍历订单集合,判断是否已存在相同商品订单
for(int i=0;i<olist.size();i ){
if(bid==olist.get(i).getGoods().getBid()){
//number为空说明是从index页面过来的
if(number==null){
//修改数量:原来的数量 1
olist.get(i).setGnumber(olist.get(i).getGnumber() 1);
//修改总价
olist.get(i).setSumPrice();
}else{//number为空说明是从spcar页面过来的
//修改数量:原来的数量修改为count
olist.get(i).setGnumber(count);
//修改总价
olist.get(i).setSumPrice();
}
b=false;
}
}
if(b){
//把订单放到ArrayList集合中
olist.add(oi);
}
//把集合放到session中
session.setAttribute("olist", olist);
//遍历订单集合
for(int i=0;i<olist.size();i ){
SumPrice =olist.get(i).getSumPrice();
}
session.setAttribute("SumPrice", SumPrice);
//跳转页面
response.sendRedirect("spcar.jsp");
%>
dodele.jsp删除
代码语言:javascript
复制<%@page import="com.zking.entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
//接受商品的编号
int bid=Integer.valueOf(request.getParameter("bid"));
//获取session中的订单集合
ArrayList<OrderItem>olist=(ArrayList<OrderItem>)session.getAttribute("olist");
//遍历订单集合,判断是否已存在相同商品订单
for(int i=0;i<olist.size();i ){
if(bid==olist.get(i).getGoods().getBid()){
olist.remove(i);
}
}
double SumPrice=0;
for(int i=0;i<olist.size();i ){
SumPrice =olist.get(i).getSumPrice();
}
session.setAttribute("SumPrice", SumPrice);
//把集合放到session中
session.setAttribute("olist", olist);
//跳转页面
response.sendRedirect("spcar.jsp");
%>
spcar.jsp购物车页面
代码语言:javascript
复制<%@page import="com.zking.entity.OrderItem"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引用jQuery库 -->
<script type="text/javascript" src="js/jquery-3.3.1.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<script type="text/javascript">
function xg(obj,bjd) {
var gnumber=obj.value;
if(gnumber>0){
location.href="dogwc.jsp?bid=" bjd "&gn=" gnumber;
}else{
alert("商品数量不能<=0:");
}
}
function ff(xx,bid,i) {
var gnumber=document.getElementById(xx).value;
if(gnumber>0){
if(i==1){
gnumber--;
location.href="dogwc.jsp?bid=" bid "&gn=" gnumber;
}else if(i==2) {
gnumber ;
location.href="dogwc.jsp?bid=" bid "&gn=" gnumber;
}
}else{
alert("商品数量不能<=0:");
}
}
</script>
</head>
<body>
<h1 align="center">
<a href="index.jsp">
<span class="glyphicon glyphicon-home"></span>
</a>
</h1>
<table class="table table-hover">
<tr>
<td>商品图片</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品介绍</td>
<td>商品数量</td>
<td>订单总价</td>
<td>操作一下</td>
</tr>
<%
// 获取到session中的订单集合
ArrayList<OrderItem> olist=(ArrayList<OrderItem>)session.getAttribute("olist");
//把订单绑定到页面
for(int i=0;i<olist.size();i ){
%>
<tr>
<td>
<img alt="" src="<%=olist.get(i).getGoods().getBface() %>">
</td>
<td><%=olist.get(i).getGoods().getBname() %></td>
<td><%=olist.get(i).getGoods().getBprice() %></td>
<td><%=olist.get(i).getGoods().getBinfo() %></td>
<td>
<button onclick="ff(<%=olist.get(i).getGoods().getBid() %>,<%=olist.get(i).getGoods().getBid() %>,1)">-</button>
<input onblur="xg(this,<%=olist.get(i).getGoods().getBid() %>)" id="<%=olist.get(i).getGoods().getBid() %>" style='width: 40px;text-align:center;'type="text" value="<%=olist.get(i).getGnumber() %>">
<button onchange="if(/D/.test(this.value)){alert('只能输入数字');this.value='';}" onclick="ff(<%=olist.get(i).getGoods().getBid() %>,<%=olist.get(i).getGoods().getBid() %>,2)"> </button>
</td>
<td><%=olist.get(i).getSumPrice() %></td>
<td><a href="dodele.jsp?bid=<%=olist.get(i).getGoods().getBid() %>"><span class="glyphicon glyphicon-trash"></span></a></td>
</tr>
<%} %>
</table>
<p align="right" style="margin-rigth:40px">
<button class="btn btn-success">总价格:<%=session.getAttribute("SumPrice") %></button>
</p>
</body>
</html>
以上就是今天的一些简单分享
继续加油