SSH Framework Online Mall project 17th War Shopping cart basic function _java

Source: Internet
Author: User
Tags zip ssh

In the previous section we finished the detailed page of the product and used the hibernate level two cache load Detail page to improve the performance of the system, click the article to view:. In this section we start doing the shopping cart section.

1. Add a new table
First we add a few tables to the database: the user table, the Order Status table, the order form (cart table), and the shopping item table. The user's table contains basic information about the user, the order Status table mainly stores the status of the order, such as shipped this, the order table mainly stores the user's information and order status, so it is associated with the user table and Order Status table, the shopping Item table stores a product and the order, so it is related to the commodity table and order form. See the following SQL statement for specific table information:

/*============================*//* Table: User table structure *//*============================*/CREATE TABLE User (/* User number    , Automatic growth * * ID int primary key NOT NULL auto_increment,/* User login name/login varchar (20),/* User's real name */Name varchar (20),/* User login Password/pass varchar (20),/* User sex * * sex varchar (20),/* User Tel/Phone varchar (20) 
 
,/* User Email * * Email varchar (20));  /*=============================*//* Table: Order Status Table Structure * */*=============================*/CREATE TABLE status (/* 
 
Status number, automatic growth * * ID int primary key NOT NULL auto_increment,/* order state/Status varchar (10)); 
 /*=============================*//* Table: Shopping cart (order) Sheet Structure * */*=============================*/CREATE TABLE Forder ( /* order number, automatic growth * * ID int primary key NOT NULL auto_increment,/* Recipient name * * VARCHAR (20),/* Recipient phone/Pho NE varchar (20),/* Distribution information/* remark varchar (20),/* Next single date/date timestamp default Current_timestAMP,/* Total Order Amount * * (8,2),/* Recipient ZIP/Post varchar (20),/* Recipient ZIP/Address varchar (200), /* Order status/SID int default 1,/* Member number/UID int, constraint SID_FK foreign key (SID) references status (i 
 
d), Constraint UID_FK foreign key (UID) references user (ID); /*=============================*//* Table: Shopping Item table Structure * */*=============================*/CREATE TABLE Sorder (/  
 * Shopping item number, automatic growth * * ID int primary key NOT NULL auto_increment,/* The name of the purchased product * * * VARCHAR (20),/* The price of the commodity at the time of purchase * * *      Price Decimal (8,2),/* Purchase quantity/number int NOT NULL,/* belongs commodity number/PID int,/* This order item, the order number that belongs to/FID int, constraint PID_FK foreign KEY (PID) references product (ID), Constraint FID_FK foreign key (FID) references for 
 Der (id));

Then we convert these tables to Pojo by reverse engineering, not in detail.

2. Background Logic of Shopping cart
the logic of the 2.1 service layer
When a user adds a product to a shopping cart, we first need to get the product information through the ID of the goods, and then add the product to the shopping cart, before adding, we first have to determine whether the current session of the shopping cart, if not, we have to create a shopping cart, if there is, We add the current items to our shopping cart, before adding, you need to determine whether the shopping item in the shopping cart already exists, if there is only need to increase the corresponding amount of shopping, if it does not exist then add, and then calculate the total price of shopping, and finally the shopping cart saved to the session. The whole process is shown in the following schematic:

Next, we'll implement the specific logic, starting with the new two service interfaces: Sorderservice and Forderservice. There are two main methods defined in Sorderservice: Converting a user-added item to a shopping item, and then adding a shopping item to a shopping cart; Forderservice mainly defines methods for calculating the total price of a shopping cart, as follows:

Sorderservice interface Public 
interface Sorderservice extends baseservice<sorder> { 
 //Add shopping items, return to new shopping cart 
 Public Forder Addsorder (Forder forder, product product); 
 Convert the product data into a shopping item public 
 sorder Producttosorder (product product); 
} 
Forderservice interface Public 
interface Forderservice extends baseservice<forder> { 
 //Calculate total shopping Price 
 Public double clutotal (Forder forder); 
} 

Then we implement the two interfaces concretely:

Sorderserviceimpl Implementation Class @Service ("Sorderservice") public class Sorderserviceimpl extends Baseserviceimpl<sorder > Implements Sorderservice {@Override public forder addsorder (Forder forder, product product) {The boolean is Have = false; 
  Used to mark a repeat shopping item//Get the current shopping item Sorder Sorder = Producttosorder (product); Determines whether the current item is duplicated and, if it is repeated, adds a quantity for (Sorder old:forder.getSorders ()) {if (Old.getproduct (). GetId (). Equals (Sorder.getpro 
    Duct (). GetId ())) {//shopping item has duplicate, add quantity can Old.setnumber (Old.getnumber () + sorder.getnumber ()); 
    Ishave = true; 
   Break 
  }//The current shopping item does not exist in the shopping cart, the new addition can be if (!ishave) {forder.getsorders (). Add (Sorder); 
 return forder; 
  @Override Public Sorder Producttosorder (product product) {Sorder Sorder = new Sorder (); 
  Sorder.setname (Product.getname ()); 
  Sorder.setnumber (1); 
  Sorder.setprice (Product.getprice ()); 
  Sorder.setproduct (product); 
 return sorder; The//forderserviceimpl implementation class @Service ("ForderserVice ") public class Forderserviceimpl extends baseserviceimpl<forder> implements Forderservice {@Override PU 
  Blic double clutotal (Forder forder) {double total = 0.0; 
  For (Sorder sorder:forder.getSorders ()) {total = Sorder.getnumber () * Sorder.getprice (); 
 Return to total;  } 
  
}

Then we need to inject the two beans into the baseaction for sorderaction to use:

@Controller ("Baseaction") 
@Scope ("prototype") Public 
class Baseaction<t> extends Actionsupport Implements requestaware,sessionaware,applicationaware,modeldriven<t> { 
 
 //Omitting other extraneous code 
 
 ... @Resource 
 protected Forderservice forderservice; 
 @Resource 
 protected Sorderservice sorderservice; 
 
} 

Well, the logic of the service layer is done, and the next step is to do the action part:
The logic of the 2.2 action section
We create a new sorderaction and walk through the process shown on the logical diagram above to complete the logic of adding a cart. The code is as follows:

@Controller 
@Scope ("prototype") Public 
class Sorderaction extends baseaction<sorder> { 
 public String Addsorder () { 
   
  //1. Obtain the corresponding product data product product 
  = Productservice.get (Model.getproduct () according to Product.id. GetId ()); 
   
  2. Determines whether the current session has a shopping cart, or if not, create if 
  (Session.get ("forder") = null) { 
   //Create a new cart and store it in session 
   Session.put (" Forder ", New Forder (New Hashset<sorder> ())); 
  } 
 
  3. Convert the product information to Sorder and add it to the shopping cart (to determine if the item is duplicated) 
  forder Forder = (forder) session.get ("Forder"); 
  Forder = Sorderservice.addsorder (Forder, product); 
   
  4. Calculate the total price of shopping 
  Forder.settotal (forderservice.clutotal (Forder)); 
  5. Store the new shopping cart in session 
  session.put ("Forder", Forder); 
  return "Showcart"; 
 } 
 

Configure the Struts.xml file:

<action name= "sorder_*" class= "sorderaction" method= "{1}" > 
 <result name= "Showcart" >/showcart.jsp </result> 
</action> 

Then jump to the shopping cart display page showcart.jsp,showcart.jsp The foreground procedure for the shopping cart section is as follows:


3. The front link jumps

The backstage part all finished, next will the foreground detail.jsp page Add the shopping cart the link address to be able to visit Sorderaction:


This will be able to jump correctly, below we look at the shopping Cart display page specific effect:

The basic function of our shopping cart is done, and we will make some improvements to it later.

Original address: http://blog.csdn.net/eson_15/article/details/51418350
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.