In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Today, to write about shopping carts, here are four questions:
1) if the user does not log in to the user name and password, add the product, close the browser and then open it, do not log in the user name and password
Q: do you still have the shopping cart goods?
2) the user logs in with the user name and password, adds the product, closes the browser and then opens it without logging in the user name and password.
Q: do you still have the shopping cart goods?
3) the user logs in the user name and password, adds the product, closes the browser, then opens it, and logs in the user name and password
Q: do you still have the shopping cart goods?
4) the user logs in the username and password, adds the product, closes the browser, opens the browser at home and opens the login username and password.
Q: do you still have the shopping cart goods?
The above four questions are all based on JD.com, so guess what the result is?
1) in
2) gone
3) in
4) in
If you can guess the answer, then you are really good, so how do you achieve these four points? (if you have a disapproving partner, you can experiment with JD.com.)
Next we will explain the principle of shopping cart, and then talk about the specific implementation of code.
1) the user does not log in, add the item, at this time the item is added to the browser's Cookie, so when you visit again (do not log in), the item is still in the Cookie, so the item in the shopping cart still exists.
2) when the user logs in and adds items, both the items in Cookie and the items selected by the user will be added to the shopping cart, and then the items in Cookie will be deleted. So when the user visits again (does not log in), the shopping cart item in Cookie has been deleted, so the shopping cart item is gone.
3) when the user logs in and adds the item, the item is added to the database for persistent storage, and the login user name and password are opened again, and the product selected by the user must still exist, so the item in the shopping cart still exists.
4) reason 3)
Here again, the advantages of saving items to Cookie without login and the comparison between saving to Session and database:
1:Cookie: pros: save user browsers (don't waste our company's servers) disadvantages: Cookie disabled, do not provide save
2:Session: (Redis: waste a lot of server memory: implement, disable Cookie) very fast
3: the speed of database (Mysql, Redis, SOlr) is too slow if it can be persisted.
So what I'm going to talk about today is:
User not logged in: shopping cart added to Cookie
User login: save the shopping cart to Redis (no database)
Illustration of the overall thinking:
Then there is a code example to implement the function of the shopping cart:
First, let's take a look at the design of the shopping cart and the shopping item JavaBean:
Shopping cart: buyerCart.java
The @ JsonIgonre annotation is used here because the following needs to convert BuyerCart to Json format, and these fields only have the get method, so they cannot be converted, and you need to ignore Json.
Here are the shopping items: buyerItem.java
1. Add goods to the shopping cart
The parameters passed here are skuId (primary key of inventory table, id of goods stored in inventory table, color, size, inventory, etc.), purchase quantity amount.
Then let's take a look at how Controller handles it:
Here is a knowledge point: convert an object into a json string / json string into an object
We first write a small Demo to demonstrate the conversion between json and objects, which uses the ObjectMapper class in springmvc.
Execution result:
Here we use Include.NON_NULL. If the attribute in TestTb is null, it will not be converted to Json. From the object-- > Json string, use objectMapper.writeValue (). From the Json string-- > object uses objectMapper.readValue ().
Return to the code in our project above and add this item to Cookie only if you are not logged in.
We debug can see:
Here the object shopping cart object buyerCart has been converted to Json format.
Add items to the shopping cart, whether logged in or not, first take out the shopping cart in Cookie, and then append the currently selected item to the shopping cart.
Then log in and empty the shopping cart in Cookie and add the contents of the shopping cart to Redis for persistence.
If you are not logged in, append the selected item to Cookie.
The code to append the shopping cart to Redis: insertBuyerCartToRedis (this includes determining whether the same item is added)
Determine whether the user is logged in: String username =
SessionProviderService.getAttributterForUsername (RequestUtils.getCSessionId (request, response))
2. Shopping cart display page
Finally, redirect to the shopping cart display page: return "redirect:/shopping/toCart". There are two ways to enter the settlement page:
1) Click to add the shopping cart on the product details page.
2) Click the shopping cart button directly to enter the shopping cart settlement page.
Let's take a look at the code on the settlement page:
Here is the shopping cart details display page, it should be noted that if the same item is continuously added, it needs to be merged.
The shopping cart details display page includes two parts: 1) merchandise details 2) Total (total merchandise, freight)
Among them, 1) the details of the goods include the size, color, quantity and availability of the goods.
Take out the shopping cart from Redis: buyerCart = cartService.selectBuyerCartFromRedis (username)
Fill up the shopping cart. Just load the skuId into the shopping cart. Here you also need to find out the sku details: List items = buyerCart.getItems ()
BuyerItem.setSku (cartService.selectSkuById (buyerItem.getSku (). GetId ()
Then return to "cart.jsp", which is the shopping cart details display page.
3. Go to the settlement page
This means that the user must log in and there must be items in the shopping cart.
So here you need to make use of the filtering function of springmvc. Users must log in when they click on the settlement, and if they do not log in, they will be prompted to log in.
Check out the specified shopping cart, because the items we need to buy will be checked on the shopping cart details page before we settle the check, so the checkout is based on the checked items.
BuyerCart buyerCart = cartService.selectBuyerCartFromRedisBySkuIds (skuIds, username)
Take the specified item out of the shopping cart:
1) when we buy only one item that is out of stock, refresh the shopping cart details page to show the status of the item out of stock.
2) when shopping cart noon goods, refresh the current page.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.