




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、目標:l 掌握購物車的信息如何存儲;l 掌握購物車常用功能的實現(xiàn)。主要內(nèi)容:l 首先分析用戶上網(wǎng)購物的一般過程;l 介紹采用什么樣的數(shù)據(jù)結(jié)構(gòu)存儲購物信息;l
2、0; 編寫購物界面;l 完成向購物車添加物品的功能。1、 用戶上網(wǎng)購物的一般過程在瀏覽物品的過程中如果對某件物品感興趣,會添加到購物車(購物籃)中,隨時可以查看購物車中的信息,如果不想要某件物品的話,可以刪除,或者修改某種物品的數(shù)量,或者整個清空購物車,可以繼續(xù)選擇物品向購物車中添加。最后用戶可以購物這些物品,經(jīng)過輸入個人的送貨地址信息和設(shè)定交易方式之后,可以生成訂單。網(wǎng)站的管理員可以對訂單進行管理
3、。本實例模擬這個過程,但是進行了簡化:只能在物品列表中選擇物品向購物車中添加。確定購買后,不需要設(shè)置交易方式以及付款等。實際處理過程,可以使用我們前面介紹的功能完成。2、 購物車信息組織因為在用戶訪問網(wǎng)站的整個過程中都可以訪問購物車信息,所以購物車對象應(yīng)該存放在session中。因為用戶購買的物品的種類和數(shù)量都不確定,所以需要使用一個合適的數(shù)據(jù)結(jié)構(gòu)存儲,我們選擇ArrayList。每一種物品都涉及數(shù)量,需要進行封裝,把物品和數(shù)量封裝成購物項,使用Item,每個Item對應(yīng)一種物品以及該種物品的數(shù)量。需要編寫物品類表示物品的基本信息。參考代碼如下:2.1 物品類
4、 該類中包含兩個與分頁顯示相關(guān)的方法。其中用到的DBBean是前面介紹的。package javabean;public class Goods private String goodsid; private String goodsname; private float price; / 物品編號 public void setGoodsid(String goodsid)
5、0; this.goodsid = goodsid; public String getGoodsid() return goodsid; / 物品名稱 public void setGoodsname(String goodsname) this.goodsname = good
6、sname; public String getGoodsname() return goodsname; / 物品價格 public void setPrice(float price) this.price = price; public
7、float getPrice() return price; public ArrayList getGoodsByPage(int pageNo) int number = 10;
8、160; / 每一頁顯示的記錄數(shù) int begin = (pageNo * number) - 9; int end = pageNo * number; &
9、#160; int index = 1; DBBean db = new DBBean();
10、160; / 要返回的結(jié)果對象 ArrayList goods = new ArrayList();
11、; String sql = "select * from goods" ResultSet rs; try
12、160; rs = db.executeQuery(sql,null); while (rs.next()
13、 / 在begin之前的記錄是不顯示的 if (index < begin) &
14、#160; index+;
15、; continue;
16、 / 在end之后的記錄也不顯示
17、60; if (index > end) break;
18、 index+; String go
19、odsid = rs.getString(1); String goodsname = rs.getString(2);
20、 float price = rs.getFloat(3); Goods g = new Goods();&
21、#160; g.setGoodsid(goodsid);
22、160; g.setGoodsname(goodsname); g.setPrice(price);
23、0; goods.add(g); &
24、#160; catch(Exception e) e.printStackTrace(); finally &
25、#160; db.close(); return goods;
26、0; public Goods findGoodsById(String goodsid) try /
27、;編寫查詢數(shù)據(jù)庫信息的SQL語句 String sql = "select * from goods where goodsid=?"
28、 DBBean db = new DBBean(); ArrayList params = new ArrayList(); par
29、ams.add(goodsid); ResultSet rs = db.executeQuery(sql,params); if(
30、rs.next() /String goodsid =rs.getString(1);
31、0; String goodsname = rs.getString(2); float price = rs.getFloat(3); Go
32、ods temp = new Goods(); temp.setGoodsid(goodsid); temp.setGoodsname(goodsname);
33、; temp.setPrice(price); db.close(); &
34、#160; return temp; else return null;
35、; catch (Exception e) return null;
36、0; public int getPageCount() try / 編寫查詢數(shù)據(jù)庫信息的SQL語句
37、160; String sql = "select count(*) from goods"
38、60; DBBean db = new DBBean(); ResultSet rs=db.executeQuery(sql,null);
39、60; int number=0; if(rs.next()
40、 number = rs.getInt(1); db.close();
41、 return (number - 1) / 10 + 1;
42、 catch (Exception e) return 0; 2.2 Item類
43、package javabean;/ 購物項public class Item private Goods goods; private int quantity; public Item(Goods d,int quantity) this.goods = d; this.quantity = quantity; public
44、void setGoods(Goods goods) this.goods = goods; public Goods getGoods() return goods; public void setQuantity(int quantity) this.quanti
45、ty = quantity; public int getQuantity() return quantity; 3、 物品信息顯示功能采用MVC模式,考慮視圖部分,不需要輸入界面,只需要顯示信息的界面。模型部分,在前面的代碼中已經(jīng)實現(xiàn)??刂破鞑糠郑枰帉慓etAllGoods.java。參考代碼分別如下:3.1 界面代碼文件名:goodslist.jsp<% page contentType="
46、text/html;charset=gb2312"%><c:if test="$pageNo!=1"> <a href="getAllGoods?pageNo=1">第一頁</a> <a href="getAllGoods?pageNo=$pageNo-1">上一頁</a></c:if><c:if t
47、est="$pageNo!=pageCounter"> <a href="getAllGoods?pageNo=$pageNo+1">下一頁</a> <a href="getAllGoods?pageNo=$pageCounter">最后一頁</a></c:if><br><table width="2
48、00" border="1" height="56"> <tbody> <tr>
49、0; <td> 物品編號 &
50、#160; </td> <td>
51、160; 物品名稱 </td> <td>
52、; 物品價格 </td>&
53、#160; </tr> <c:forEach var="g" items="$goods">
54、 <tr> <td>
55、160; $g.goodsid
56、60; </td> <td>
57、0; $g.goodsname </td>
58、60; <td>
59、0; $g.price </td> &
60、#160; <td>
61、160; <a href="addToCart?goodsid=$g.goodsid">添加到購物車</a> </td>
62、160; </tr> </c:forEach> </tbody></table>3.2 控制器代package servlet;import javabea
63、n.*;public class GetAllGoods extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException /response.setContentType("text/html;charset=gb2312"
64、); /PrintWriter out = response.getWriter(); / 第一步:獲取用戶的輸入信息 String pageNo=request.getParameter("pageNo"); int iPageNo
65、=1; if(pageNo!=null) iPageNo = Integer.parseInt(pageNo); / 第二步:調(diào)用JavaBean Goods g = new G
66、oods(); ArrayList goods=null; goods = g.getGoodsByPage(iPageNo); int pageCount=g.getPageCount(); / out.println("記錄數(shù):"+users.size();
67、60; / out.println("當前頁碼:"+iPageNo); / out.println("總頁碼:"+pageCount); / 第三步:傳值 request.setAttribute("goods",goods); &
68、#160; request.setAttribute("pageNo",new Integer(iPageNo); request.setAttribute("pageCounter",new Integer(pageCount); / 第四步:選擇一個界面對用戶進行響應(yīng) String forward="goodslist.jsp&q
69、uot; RequestDispatcher rd = request.getRequestDispatcher(forward); rd.forward(request,response); public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException
70、,ServletException doGet(request,response); 4、 顯示購物車中信息該功能直接從session中獲取購物車信息,所以不需要控制器和模型部分,只需要編寫顯示購物車信息的JSP文件即可,文件名為cart.jsp,參考代碼如下:<% page contentType="text/html;charset=gb2312"%>購物車中的信息<br><table border=1>
71、 <tr> <td>物品編號</td> <td>物品名稱</td> <td>價格</td> <td>數(shù)量</td> </tr><c:forEach var="item" item
72、s="$cart"> <tr> <td>$item.quantity</td></c:forEach></table>5、 向購物車中添加物品采用MVC模式。首先考慮輸入和輸出,添加物品的輸入就是物品信息列表界面,輸出應(yīng)該是添加后的購物車信息界面(也可以重新回到物品信息界面),這兩個界面都不需要編寫??紤]模型部分,需要編寫購物車管理JavaBean,完成處理??紤]控制器部分,需要獲取用戶選擇的物品,然后添加到調(diào)用購物
73、車管理Bean,完成添加。下面是參考代碼。5.1 模型部分文件名CartManager.javapackage javabean;public class CartManager / 表示購物車 private ArrayList cart; public void setCart(ArrayList cart) this.cart = cart; public ArrayLis
74、t getCart() return cart; / 添加的物品數(shù)量 public ArrayList addToCart(Goods g,int quantity) if(cart=null) / 實例化購物車對
75、象 cart=new ArrayList(); / 添加到購物車 Item item = new Item(g,quantity); cart.add(item);
76、60; else / 轉(zhuǎn)換成數(shù)組 Object items = cart.toArray(); boolean find=false; / 表示是否查找到&
77、#160; for(int i=0;i<items.length;i+) Item temp = (Item)itemsi;
78、 / 判斷購物車中是否存在要添加的物品 if(temp.getGoods().getGoodsid().equals(g.getGoodsid()
79、; temp.setQuantity(temp.getQuantity()+quantity); find=true; &
80、#160; break; if(!find)
81、; / 添加到購物車 Item item = new Item(g,quantity); cart.add(item);
82、; return cart; 5.2 控制器部分文件名:AddToCart.javapackage servlet;import javabean.*;public class AddToCart extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException /
83、60; response.setContentType("text/html;charset=gb2312");/ PrintWriter out = response.getWriter(); try / 得到要添加的物品的編號 String goodsid = request.
84、getParameter("goodsid"); / 創(chuàng)建JavaBean對象 CartManager cartManager=new CartManager(); / 得到session對象 HttpSession session = request.getSession(true);
85、0; / 得到購物車對象 ArrayList cart = (ArrayList)session.getAttribute("cart"); / 用cart初始化cartManager cartManager.setCart(cart); / 構(gòu)造物品對象
86、60; Goods g = new Goods(); g = g.findGoodsById(goodsid); / out.println(g.getGoodsid(); cartManager.addToCart(g,1); / 先把購物車重新存到session sessi
87、on.setAttribute("cart",cartManager.getCart(); catch(Exception e) / out.println(e.toString(); response.sendRedirect("cart.jsp"); &
88、#160; public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException doGet(request,response); 6、 Servlet的配置web.xml文件內(nèi)容如下:<?xml version="1.0" encodin
89、g="UTF-8"?><servlet> <servlet-name>getAllGoods</servlet-name> <servlet-class>servlet.GetAllGoods</servlet-class></servlet><servlet-mapping>
90、0; <servlet-name>getAllGoods</servlet-name> <url-pattern>/getAllGoods</url-pattern></servlet-mapping><servlet> <servlet-name>addToCart</servlet
91、-name> <servlet-class>servlet.AddToCart</servlet-class></servlet><servlet-mapping> <servlet-name>addToCart</servlet-name> <url-patte
92、rn>/addToCart</url-pattern></servlet-mapping></web-app>7、 運行編譯所有文件,然后先訪問getAllGoods Servlet,然后在物品信息界面上選擇物品添加到購物車,之后就可以看到購物車中的信息了。主要內(nèi)容:l 完成購物車的其他基本功能;l 生成訂單;l 小結(jié)1、購物車的其它功能對購物車的物品數(shù)量修改和物品刪除功能是兩外兩個基本功能。實現(xiàn)過程與添加工程比較類似,這里只給出參考代碼:1.1 模型部分文件名:C
93、artManager.java(在上一講的基礎(chǔ)上修改,紅色部分為添加的內(nèi)容)package javabean;public class CartManager / 表示購物車 private ArrayList cart; public void setCart(ArrayList cart) this.cart = cart; public ArrayList getCart(
94、) return cart; / 添加的物品數(shù)量 public ArrayList addToCart(Goods g,int quantity) if(cart=null) / 實例化購物車對象
95、60; cart=new ArrayList(); / 添加到購物車 Item item = new Item(g,quantity); cart.add(item);
96、 else / 轉(zhuǎn)換成數(shù)組 Object items = cart.toArray(); boolean find=false; / 表示是否查找到
97、; for(int i=0;i<items.length;i+) Item temp = (Item)itemsi;
98、0; / 判斷購物車中是否存在要添加的物品 if(temp.getGoods().getGoodsid().equals(g.getGoodsid()
99、60; temp.setQuantity(temp.getQuantity()+quantity); find=true; brea
100、k; if(!find) /&
101、#160;添加到購物車 Item item = new Item(g,quantity); cart.add(item); re
102、turn cart; public void delete(String goodsid) / 轉(zhuǎn)換成Iterator對象 Iterator i = cart.iterator(); while(i.hasNext()
103、60; / 得到一個購物項 Item temp = (Item)i.next(); if(temp.getGoods().getGoodsid().equals(goodsid) &
104、#160; cart.remove(temp); break; public void update(String goodsid,int quantity) &
105、#160; Iterator i = cart.iterator(); while(i.hasNext() / 得到一個購物項 Item temp = (Item)i.next();
106、; if(temp.getGoods().getGoodsid().equals(goodsid) temp.setQuantity(quantity); break;
107、; 1.2 修改視圖部分文件名:cart.jsp(在上一講的基礎(chǔ)上修改,紅色部分為添加的內(nèi)容)<% page contentType="text/html;charset=gb2312"%>購物車中的信息<br><table border=1> <
108、tr> <td>物品編號</td> <td>物品名稱</td> <td>價格</td> <td>數(shù)量</td> </tr><c:forEach var="item" items="$ca
109、rt"> <tr><form action="processCart"> <td><input type="text" name="quantity" value="$item.quantity"></td> <td><input type="submit" n
110、ame="action" value="修改"></td> <td><input type="submit" name="action" value="刪除"></td></form> </tr></c:forEach></table>1.3 控制器刪除和修改功能使用相同的控制器,會根據(jù)提交按鈕的值確定要完成的功
111、能,參考代碼如下:文件名:ProcessCart.javapackage servlet;import javabean.*;public class ProcessCart extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
112、60; /response.setContentType("text/html;charset=gb2312"); /PrintWriter out = response.getWriter(); try
113、160; / request.setCharacterEncoding("gb2312"); / 得到要添加的物品的編號 String goodsid = request.getParameter("
114、goodsid"); / 得到執(zhí)行命令:刪除還是修改 String action = request.getParameter("action");
115、160; action=new String(action.getBytes("8859_1"); /out.println(action); String quantity=null;
116、; if(action.equals("修改") quantity = request.getParameter("quantity"); / 創(chuàng)建JavaBean對象 Ca
117、rtManager cartManager=new CartManager(); / 得到session對象 HttpSession session = request.getSession(true); / 得到購物車對象 ArrayList cart = (ArrayList)session.getAttribute("cart"); / 用cart初始化cartManager cartManager.setCart(cart);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 行政效能評估指標構(gòu)建試題及答案
- 2025年貨物購銷合同范本
- 行政管理服務(wù)優(yōu)化途徑與實踐試題及答案
- 老年義診政策解讀
- 2025停車場租賃合同范本
- 行政管理視角下的試題及答案總結(jié)
- 2025建筑工程混凝土采購合同
- 行政管理實務(wù)中的法律意識試題及答案
- 2025年梅閣村蓮花山地塊及地上相關(guān)資產(chǎn)租賃合同
- 2025企業(yè)單位員工勞動合同模板「版」
- 《融資融券對企業(yè)創(chuàng)新的影響實證研究》11000字【論文】
- 預(yù)防肺癌健康教育
- 2025中衛(wèi)輔警考試題庫
- 湖北省武漢市2025屆高三下學期二月調(diào)研考試數(shù)學試卷
- 漢語語氣詞的語用功能分析論文
- 光伏材料與器件-深度研究
- 高考英語閱讀理解題干、選項及近五年高頻詞匯
- 廣東省華附、省實、廣雅、深中2025屆高三四校聯(lián)考語文試題與答案
- 骨科專業(yè)培訓(xùn)計劃及總結(jié)
- 2025年河北省職業(yè)院校技能大賽工業(yè)互聯(lián)網(wǎng)集成應(yīng)用參考試題庫(含答案)
- 鋼結(jié)構(gòu)鋼筋大棚施工方案
評論
0/150
提交評論