国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

JavaWeb購物車項目 思路&拓展&綜合提升

這篇具有很好參考價值的文章主要介紹了JavaWeb購物車項目 思路&拓展&綜合提升。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

?一、實現(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

  • 前臺
  1. 包括用戶注冊,登錄 ?

Html +js+jsp

考慮自動登錄(cookie實現(xiàn) ?登錄頁面)

  1. 進(jìn)入購物頁面購物(數(shù)據(jù)均來自數(shù)據(jù)庫),商品展示頁面shop.jsp顯示如下(大概顯示效果如下,為了界面美觀,可以加入相應(yīng)的商品圖片 )(該頁面相當(dāng)于游客頁面)JavaWeb購物車項目 思路&拓展&綜合提升

?1.點擊”加入購物車?的同時,要將數(shù)據(jù)(訂單項的集合)保存到session中,再轉(zhuǎn)入下圖購物車頁面cart.jsp(必須登錄才能進(jìn))(大概效果如下)[從session中取出訂單項集合]JavaWeb購物車項目 思路&拓展&綜合提升

    1. 其中商品數(shù)量可以在修改數(shù)量以后點擊【修改】按鈕進(jìn)行修改
    2. 刪除按鈕則可以刪除該購買商品(必須有提示)
    3. 點擊繼續(xù)購物后應(yīng)該轉(zhuǎn)入購物頁面繼續(xù)購物
    4. 點擊結(jié)算后應(yīng)該轉(zhuǎn)入結(jié)賬頁面,提示用戶當(dāng)前購物車總金額,并清空購買信息。
    5. 訂單項組成:商品基本信息Goods??數(shù)量num ?單個商品的總價sumprice
  • 后臺
  1. 包括對商品的管理(考慮刪除的真正含義)以及 分頁 下架 ?狀態(tài)列0 1
  2. 包括對用戶的管理(考慮刪除的真正含義)以及 分頁
  3. **思考訂單&訂單項(一個訂單可以包含多個訂單項)的概念
  4. 解決兩個問題:訂單項覆蓋問題 加同一個商品重復(fù)問題

涉及的技術(shù)點:

?JDBC

?Entity:

??--Goods

??--User

?

Session:購物車(增刪改查)

二、JSP 頁面實現(xiàn)(臨時性購物車項目)

????????第一部分:images(圖片)

JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升JavaWeb購物車項目 思路&拓展&綜合提升

????????第二部分: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)類)

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • JavaWeb購物系統(tǒng)(五)購物車模塊的實現(xiàn)

    JavaWeb購物系統(tǒng)(五)購物車模塊的實現(xiàn)

    未添加商品效果圖 添加商品之后的效果圖 添加商品 購物車中商品的數(shù)量增加、減少、通過鍵盤輸入改變數(shù)量 清空購物車 計算購物車商品的總價格 我們的購物車采用的是在服務(wù)端,即:使用session來存儲。這樣做的 缺點 :無法永久存儲,當(dāng)服務(wù)端關(guān)閉的時候,會銷毀。 優(yōu)點

    2024年02月07日
    瀏覽(28)
  • javaweb實現(xiàn)購物車功能

    javaweb實現(xiàn)購物車功能

    使用到的相關(guān)知識(部分知識點在文章中簡單涉及到): ? ? ? ?html? cs? javascript? jsp??servlet? ?ajax? jQuery? Mysql? MyBatis(持久層框架,用來連接數(shù)據(jù)庫,這里可以使用jdbc進(jìn)行數(shù)據(jù)庫的連接)? 功能使用MVC設(shè)計模式,以及三層架構(gòu)思想 注: 本篇使用 Session 對購物車進(jìn)行存儲,具體參

    2023年04月21日
    瀏覽(24)
  • JavaWeb購物系統(tǒng)(六)購物車訂單模塊的實現(xiàn)

    JavaWeb購物系統(tǒng)(六)購物車訂單模塊的實現(xiàn)

    有訂單時的效果圖 無訂單時的效果圖 訂單詳情頁 生成訂單 訂單頁的展示 查看訂單詳情 和購物車同樣的,首先得知道我們的訂單對應(yīng)的哪個實體對象。一個用戶可能有多條訂單記錄,一個訂單里邊可以包含多個 商品(也可以理解為多個購物項) 。理清這個邏輯之后,我們

    2024年02月05日
    瀏覽(20)
  • Javaweb MVC+dao實現(xiàn)簡單購物車(實驗)

    Javaweb MVC+dao實現(xiàn)簡單購物車(實驗)

    本篇文章給大家分享一下簡單購物車的實現(xiàn)。含商品圖片、清空購物車、指定數(shù)量增加、結(jié)算。 代碼放網(wǎng)盤里了,需要自取。注意是登錄注冊+購物車的,這兩個實際運行是分開的,登錄注冊需運行user_login.jsp,購物車需運行cart.jsp。提取碼:poem 首先給出截圖,需要建這些文

    2024年02月03日
    瀏覽(19)
  • Vue項目商品購物車前端本地緩存邏輯(適用H5/ipad/PC端)——前端實現(xiàn)購物車刪除商品、購物車增減數(shù)量,清空購物車功能

    Vue項目商品購物車前端本地緩存邏輯(適用H5/ipad/PC端)——前端實現(xiàn)購物車刪除商品、購物車增減數(shù)量,清空購物車功能

    Vue3 + Vite + Ts開源后臺管理系統(tǒng)模板 基于ElementUi或AntdUI再次封裝基礎(chǔ)組件文檔 基于Element-plus再次封裝基礎(chǔ)組件文檔(vue3+ts)

    2024年02月12日
    瀏覽(31)
  • Vue項目(購物車)

    目錄 購物車效果展示: 購物車代碼: 購物車效果展示: 此項目添加、修改、刪除數(shù)據(jù)的地方都寫了瀏覽器都會把它存儲起來 下次運行項目時會把瀏覽器數(shù)據(jù)拿出來并在頁面展示 Video_20230816145047 購物車代碼: 復(fù)制完代碼,需改下script中引入的vue文件地址; 可直接使用

    2024年02月12日
    瀏覽(24)
  • 【javaweb+springboot】旅游網(wǎng)頁面設(shè)計(主購物車功能)——前后端分離+服務(wù)端客戶端增刪改查(完整代碼+文檔)

    【javaweb+springboot】旅游網(wǎng)頁面設(shè)計(主購物車功能)——前后端分離+服務(wù)端客戶端增刪改查(完整代碼+文檔)

    一、項目背景 由于疫情原因,張家界旅游業(yè)受到很大的影響,為了促進(jìn)旅游業(yè)的發(fā)展,吸引更多游客來到張家界旅游,幫助游客更好地了解張家界,創(chuàng)建張家界旅游網(wǎng),推進(jìn)旅游發(fā)展大會的開展,展示當(dāng)?shù)仫L(fēng)土人情。景區(qū)推薦和酒店預(yù)定使得游客出行更加的方便,通過游客留

    2024年02月09日
    瀏覽(20)
  • 微信小程序 - 商城項目 - 購物車

    微信小程序 - 商城項目 - 購物車

    ? 引入 WeUI:

    2023年04月22日
    瀏覽(21)
  • SpringBoot項目--電腦商城【加入購物車】

    1.使用use命令先選中store數(shù)據(jù)庫 2.在store數(shù)據(jù)庫中創(chuàng)建t_cart用戶數(shù)據(jù)表 在entity包下創(chuàng)建購物車的Cart實體類并使其繼承BaseEntity 1.向購物車表中插入商品數(shù)據(jù)的SQL語句 2.如果當(dāng)前商品已經(jīng)在購物車存在,則直接更新商品即可 3.在插入或者更新具體執(zhí)行哪個語句,取決于數(shù)據(jù)庫中是否

    2024年02月09日
    瀏覽(22)
  • 【java】【項目實戰(zhàn)】[外賣八]產(chǎn)品展示、購物車、下單開發(fā)

    【java】【項目實戰(zhàn)】[外賣八]產(chǎn)品展示、購物車、下單開發(fā)

    目錄 一、導(dǎo)入用戶地址簿相關(guān)功能代碼 1 需求分析 2 數(shù)據(jù)模型 3 導(dǎo)入功能代碼 ?3.1 實體AddressBook? 3.2 Mapper接口AddressBookMapper? ?3.3 業(yè)務(wù)接口層AddressBookService 3.4 業(yè)務(wù)接口實現(xiàn)類AddressBookServiceImpl 3.5 控制層AddressBookController 4 功能測試 二、菜品展示 1 需求分析 2 代碼開發(fā) ?2.1 m

    2024年02月11日
    瀏覽(25)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包