之前沒有接觸過購物車的東東,也不知道購物車應該怎么做,所以在查詢了很多資料,總結一下購物車的功能實現。
查詢的資料,找到三種方法:
1.用cookie實現購物車;
2.用session實現購物車;
3.用cookie和數據庫(購物車信息持久化)實現購物車;
分析一下這三種方法的優缺點:
1.單純有cookie實現購物車,這樣的購物車不是很理想,設想一下,如果客戶端的瀏覽器把cookie給禁用了,這種方法就會在這里流產…
2.session中保存購物車的信息,這個只是在一個會話中可用,如果用戶沒有登錄,或者說登錄了以后,添加購物車,在關閉瀏覽器或者登出后,之前所添加的購物車通通都流產啦…
3.我這里要說就是這種方法啦…..
主要的流程:
a.用戶登錄前的數據流:用戶在沒有登錄系統的時候,對喜歡的商品進行添加購物車,那么這個時候,我們可以把購物車信息保存到cookie中,這里會涉及到cookie的添加,修改操作;也即如果之前在cookie中不存對應的cookie,則就對cookie進行添加操作。如果在cookie中存在對應的cookie,那么,這時候,就要對cookie進行修改操作了(這里涉及到用戶對同一個商品進行多次添加購物車的情況)。
b.用戶登錄后的數據流:用戶在登錄后,系統首先做的第一件事就是去獲取對應的cookies,如果存在相關的購物車cookies,那么就對該購物車信息進行相應用戶user的持久化操作,要么添加,要么修改。(添加操作:該用戶所對應的購物車如果沒有相應的信息進行添加操作;修改操作:類似的,如果存在對應用戶的購物車信息,就進行修改操作)。用戶登錄后,也可以進行購物車的添加操作,不過,這里不是添加到cookie中,而是直接持久化到數據庫中。注:用戶登錄后的數據都是和數據庫打交道。
代碼部分:
注:
1
|
conf.iduona_cashticket_cookie_startname = "iduona_cashticket_" ; |
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/** * 用戶登錄 * * @author hongten */ public void login() { //用戶登錄的時候,去讀取cookies,并且進行持久話操作,更多的登錄操作這里省略啦.... peristshoppingcartwhenuserlogin(newuser); } /** * 加入購物車<br> 我的java學習交流qq群:589809992 我們一起學java! * ============================================<br> * 用戶登錄前:<br> * 用戶在選擇現金券的時候,點擊現金券的加入購物車的時候,會把該現金券的信息(現金券的id,購買數量)<br> * 傳遞到這里,這時候,后臺要做的就是從cookie中查詢出是否有相同的記錄,如果有相同的記錄<br> * 則把相應的記錄更新;否則,就添加新的記錄<br> * 用戶登錄后:<br> * 用戶在登錄后,如果有添加購物車操作,則不用保存到cookie中,而是直接持久化購物車信息<br> * * @throws exception */ public void addtoshoppingcart() throws exception { if (cashticket == null || cashticket.getid() == null || cashticket.getid() < 1 ) { write( "nullid" ); } else if (q == null || q == "" ) { // 購買數量,默認情況下面為1 q = string.valueof( 1 ); } else { // 讀取所有的cookie cookie cookies[] = servletactioncontext.getrequest().getcookies(); if (cookies == null || cookies.length < 0 ) { // 沒有cookie system.out.println( "there is no any cookie .." ); } else { // 判斷用戶是否登錄 if (getuserinsession() == null ) { boolean flag = true ; for (cookie c : cookies) { if (c.getname().equals(conf.iduona_cashticket_cookie_startname + cashticket.getid())) { // 說明已有的cookies中有相應的cookie,就進行更新操作 integer oldvalue = integer.valueof(c.getvalue()); integer newvalue = integer.valueof(oldvalue + integer.valueof(q)); fixcookie(c, newvalue.tostring().trim()); flag = false ; } } // 說明已有的cookies中沒有相應的cookie,就進行添加操作 if (flag) { addcookie(conf.iduona_cashticket_cookie_startname + cashticket.getid(), q.trim()); } // ================================================== // 測試用,讀取所有的cookies readshoppingcartfromcookie(); // ================================================== write( "success" ); } else { // 如果用戶登錄,說明session存在user,這時就持久化購物車信息 cashticket cashtickettemp = cashticketservice.get(cashticket.getid()); if (shoppingcartservice.isexistuserandcashticket(getuserinsession(), cashtickettemp)) { shoppingcart oldshoppingcart = shoppingcartservice.getbyuserandcashticket(getuserinsession(), cashtickettemp); oldshoppingcart.setamount(oldshoppingcart.getamount() + integer.valueof(q)); if (shoppingcartservice.update(oldshoppingcart)) { write( "success" ); } } else { shoppingcart shoppingcarttemp = new shoppingcart(); shoppingcarttemp.setamount(integer.valueof(q)); shoppingcarttemp.setuser(getuserinsession()); shoppingcarttemp.setcashticket(cashtickettemp); shoppingcarttemp.setcreatetime( new date()); shoppingcarttemp.setstatustype(statustype.positive); shoppingcarttemp.setuuid(uuid.randomuuid().tostring()); if (shoppingcartservice.save(shoppingcarttemp)) { write( "success" ); } } } } } } /** * 從cookie中讀取購物車信息 * * @throws exception * @return */ public void readshoppingcartfromcookie() throws exception { system.out.println( "======================================================" ); cookie cookies[] = servletactioncontext.getrequest().getcookies(); if (cookies == null || cookies.length < 0 ) { // system.out.println("there is no any cookie .."); // 沒有cookie } else { for (cookie c : cookies) { system.out.println( "haha there are many cookies :" + c.getname() + " " + c.getvalue()); } } } /** * 添加cookie操作 * * @param name * cookie的name * @param value * cookie的value */ public void addcookie(string name, string value) { cookie cookie = new cookie(name.trim(), value.trim()); cookie.setmaxage( 2 * 60 * 60 * 1000 ); // 設置為2個鐘 servletactioncontext.getresponse().addcookie(cookie); } /** * 更新cookie操作 * * @param c * 要修改的cookie * @param value * 修改的cookie的值 */ public void fixcookie(cookie c, string value) { c.setvalue(value.trim()); c.setmaxage( 2 * 60 * 60 * 1000 ); // 設置為2個鐘 servletactioncontext.getresponse().addcookie(c); } /** * 當用戶登錄的時候,持久化cookie中的購物車信息,更新為本用戶的購物車信息 */ public void peristshoppingcartwhenuserlogin(user user) { if ( null != user) { cookie cookies[] = servletactioncontext.getrequest().getcookies(); if (cookies != null ) { for (cookie c : cookies) { if (c.getname().startswith(conf.iduona_cashticket_cookie_startname)) { // 獲取cookie的名稱:"iduona_cashticket_45" 和 cookie的值: "21" string name = c.getname(); integer amount = integer.valueof(integer.valueof(c.getvalue())+integer.valueof(q)); integer ct_id = integer.valueof(name.substring(name.lastindexof( "_" ) + 1 )); cashticket temp = cashticketservice.get(ct_id); shoppingcart shoppingcarttemp = new shoppingcart(); if ( null != temp) { if (shoppingcartservice.isexistuserandcashticket(user, temp)) { // 進行更新操作 shoppingcart oldshoppingcart = shoppingcartservice.getbyuserandcashticket(user, temp); oldshoppingcart.setamount(amount); shoppingcartservice.update(oldshoppingcart); } else { // 否則進行保存記錄 shoppingcarttemp.setamount(amount); shoppingcarttemp.setuser(user); shoppingcarttemp.setcashticket(temp); shoppingcarttemp.setcreatetime( new date()); shoppingcarttemp.setstatustype(statustype.positive); shoppingcarttemp.setuuid(uuid.randomuuid().tostring()); shoppingcartservice.save(shoppingcarttemp); } } } } // 移除所有的現金券cookies removeallcookies(); } } } /** * 移除所有的現金券cookies操作 */ public void removeallcookies() { cookie cookies[] = servletactioncontext.getrequest().getcookies(); if (cookies == null || cookies.length < 0 ) { // 沒有cookie system.out.println( "there is no any cookie .." ); } else { system.out.println( "開始刪除cookies.." ); for (cookie c : cookies) { if (c.getname().startswith(conf.iduona_cashticket_cookie_startname)) { c.setmaxage( 0 ); // 設置為0 servletactioncontext.getresponse().addcookie(c); } } } } |
效果:
用戶沒有登錄的情況下
用戶登錄了以后:
數據庫里面的情況:
登錄前數據
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://geek.csdn.net/news/detail/240655?utm_source=tuicool&utm_medium=referral