目錄
?一、實現(xiàn)思路
二、JSP 頁面實現(xiàn)(臨時性購物車項目)
????????第一部分:images(圖片)
????????第二部分:SQL代碼
????????第三部分:代碼
?????????????????實體層(entity):
????????????????????????1.entity 包 (package com.zking.goods.entity;)
????????????????????????????????1.1 Users.java (用戶實體類)
? ? ????????????????? ? 1.2?Goods.java? (商品實體類)
????????????????????????1.3 Cart.java (購物車實現(xiàn)類)
????????????????數(shù)據(jù)庫訪問包utils (package com.zking.goods.utils;)
? ? ????????????????? ? 1.1 BaseDao.java 萬能增刪改查方法(簡易)實現(xiàn)類
????????????????? ? ? ? 1.2 DBHelper.java 數(shù)據(jù)庫幫助類
????????業(yè)務(wù)邏輯層 BBL(biz|services):
????????? ? ? ? 1.biz包代碼 (package com.zking.goods.biz;)? ? ? ?IGoodsBiz.java?(商品方法定義接口)
????????????????2.biz包中的impl包代碼? (package com.zking.goods.biz.impl;)? ?GoodsBizImpl.java (商品方法實現(xiàn)類命名)
????????數(shù)據(jù)訪問層 DAL? ?(dao):
????????????????1.dao包代碼(package com.zking.goods.dao;)? ? ? ?
????????????????????????1.1 IGoodsDao.java (商品方法定義接口)
????????第四部分:JSP 代碼?
????????????????1.index.jsp? ? ? ? 商品信息顯示頁面
????????????????2.doShopping.jsp? ? ? ? 購物車處理頁面?
????????????????3.cart.jsp? ? ? ? 實現(xiàn)購物車界面
????????????????4.doDel.jsp? ? ? ? 購物車刪除處理頁面
????????????????5.doUpdate.jsp (修改購物車信息的處理頁面)
?一、實現(xiàn)思路
購物車shop
- 前臺
- 包括用戶注冊,登錄 ?
Html +js+jsp
考慮自動登錄(cookie實現(xiàn) ?登錄頁面)
- 進(jìn)入購物頁面購物(數(shù)據(jù)均來自數(shù)據(jù)庫),商品展示頁面shop.jsp顯示如下(大概顯示效果如下,為了界面美觀,可以加入相應(yīng)的商品圖片 )(該頁面相當(dāng)于游客頁面)
![]()
?1.點擊”加入購物車”?的同時,要將數(shù)據(jù)(訂單項的集合)保存到session中,再轉(zhuǎn)入下圖購物車頁面cart.jsp(必須登錄才能進(jìn))(大概效果如下)[從session中取出訂單項集合]
- 其中商品數(shù)量可以在修改數(shù)量以后點擊【修改】按鈕進(jìn)行修改
- 刪除按鈕則可以刪除該購買商品(必須有提示)
- 點擊繼續(xù)購物后應(yīng)該轉(zhuǎn)入購物頁面繼續(xù)購物
- 點擊結(jié)算后應(yīng)該轉(zhuǎn)入結(jié)賬頁面,提示用戶當(dāng)前購物車總金額,并清空購買信息。
- 訂單項組成:商品基本信息Goods??數(shù)量num ?單個商品的總價sumprice
- 后臺
- 包括對商品的管理(考慮刪除的真正含義)以及 分頁 下架 ?狀態(tài)列0 1
- 包括對用戶的管理(考慮刪除的真正含義)以及 分頁
- **思考訂單&訂單項(一個訂單可以包含多個訂單項)的概念
- 解決兩個問題:訂單項覆蓋問題 加同一個商品重復(fù)問題
涉及的技術(shù)點:
?JDBC
?Entity:
??--Goods
??--User
?
Session:購物車(增刪改查)
二、JSP 頁面實現(xiàn)(臨時性購物車項目)
????????第一部分:images(圖片)
????????第二部分:SQL代碼
--數(shù)據(jù)庫設(shè)計 CartDB
--創(chuàng)建商品表
create table goods
(
gid --商品編號
gname --商品名稱
gprice --商品單價
ginfo --商品描述信息
gpath --商品圖片路徑
)
--插入具體的數(shù)據(jù)
insert into goods?values(1,'mp31',100,'mp31','images/1.jpg')?;
insert into goods?values(2,'mp32',1200,'mp32','images/2.jpg')?;
insert into goods?values(3,'mp33',100,'mp33','images/3.jpg')?;
insert into goods?values(4,'mp34',1600,'mp34','images/4.jpg')?;
insert into goods?values(5,'mp35',1500,'mp35','images/5.jpg')?;
insert into goods?values(6,'mp36',1400,'mp36','images/6.jpg')?;
insert into goods?values(7,'mp37',1700,'mp37','images/7.jpg')?;
insert into goods?values(8,'mp38',1300,'mp38','images/8.jpg')?;
insert into goods?values(9,'mp39',1500,'mp39','images/9.jpg')?;
insert into goods?values(10,'mp310',1600,'mp310','images/10.jpg')?;
insert into goods?values(11,'mp311',1600,'mp311','images/11.jpg');
????????第三部分:代碼
?????????????????實體層(entity):
????????????????????????1.entity 包 (package com.zking.goods.entity;)
????????????????????????????????1.1 Users.java (用戶實體類)
package com.zking.goods.entity;
public class Users {
private int cid;
private String cname;
private String cpwd;
private int urole;
private float cmoney;
public Users() {
// TODO Auto-generated constructor stub
}
public Users(int cid, String cname, String cpwd, int urole, float cmoney) {
super();
this.cid = cid;
this.cname = cname;
this.cpwd = cpwd;
this.urole = urole;
this.cmoney = cmoney;
}
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCpwd() {
return cpwd;
}
public void setCpwd(String cpwd) {
this.cpwd = cpwd;
}
public int getUrole() {
return urole;
}
public void setUrole(int urole) {
this.urole = urole;
}
public float getCmoney() {
return cmoney;
}
public void setCmoney(float cmoney) {
this.cmoney = cmoney;
}
@Override
public String toString() {
return "Users [cid=" + cid + ", cname=" + cname + ", cpwd=" + cpwd + ", urole=" + urole + ", cmoney=" + cmoney
+ "]";
}
}
? ? ????????????????? ? 1.2?Goods.java? (商品實體類)
package com.zking.goods.entity;
public class Goods {
private int gid;
private String gname;
private float gprice;
private String ginfo;
private String gpath;
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(String gname, float gprice, String ginfo, String gpath) {
super();
this.gname = gname;
this.gprice = gprice;
this.ginfo = ginfo;
this.gpath = gpath;
}
public Goods(int gid, String gname, float gprice, String ginfo, String gpath) {
super();
this.gid = gid;
this.gname = gname;
this.gprice = gprice;
this.ginfo = ginfo;
this.gpath = gpath;
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getGname() {
return gname;
}
public void setGname(String gname) {
this.gname = gname;
}
public float getGprice() {
return gprice;
}
public void setGprice(float gprice) {
this.gprice = gprice;
}
public String getGinfo() {
return ginfo;
}
public void setGinfo(String ginfo) {
this.ginfo = ginfo;
}
public String getGpath() {
return gpath;
}
public void setGpath(String gpath) {
this.gpath = gpath;
}
@Override
public String toString() {
return "Goods [gid=" + gid + ", gname=" + gname + ", gprice=" + gprice + ", ginfo=" + ginfo + ", gpath=" + gpath
+ "]";
}
}
????????????????????????1.3 Cart.java (購物車實現(xiàn)類)
package com.zking.goods.entity;
public class Cart {
private Goods goods;//對象:包含商品所有屬性
private int ccount;//數(shù)量 單個商品的數(shù)量
private float ctotal;//單個商品的總價格
public Cart() {
// TODO Auto-generated constructor stub
}
public Cart(Goods goods, int ccount, float ctotal) {
super();
this.goods = goods;
this.ccount = ccount;
this.ctotal = ctotal;
}
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getCcount() {
return ccount;
}
public void setCcount(int ccount) {
this.ccount = ccount;
}
public float getCtotal() {
return ctotal;
}
//計算總價格
public void setCtotal() {
this.ctotal = this.getGoods().getGprice() * this.getCcount();
}
@Override
public String toString() {
return "Cart [goods=" + goods + ", ccount=" + ccount + ", ctotal=" + ctotal + "]";
}
}
????????????????數(shù)據(jù)庫訪問包utils (package com.zking.goods.utils;)
? ? ????????????????? ? 1.1 BaseDao.java 萬能增刪改查方法(簡易)實現(xiàn)類
package com.zking.goods.utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class BaseDao {
//protected 訪問控制符 受保護(hù)的
protected Connection conn =null;
protected PreparedStatement ps = null;
protected ResultSet rs = null;
/**
* 方法功能:通用增刪改方法
*/
public int executeUpdate(Connection conn,String sql,Object...x) {
int n = 0;
try {
// a.獲取數(shù)據(jù)庫連接
conn = DBHelper.getConn();
// b.sql傳入方法返回執(zhí)行對象
ps = conn.prepareStatement(sql);
//新增的sql語句 有占位符
if(null!=x) {
//拿到可變參數(shù)中的2個值
for(int i = 0;i<x.length;i++) {
//System.out.println(x[i]);
ps.setObject(i+1, x[i]);
}
}
n = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(conn, ps, null);
}
return n;
}
/**
* 方法功能:通用查詢 查詢所有 查詢一個 模糊查詢
*/
public ResultSet executeQuery(String sql,Object...x) {
try {
// a.獲取數(shù)據(jù)庫連接
conn = DBHelper.getConn();
// b.sql傳入方法返回執(zhí)行對象
ps = conn.prepareStatement(sql);
//新增的sql語句 有占位符
if(null!=x) {
//拿到可變參數(shù)中的2個值
for(int i = 0;i<x.length;i++) {
//System.out.println(x[i]);
ps.setObject(i+1, x[i]);
}
}
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}finally {
//不能關(guān)閉 通用的查詢base方法 不能實現(xiàn)數(shù)據(jù)庫關(guān)閉
//DBHelper.myClose(conn, ps, null);
}
return rs;
}
}
????????????????? ? ? ? 1.2 DBHelper.java 數(shù)據(jù)庫幫助類
package com.zking.goods.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 數(shù)據(jù)庫幫助類
*
* @author Administrator
*
*/
public class DBHelper {
private static final String URL = "jdbc:oracle:thin:@localhost:1521:orcl";
// 1.加載驅(qū)動 靜態(tài)代碼塊進(jìn)行封裝 優(yōu)先級別最高(靜態(tài)代碼塊>普通代碼塊>構(gòu)造函數(shù))
static {
try {
// OracleDriver
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}
// 2.建立數(shù)據(jù)庫連接
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(URL, "scott", "123");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String[] args) {
// System.out.println(DBHelper.getConn());
}
// 3.關(guān)閉服務(wù)
/**
* 方法功能:數(shù)據(jù)庫服務(wù)關(guān)閉
*
* @param conn
* 連接對象
* @param ps
* 執(zhí)行對象
* @param rs
* 結(jié)果集對象
*/
public static void myClose(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (null != conn && !conn.isClosed()) {
conn.close();
}
if (null != ps) {
ps.close();
}
if (null != rs) {
rs.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
????????業(yè)務(wù)邏輯層 BBL(biz|services):
????????? ? ? ? 1.biz包代碼 (package com.zking.goods.biz;)? ? ? ?IGoodsBiz.java?(商品方法定義接口)
package com.zking.goods.biz;
import java.util.List;
import com.zking.goods.entity.Goods;
public interface IGoodsBiz {
List<Goods> queryGoodsAll();
/**
* 根據(jù)商品編號獲取對應(yīng)的信息
*/
Goods getGoodsByCid(int nid);
}
????????????????2.biz包中的impl包代碼? (package com.zking.goods.biz.impl;)? ?GoodsBizImpl.java (商品方法實現(xiàn)類命名)
package com.zking.goods.biz.impl;
import java.util.List;
import com.zking.goods.biz.IGoodsBiz;
import com.zking.goods.dao.IGoodsDao;
import com.zking.goods.dao.impl.GoodsDaoImpl;
import com.zking.goods.entity.Goods;
public class GoodsBizImpl implements IGoodsBiz {
//實例化數(shù)據(jù)訪問層的dao
IGoodsDao igd = new GoodsDaoImpl();
@Override
public List<Goods> queryGoodsAll() {
return igd.queryGoodsAll();
}
@Override
public Goods getGoodsByCid(int cid) {
return igd.getGoodsByCid(cid);
}
}
????????數(shù)據(jù)訪問層 DAL? ?(dao):
????????????????1.dao包代碼(package com.zking.goods.dao;)? ? ? ?
????????????????????????1.1 IGoodsDao.java (商品方法定義接口)
package com.zking.goods.dao;
import java.util.List;
import com.zking.goods.entity.Goods;
public interface IGoodsDao {
List<Goods> queryGoodsAll();
/**
* 根據(jù)商品編號獲取對應(yīng)的信息
*/
Goods getGoodsByCid(int cid);
}
????????1.2.dao包中的impl包(package com.zking.goods.dao.impl;)? ? ? ? GoodsDaoImpl.java (商品方法實現(xiàn)類)文章來源:http://www.zghlxwxcb.cn/news/detail-492530.html
package com.zking.goods.dao.impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zking.goods.dao.IGoodsDao;
import com.zking.goods.entity.Goods;
import com.zking.goods.utils.BaseDao;
public class GoodsDaoImpl extends BaseDao implements IGoodsDao {
@Override
public List<Goods> queryGoodsAll() {
// 集合
List<Goods> list = new ArrayList<Goods>();
// 查看所有的sql語句
String sql = "select * from goods";
// 通過繼承的方式 來到通用查詢方法
ResultSet rs = this.executeQuery(sql);
try {
while (rs.next()) {
list.add(new Goods(rs.getInt(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5)));
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
// List<Goods> queryGoodsAll = new GoodsDaoImpl().queryGoodsAll();
// for (Goods goods : queryGoodsAll) {
//
// System.out.println(goods);
// }
Goods goods = new GoodsDaoImpl().getGoodsByCid(1);
System.out.println(goods);
}
@Override
public Goods getGoodsByCid(int gid) {
Goods goods = null;
// 查看所有的sql語句
String sql = "select * from goods where gid = "+gid;
// 通過繼承的方式 來到通用查詢方法
ResultSet rs = this.executeQuery(sql);
try {
if (rs.next()) {
goods = new Goods(rs.getInt(1), rs.getString(2), rs.getFloat(3), rs.getString(4), rs.getString(5));
}
} catch (Exception e) {
e.printStackTrace();
}
return goods;
}
}
????????當(dāng)以上這些功能都實現(xiàn)的時候,就可以進(jìn)行以下的界面操作以及界面處理的操作。文章來源地址http://www.zghlxwxcb.cn/news/detail-492530.html
????????第四部分:JSP 代碼?
????????????????1.index.jsp? ? ? ? 商品信息顯示頁面
<%@page import="com.zking.goods.entity.Goods"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.goods.biz.IGoodsBiz"%>
<%@page import="com.zking.goods.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.goods.entity.Users"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- 通過session默認(rèn)保存一個用戶 -->
<%
Users users = new Users(1,"zz","123",1,1000);
//存儲到session
session.setAttribute("users", users);
%>
<h3>zz購物商城首頁</h3>
<% Users u = (Users)session.getAttribute("users"); %>
<p>歡迎您!<%=u.getCname() %></p>
<table border ="1" width = "70%">
<tr>
<th>編號</th>
<th>名稱</th>
<th>價格</th>
<th>描述</th>
<th>圖片</th>
<th>操作</th>
</tr>
<%
//調(diào)用biz層顯示所有的數(shù)據(jù)
IGoodsBiz igb = new GoodsBizImpl();
//調(diào)用查詢所有的方法
List<Goods> listGoods = igb.queryGoodsAll();
for(Goods goods:listGoods){
%>
<tr>
<td><%=goods.getGid() %></td>
<td><%=goods.getGname() %></td>
<td><%=goods.getGprice() %></td>
<td><%=goods.getGinfo() %></td>
<td><img src = "<%=goods.getGpath()%>"/></td>
<td>
<button onclick="addCart(<%=goods.getGid()%>)">加入購物車</button>
</td>
</tr>
<%
}
%>
</table>
<script type="text/javascript">
//加入購物車的點擊事件
function addCart(cid){
//alert(cid)
location.href = "doShopping.jsp?cid="+cid;
}
</script>
</body>
</html>
????????????????2.doShopping.jsp? ? ? ? 購物車處理頁面?
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.goods.entity.Cart"%>
<%@page import="com.zking.goods.entity.Goods"%>
<%@page import="com.zking.goods.biz.impl.GoodsBizImpl"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 加入購物車的do處理頁面 -->
<!--
購物車流程:
1.Goods---顯示商品信息
2.Cart ---購物車中的信息
加入購物車 不是將Goods進(jìn)行存儲 而是存儲的是Cart實體
-->
<%
//將點擊的商品存儲到購物車容器中(session)
//設(shè)置編碼
request.setCharacterEncoding("utf-8");
//獲取商品編號
String id = request.getParameter("cid");
int cid = 0;
if(null!=id){
cid = Integer.valueOf(id);
}
//拿到cid 根據(jù)cid獲取商品的其它信息
Goods goods = new GoodsBizImpl().getGoodsByCid(cid);
//數(shù)量默認(rèn)一件
//總價格 商品的單價 * 數(shù)量(1件)
//將上面獲取的信息封裝到Cart實體中
Cart cart = new Cart();
cart.setGoods(goods);
cart.setCcount(1);
cart.setCtotal();
out.println(cart);
//加入購物車的版本1
//點擊一個就加入一個
/* session.setAttribute("cart", cart);
response.sendRedirect("cart.jsp"); */
//加入購物車的版本2
/*
List<Cart> listCarts = new ArrayList<Cart>();
listCarts.add(cart);
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp");
*/
//加入購物車的版本3
//將商品加入購物車之前,先要獲取購物車
//假設(shè) 假設(shè)購物車的標(biāo)記 listCarts
//1.獲取購物車
//session.getAttribute("listCarts"); 如果不存在 結(jié)果:null
/* List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
//List<Cart> listCarts = null;
//2.判斷 如果購物車為null 則創(chuàng)建一個
if(listCarts == null){
//創(chuàng)建一個購物車容器
listCarts = new ArrayList<Cart>();
}
listCarts.add(cart);
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp"); */
//問題:當(dāng)點擊同一件商品時,如果購物車中存在,不會修改數(shù)量,只會重新添加
//解決這個問題
//版本4
//1.獲取購物車
List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
//2.判斷非空
boolean flag = true;
if(null == listCarts){//說明用戶時第一次將商品加入購物車
//創(chuàng)建購物車
listCarts = new ArrayList<Cart>();
}else{//否則 購物車中存在商品
//遍歷所有的購物車中的商品 看該購物車中是否存在剛才點擊的cart實體封裝
for(Cart c : listCarts){
//判斷傳遞的cid與當(dāng)前l(fā)istCarts中的每一個cid進(jìn)行匹配
if(cid == c.getGoods().getGid()){//說明購物車中存在該商品
flag = false;
//修改數(shù)量
c.setCcount(c.getCcount()+1);
//修改總價格
c.setCtotal();
}
}
}
//判斷flag標(biāo)記
if(flag == true){//說明購物車中沒有該商品
listCarts.add(cart);
}
//重新保存購物車
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp");
%>
????????????????3.cart.jsp? ? ? ? 實現(xiàn)購物車界面
<%@page import="java.util.List"%>
<%@page import="com.zking.goods.entity.Cart"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>我的購物車</h2>
<a href = "index.jsp">返回首頁</a>
<hr/>
<table border ="1" width = "70%">
<tr>
<th>編號</th>
<th>名稱</th>
<th>價格</th>
<th>圖片</th>
<th>數(shù)量</th>
<th>總價格</th>
<th>操作</th>
</tr>
<%
//java代碼
//獲取購物車
//Cart cart = (Cart)session.getAttribute("cart");
List<Cart> listCarts = (List<Cart>)session.getAttribute("listCarts");
//定義一個頁碼
int pageIndex = 1;
//定義一個變量存儲每頁顯示的條數(shù)
int pageSize = 4;
//當(dāng)用戶點擊了下一頁 獲取到下一頁超鏈接上的參數(shù)
String pIndex= request.getParameter("pageIndex");
if(pIndex!=null){
pageIndex = Integer.valueOf(pIndex);
}
//定義一個變量存儲總記錄數(shù)
int pageCount = listCarts.size();
System.out.println("總記錄數(shù): "+pageCount);
//pageIndex = 1 0-3 0 1 2 3
//pageIndex = 2 4-7 4 5 6 7
int start = (pageIndex-1)*pageSize;
//sublist 參數(shù)end 沒有等于
int end = pageIndex*pageSize;
if(end > pageCount){
end = pageCount;
}
int pageMax = pageCount/pageSize;
if(pageCount%pageSize!=0){
pageMax++;
}
//調(diào)用偽分頁的方法 subList(start,end);
listCarts = listCarts.subList(start,end);
for(Cart cart : listCarts){
%>
<tr id = "<%=cart.getGoods().getGid()%>">
<td><%=cart.getGoods().getGid() %></td>
<td><%=cart.getGoods().getGname() %></td>
<td><%=cart.getGoods().getGprice() %></td>
<td><img src = "<%=cart.getGoods().getGpath() %>"/></td>
<td>
<button onclick="add('a',<%=cart.getGoods().getGid()%>)">-</button>
<input type = "text" style ="width:40px" value = "<%=cart.getCcount() %>"/>
<button onclick="add('b',<%=cart.getGoods().getGid()%>)">+</button>
</td>
<td><%=cart.getCtotal() %></td>
<td>
<button onclick="delCart(<%=cart.getGoods().getGid()%>)">刪除</button>
<button onclick="updateCart(<%=cart.getGoods().getGid()%>)">修改</button>
</td>
</tr>
<%
}
%>
</table>
<!-- 購物車頁面實現(xiàn)分頁 -->
<!-- 每一頁現(xiàn)實的商品條數(shù)4條 -->
<p>
[<%=pageIndex %>/<%=pageMax %>]
<a href = "cart.jsp?pageIndex=1">首頁</a>
<a href = "cart.jsp?pageIndex=<%=pageIndex-1<0?1:pageIndex-1%>">上一頁</a>
<a href = "cart.jsp?pageIndex=<%=pageIndex+1>pageMax?pageMax:pageIndex+1%>">下一頁</a>
<a href="cart.jsp?pageIndex=<%=pageMax%>">尾頁</a>
</p>
<script type="text/javascript">
function delCart(id){
if(window.confirm("您確定要刪除嗎?")){
location.href="doDel.jsp?gid="+id;
}
}
function add(type,id){
//根據(jù)參數(shù)id獲取tr標(biāo)簽
var tr = document.getElementById(id);
//console.log(tr)
//根據(jù)tr獲取數(shù)量
var ccount = tr.cells[4].children[1].value;
console.log(ccount);
if(type === 'a'){//減法
ccount--;
}else if(type==='b'){//加法
ccount++;
}
tr.cells[4].children[1].value = ccount;
}
function updateCart(id){
//根據(jù)id獲取到數(shù)量
var tr = document.getElementById(id);
var ccount = tr.cells[4].children[1].value;
//console.log(ccount);
location.href="doUpdate.jsp?gid="+id+"&ccount="+ccount;
}
</script>
</body>
</html>
????????????????4.doDel.jsp? ? ? ? 購物車刪除處理頁面
<%@page import="java.util.Iterator"%>
<%@page import="com.zking.goods.entity.Cart"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id = request.getParameter("gid");
int cid = 0;
if(null!=id){
cid = Integer.valueOf(id);
}
//獲取購物車
List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
System.out.println(listCarts);
/* for(int i = 0;i<listCarts.size();i++){
if(cid == listCarts.get(i).getGoods().getGid()){
listCarts.remove(i);
}
} */
//迭代器的方式進(jìn)行刪除
Iterator<Cart> its = listCarts.iterator();
while(its.hasNext()){
Cart cc = its.next();
//its.remove();
if(cc.getGoods().getGid() == cid){
its.remove();
}
}
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp");
%>
????????????????5.doUpdate.jsp (修改購物車信息的處理頁面)
<%@page import="com.zking.goods.entity.Cart"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String id1 = request.getParameter("gid");
String id2 = request.getParameter("ccount");
int gid = 0;
if(id1!=null){
gid = Integer.valueOf(id1);
}
int ccount = 0;
if(id2!=null){
ccount = Integer.valueOf(id2);
}
//獲取購物車
List<Cart> listCarts = (List<Cart> )session.getAttribute("listCarts");
for(Cart c:listCarts){
if(c.getGoods().getGid() == gid){
c.setCcount(ccount);
c.setCtotal();
}
}
session.setAttribute("listCarts", listCarts);
response.sendRedirect("cart.jsp");
%>
到了這里,關(guān)于JavaWeb購物車項目 思路&拓展&綜合提升的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!