本文實例為大家分享了java實現購物車功能的具體代碼,供大家參考,具體內容如下
1 需要實現
1、實現淘淘商城的購物車功能
2 購物車功能
2.1 功能說明
1、商品加入購物車時,不是必須要求登錄。京東不需要登錄,淘寶需要登錄。各有好處。
2、計算購物車中商品的總價。當商品數量發生變化時需要重新計算。
3、用戶可以刪除購物車中的商品。
4、用戶下單后,刪除購物車的功能。
購物車在用戶不登陸的情況下也可以使用購物車。需要把購物車的商品信息寫入cookie中。所有對購物車的操作都是操作cookie。有效 的降低數據庫的壓力。
缺點:換一臺電腦后購物車的商品不能同步。
實現的工程:taotao-protal中實現購物車功能。只需要調用商品信息的服務,除此之外不需要和其他系統交互。
2.2 功能分析
1、在用戶不登陸的清空下也可以使用購物車,那么就需要把購物車信息放入cookie中。
2、可以把商品信息,存放到pojo中,然后序列化成json存入cookie中。
3、取商品信息可以從cookie中把json數據取出來,然后轉換成java對象即可。
4、此功能只需要操作cookie不需要數據庫的支持,所以只需要在taotao-portal中實現即可。
5、購物車分有四種動作
a) 添加商品
b) 修改商品數量
c) 刪除購物車中的商品
d) 展示購物車商品列表
2.3 添加購物車商品
在商品詳情頁面點擊“加入購物車”按鈕提交一個請求吧商品id傳遞給controller,controller接收id,controller調用service根據商品id查詢商品基本信息。把商品寫入cookie中,加入cookie之前先從cookie中把購物車的商品取出來判斷當前購物車商品列表中是否有此商品,如果有數量加一,如果沒有添加一個商品,數量為1。展示給用戶購物車列表。
2.3.1 service
功能:接收一個商品id,數量(默認為1),根據商品id查詢商品信息。調用taotao-rest的服務。把商品添加到購物車,先把購物車商品列表取出來,判斷列表中是否有此商品,如果有就增加數量就可以了。如果沒有把此商品添加到商品列表。返回添加成功taotaoresult。
功能:
1、接收controller傳遞過來的商品id,根據商品id查詢商品信息。
2、從cookie中取出購物車信息,轉換成商品pojo列表。
3、把商品信息添加到商品列表中。
參數:
1、商品id
2、request
3、response
返回值:
taotaoresult
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
@service public class cartserviceimpl implements cartservice { //服務url @value ( "${rest_base_url}" ) private string rest_base_url; //商品服務url @value ( "${items_item_url}" ) private string items_item_url; //cookie中購物車商品對應的key @value ( "${cart_items_list_key}" ) private string cart_items_list_key; //購物車cookie生存期 @value ( "${cart_items_expire_time}" ) private integer cart_items_expire_time; /** * 添加購物車商品 * <p>title: additem</p> * <p>description: </p> * @param itemid * @param request * @param response * @return * @see com.taotao.portal.service.cartservice#additem(java.lang.long, javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse) */ @override public taotaoresult additem( long itemid, httpservletrequest request, httpservletresponse response) { //根據商品id查詢商品信息 item item = getitembyid(itemid); if (item == null ) return taotaoresult.build( 400 , "未查詢到該商品信息" ); //取cookie中購物車商品列表 list<item> cartitems = getitemlistfromcookie(request); //判斷該商品是否存在于購物車中 boolean itemexists = false ; for (item i : cartitems) { if (i.getid().longvalue() == itemid.longvalue()) { //購物車中有此商品 i.setnum(i.getnum() + 1 ); itemexists = true ; break ; } } //如果商品不存在于購物車則向購物車商品列表中添加一個商品 if (! itemexists) { //設置數量為1 item.setnum( 1 ); //把商品添加到購物車 cartitems.add(item); } //把購物車信息寫入cookie中 cookieutils.setcookie(request, response, cart_items_list_key, jsonutils.objecttojson(cartitems), cart_items_expire_time, true ); return taotaoresult.ok(cartitems); } private item getitembyid( long itemid) { //根據商品id查詢商品信息 string resultstr = httpclientutil.doget(rest_base_url + items_item_url + itemid); //轉換成taotaoresult taotaoresult result = taotaoresult.formattopojo(resultstr, item. class ); //取商品信息 item item = null ; if (result.getstatus() == 200 ) { item = (item) result.getdata(); } return item; } private list<item> getitemlistfromcookie(httpservletrequest request) { //取cookie中購物車商品列表 string cartitemsstr = cookieutils.getcookievalue(request, cart_items_list_key, true ); //如果不為空那么就轉換成java對象 list<item> cartitems = null ; if (!stringutils.isblank(cartitemsstr)) { cartitems = jsonutils.jsontolist(cartitemsstr, item. class ); } else { cartitems = new arraylist<>(); } return cartitems; } } |
2.3.2 商品pojo
商品pojo需要使用taotao-portal中的item。此pojo在反序列化時會拋異常。需要做如下修改:
2.3.3 controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@controller @requestmapping ( "/cart" ) public class cartcontroller { @autowired private cartservice cartservice; @requestmapping ( "/add/{itemid}" ) public string additem( @pathvariable long itemid, httpservletrequest request, httpservletresponse response, model model) { //添加商品信息 taotaoresult result = cartservice.additem(itemid, request, response); //錯誤信息 if (result.getstatus() != 200 ) { model.addattribute( "message" , result.getmsg()); return "error/exception" ; } //把購物車中的商品傳遞給頁面 model.addattribute( "cartlist" , result.getdata()); return "cart" ; } } |
用戶點擊“我的購物車”展示購物車信息
2.4 展示購物車商品
2.4.1 service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 取購物車列表 * <p> * title: getcartitemslist * </p> * <p> * description: * </p> * * @return * @see com.taotao.portal.service.cartservice#getcartitemslist() */ @override public list<item> getcartitemslist(httpservletrequest request) { // 從cookie中取商品列表 list<item> itemslist = getitemlistfromcookie(request); return itemslist; } |
2.4.2 controller
1
2
3
4
5
6
7
|
@requestmapping ( "/cart" ) public string showcart(httpservletrequest request, model mode) { //取購物車信息 list<item> list = cartservice.getcartitemslist(request); mode.addattribute( "cartlist" , list); return "cart" ; } |
2.5 修改商品數量
當點擊購物車商品的“+”、“-”號時增加或減少商品數量。減少商品數量時,如果數量為“1”則不繼續減少。
2.5.1 service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/** * 修改指定商品的數量 * <p>title: changeitemnum</p> * <p>description: </p> * @param itemid * @param num * @param request * @param response * @return * @see com.taotao.portal.service.cartservice#changeitemnum(long, int, javax.servlet.http.httpservletrequest, javax.servlet.http.httpservletresponse) */ @override public taotaoresult changeitemnum( long itemid, int num, httpservletrequest request, httpservletresponse response) { //從cookie中取商品列表 list<item> list = getitemlistfromcookie(request); //從商品列表中找到要修改數量的商品 for (item item : list) { if (item.getid() == itemid) { //找到商品,修改數量 item.setnum(num); break ; } } //把商品信息寫入cookie cookieutils.setcookie(request, response, cart_items_list_key, jsonutils.objecttojson(list), cart_items_expire_time, true ); return taotaoresult.ok(); } |
2.5.2 controller
1
2
3
4
5
6
7
|
@requestmapping ( "/update/num/{itemid}/{num}" ) @responsebody public taotaoresult updatenumbyid( @pathvariable long itemid, @pathvariable integer num, httpservletrequest request, httpservletresponse response) { taotaoresult result = cartservice.changeitemnum(itemid, num, request, response); return result; } |
2.6 刪除購物車商品
2.6.1 service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/** * 刪除購物車中的商品 * <p> * title: deleteitem * </p> * <p> * description: * </p> * * @param itemid * @param request * @param response * @return * @see com.taotao.portal.service.cartservice#deleteitem(java.lang.long, * javax.servlet.http.httpservletrequest, * javax.servlet.http.httpservletresponse) */ @override public list<item> deleteitem( long itemid, httpservletrequest request, httpservletresponse response) { list<item> itemslist = getcartitemslist(request); // 找到購物車中的商品,并刪除之 for (item item : itemslist) { if (item.getid().longvalue() == itemid.longvalue()) { itemslist.remove(item); break ; } } // 更新cookie中的購物車數據 cookieutils.setcookie(request, response, cart_items_list_key, jsonutils.objecttojson(itemslist), cart_items_expire_time, true ); return itemslist; } |
2.6.2 controller
1
2
3
4
5
6
7
8
|
@requestmapping ( "/delete/{itemid}" ) public string deleteitembyid( @pathvariable long itemid, httpservletrequest request, httpservletresponse response, model model) { list<item> list = cartservice.deleteitem(itemid, request, response); model.addattribute( "cartlist" , list); return "cart" ; } |
購物車存在的問題
1、更換設備購物車商品不能同步
a) 不能把購物車商品保存到數據庫
b) 要求用戶登錄才能同步信息
c) 可以把購物車信息保存到redis中,key就是用戶,value就是購物車列表
d) 購物車商品合并的問題。
2、提交訂單后購物車商品需要清空。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xxssyyyyssxx/article/details/72876247