Article From:https://www.cnblogs.com/sunshine5683/p/9913057.html

 

    From: https://blog.csdn.net/eson_15/article/details/51433247

       The last two problems of shopping cart are order information cascading and page caching. The information here refers to shopping cart and shopping items. That is to say, we store the information of shopping cart in the database at the same time, we also store the information of each shopping item, and the foreign keys are well correlated, which involves Hibernat.The cascade storage problem in e; page caching problem refers to when the user confirms the order, if the point falls back, it will return to the order confirmation page, the just order confirmation page comes out again, and the session is still there, the information is just the information, which is obviously not the result we want, we will be in the later.Face to face analysis. This section mainly discusses the cascading of order information and the caching of pages.

1. Cascade warehousing of order information

        HibernateWe need to configure the cascade entry of the two related tables. Here we mainly introduce the configuration of annotations. POJO for orders is Forder, POJO for shopping items is Sorder, Forder and Sorder are one-to-many relationships. First, we set up their annotation configuration.As follows:

 

  1.  
    @Entity
  2.  
    public class Forder implements java.io.Serializable {
  3.  
     
  4.  
    //Eliminate irrelevant code…
  5.  
     
  6.  
    private List<Sorder> sorders = new ArrayList<Sorder>();
  7.  
     
  8.  
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = “forder”)
  9.  
    public List<Sorder> getSorders() {
  10.  
    return this.sorders;
  11.  
    }
  12.  
     
  13.  
    public void setSorders(List<Sorder> sorders) {
  14.  
    this.sorders = sorders;
  15.  
    }
  16.  
    }
  17.  
     
  18.  
    @Entity
  19.  
    public class Sorder implements java.io.Serializable {
  20.  
     
  21.  
    //Eliminate irrelevant code…
  22.  
     
  23.  
    private Forder forder;
  24.  
     
  25.  
    @ManyToOne(fetch = FetchType.LAZY)
  26.  
    @JoinColumn(name = “fid”)
  27.  
    public Forder getForder() {
  28.  
    return this.forder;
  29.  
    }
  30.  
     
  31.  
    public void setForder(Forder forder) {
  32.  
    this.forder = forder;
  33.  
    }
  34.  
    }

        After this configuration, when we save the order item, we also save the purchase item, and automatically associate foreign keys. But the premise is that we set up the relationship between them, that is, setSorders () in Forder and setForder () in sorder.Attributes in entities corresponding to other associated foreign keys.

 

        Forder. setSorder (sorder) was executed when we added items to the shopping cart. Now we need to add Forder to the sorder, so we add it to the original code as follows:

 

  1.  
    //This is the code in Section 17. Let’s insert a sentence in the middle.
  2.  
    @Service(“sorderService”)
  3.  
    public class SorderServiceImpl extends BaseServiceImpl<Sorder> implements
  4.  
    SorderService {
  5.  
     
  6.  
    @Override
  7.  
    public Forder addSorder(Forder forder, Product product) {
  8.  
    boolean isHave = false; //Used to mark whether there are duplicate items
  9.  
    //Get the current item
  10.  
    Sorder sorder = productToSorder(product);
  11.  
    //Judge whether the current shopping item is duplicated or not. If duplicated, add quantity.
  12.  
    for(Sorder old : forder.getSorders()) {
  13.  
    if(old.getProduct().getId().equals(sorder.getProduct().getId())) {
  14.  
    //Shopping items are duplicated, adding quantity is enough.
  15.  
    old.setNumber(old.getNumber() + sorder.getNumber());
  16.  
    isHave = true;
  17.  
    break;
  18.  
    }
  19.  
    }
  20.  
    //Current shopping items do not exist in the shopping cart, just add new items.
  21.  
    if(!isHave) {
  22.  
    //Let’s insert a sentence here:
  23.  
    //Before adding items to a shopping cart, the association between items and shopping cart is established, but at this time forder. ID is null.
  24.  
    //But when it comes to warehousing, it’s the shopping cart first, and then the items. Then there’s the main key.
  25.  
    sorder.setForder(forder);
  26.  
    forder.getSorders().add(sorder);
  27.  
    }
  28.  
     
  29.  
    return forder;
  30.  
    }
  31.  
     
  32.  
    @Override
  33.  
    public Sorder productToSorder(Product product) {
  34.  
    Sorder sorder = new Sorder();
  35.  
    sorder.setName(product.getName());
  36.  
    sorder.setNumber(1);
  37.  
    sorder.setPrice(product.getPrice());
  38.  
    sorder.setProduct(product);
  39.  
    return sorder;
  40.  
    }
  41.  
    }

        Okay, let’s see which action we jumped to when the order was confirmed:

 

        So let’s complete the logic in forderAction:

 

  1.  
    @Controller(“forderAction”)
  2.  
    @Scope(“prototype”)
  3.  
    public class ForderAction extends BaseAction<Forder> {
  4.  
     
  5.  
    @Override
  6.  
    public Forder getModel() {
  7.  
    model = (Forder) session.get(“forder”);
  8.  
    return model;
  9.  
    }
  10.  
     
  11.  
    //Implementing Cascade Storage Function of Shopping Cart (Order) and Shopping Item (Order Item)
  12.  
    public String save() {
  13.  
    // // Deliver the shopping items in session to the current model object
  14.  
    // Forder forder = (Forder) session.get(“forder”);
  15.  
    // //model.setSorders(forder.getSorders());
  16.  
    // forder.setAddress(model.getAddress());
  17.  
    // forder.setName(model.getName());
  18.  
    // forder.setPhone(model.getPhone());
  19.  
    // forder.setRemark(model.getRemark());
  20.  
    // forder.setUser((User)session.get(“user”));
  21.  
    // forder.setStatus(new Status(1));
  22.  
    // forder.setPost(model.getPost());
  23.  
    // //Cascading in libraries (which need to be configured in XML or POJO annotations) requires sorder Association Forder
  24.  
    // //Add sorder. setForder (forder) to the orderServiceImpl class.
  25.  
    // forderService.save(forder);
  26.  
     
  27.  
    model.setUser((User)session.get(“user”));
  28.  
    model.setStatus(new Status(1));
  29.  
    forderService.save(model);
  30.  
     
  31.  
    return “bank”;
  32.  
    }
  33.  
    }

 

        As you can see from the above code, there are two ways: the first is to not override the getModel method (the part I commented out), which is rather clumsy because ForderAction inherits BaseAction and BaseAction implements ModelD.The riven interface, so the data will be encapsulated in the model, which is an attribute of BaseAction. Then we need to transfer all the information in the model to the Forder in the session, and then the data in the Forder will be available.To cascade into the library with Sorder, but this method is a bit clumsy… So we use the second method, rewrite the getModel method, assign the Forder to the model directly, and then we just need to add the cascaded items in the model, that is, the above non-note.The code of interpretation. After the user clicks on the order confirmation, the information is stored in the warehouse and jumps to the payment page (the payment page needs to be done next, at present it can be jumped to a JSP at will).

2. Page cache problem

        Now the cascade inputting of order information has been solved, but if the user clicks to confirm the order, and then goes back, we find that the original order confirmation page is still the same, and the information is just the information, session is not relevant, that is to say, it is equivalent to me to confirm the order information again, which is obviously inappropriate, that is, the user clicks to confirm the order information.Said, when the user clicks to confirm the order, we can not let the page cache, so that when the user clicks back, it will show that the page has expired, we can let it jump to the home page.

        We know that the front JSP page can be set so that the browser does not cache data, so we can do the following settings in the front confirm. JSP page:

        But the problem is not so simple, it is not possible to do this alone. In this way, the user will click back to show that the page has expired, but when the user refreshes and fails, it will show that the cache loads the original data. So we understand that since sessions are not closed yet, SESThere is an order information Forder in the sion. Users will definitely continue to get this Forder when refreshing it. It will display the original order information. So it is impossible to solve the problem only by setting it in the front desk. We also need to do relevant processing in the back desk.

        Now that we know what the problem is, we can do this: because when the user clicks to confirm the order, it will be submitted to ForderAction, and then ForderAction will be processed and jump to the payment page. We can do something in ForderAction: We willIf the original Forder in session is cleared, is that OK? This is feasible, but considering that the information about the order is still needed for later payment, we can save the original Forder in the session to another place and then the original fo.Rder is empty, so we add two lines of code to the ForderAction above, as follows:

 

  1.  
    @Controller(“forderAction”)
  2.  
    @Scope(“prototype”)
  3.  
    public class ForderAction extends BaseAction<Forder> {
  4.  
     
  5.  
    @Override
  6.  
    public Forder getModel() {
  7.  
    model = (Forder) session.get(“forder”);
  8.  
    return model;
  9.  
    }
  10.  
     
  11.  
    //Implementing Cascade Storage Function of Shopping Cart (Order) and Shopping Item (Order Item)
  12.  
    public String save() {
  13.  
    // // Deliver the shopping items in session to the current model object
  14.  
    // Forder forder = (Forder) session.get(“forder”);
  15.  
    // //model.setSorders(forder.getSorders());
  16.  
    // forder.setAddress(model.getAddress());
  17.  
    // forder.setName(model.getName());
  18.  
    // forder.setPhone(model.getPhone());
  19.  
    // forder.setRemark(model.getRemark());
  20.  
    // forder.setUser((User)session.get(“user”));
  21.  
    // forder.setStatus(new Status(1));
  22.  
    // forder.setPost(model.getPost());
  23.  
    // //Cascading in libraries (which need to be configured in XML or POJO annotations) requires sorder Association Forder
  24.  
    // //Add sorder. setForder (forder) to the orderServiceImpl class.
  25.  
    // forderService.save(forder);
  26.  
     
  27.  
    model.setUser((User)session.get(“user”));
  28.  
    model.setStatus(new Status(1));
  29.  
    forderService.save(model);
  30.  
     
  31.  
    //When the shopping cart is in storage, the shopping cart in the original session should be emptied.
  32.  
    session.put(“oldForder”, session.get(“forder”));//Save the original shopping cart information first, because you need the relevant information when you pay later.
  33.  
    session.put(“forder”, new Forder());//newA new empty shopping cart (equivalent to empty shopping cart) can also be easily bought by users.
  34.  
    return “bank”;
  35.  
    }
  36.  
    }

        Then before we finish, we have to add the following code to the confirmation order page at the front desk:

 

        Now the logic is clear, first to the order confirmation page, Forder has data, so it is not empty, this judgment is invalid, when the user clicks to confirm the order, in ForderAction, we will replace the Forder with an empty Forder object, that is to say.The original data is gone (we save another key pair in the session for later payment), so when the user points back and goes back to the confirmation page of the order, that judgment will take effect, and then jump to the home page. Here, the whole logic is complete, and the page caching problem is solved.。

Leave a Reply

Your email address will not be published. Required fields are marked *