18. Shopping cart service (ThreadLocal)

1. Get shopping cart data (go to shopping cart page)

First determine whether the user is logged in:

i) If logged in, get the shopping cart of the logged-in user and merge the temporary shopping cart (also judge whether the temporary shopping cart has data)

After merging, clear the data of the temporary shopping cart, and then go to the user's shopping cart after logging in

ii) If you are not logged in, get all the shopping items in the temporary shopping cart

Determine whether the user is logged in using ThreadLocal:

Use a filter chain to implement HandlerInterceptor and add the filter chain to the filter column

Determine whether there is user information before the target method is executed:

Put user information in ThreadLocal, so that multiple threads can access the same user information without access conflict

After the method is executed, add the assigned temporary user to the cookie

2. Add a shopping cart

The difference between addFlashAttribute() and addAttribute():

i) addFlashAttribute() puts the data in the session, which can be retrieved on the page, but it can only be retrieved once

ii) addAttribute() 1. Forward: put in the request field 2. Redirect: after splicing to the path

addToCart method:

Block and wait for the completion of the two asynchronous tasks, and then save the data to redis

If there are products added to the shopping cart this time in redis, just modify the number of products

The above getCartOps() method is to bind the shopping cart to be operated:

3. Get all shopping items in a shopping cart

4. Get the shopping items in the shopping cart according to skuId

5. Change the number of items in the shopping cart:

Get it from the shopping cart, change the quantity and rewrite it to redis

6. Adding the shopping cart successfully-Jump to the success page. This step is to prevent repeated submissions . The url address will be submitted all the time, and it is necessary to jump to a stable url address

Jump to an address and get all shopping cart items through skuId

Successfully achieved:

Go to the shopping cart page:

Succeeded in adding shopping cart: