Shopping cart settlement function of EasyUI project

Keywords: EasyUI

1, Clear objectives:

          1. Solve the problem of garbled names displayed in the last shopping cart

          2. Add order item table to database

          3. Add order table to database

          4. When changing the quantity in the order item table, the sales volume in the book table will change accordingly

2, Solve the problem of garbled names displayed in the last shopping cart

Analyze garbled Code:

        The value passed to the background may be garbled for two reasons

        1. Garbled code has been generated at the end of the foreground -- > browser

        2. Back end -- > garbled code occurs when the sub controller accepts the transfer from the front end to the background

Solution:

The code format of the front end is UTF-8, and the background will enter the coding filter after entering from mvc.

UTF-8-----   ISO-8859-1 will be coded incorrectly

package com.zking.util;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Chinese garbled code processing
 * 
 */
@WebFilter("*.action")
public class EncodingFiter implements Filter {

     private String encoding = "UTF-8";// Default character set

    public EncodingFiter() {
        super();
    }

    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;

        // Chinese processing must be put in front of the chain.doFilter(request, response) method
        res.setContentType("text/html;charset=" + this.encoding);
        if (req.getMethod().equalsIgnoreCase("post")) {
            req.setCharacterEncoding(this.encoding);
        }

//Delete this paragraph

/**else {
             Map map = req.getParameterMap();// Save the map collection of all parameter names = parameter values (array)
             Set set = map.keySet();// Fetch all parameter names
            Iterator it = set.iterator();
            while (it.hasNext()) {
                String name = (String) it.next();
                 String[] values = (String[]) map.get(name);// Get the parameter value [Note: the parameter value is an array]
                for (int i = 0; i < values.length; i++) {
                    values[i] = new String(values[i].getBytes("ISO-8859-1"),
                            this.encoding);
                }
            }
        }
     **/
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
         String s = filterConfig.getInitParameter("encoding");// Read the character set configured in the web.xml file
        if (null != s && !s.trim().equals("")) {
            this.encoding = s.trim();
        }
    }

}
 

  Display effect:

3, Add order item table to database

1. Requirements: 1. Data entry form     2. Data entry order item     3. After the order is placed, the sales quantity of goods shall be changed

2. Idea:

            1. First, create an entity class about the attributes of the order item table

              2. Next, write the OrderItemDao layer method

              3. Next is the code in Action

              4. Finally, configure

1. First, create an entity class (OrderItem) about the attributes of the order item table   Order item table)

package com.zxy.entity;
 
public class OrderItem {
  private long id;
  private long oid;
  private String bid;
  private int quantity;
public long getId() {
	return id;
}
public void setId(long id) {
	this.id = id;
}
public long getOid() {
	return oid;
}
public void setOid(long oid) {
	this.oid = oid;
}
public String getBid() {
	return bid;
}
public void setBid(String bid) {
	this.bid = bid;
}
public int getQuantity() {
	return quantity;
}
public void setQuantity(int quantity) {
	this.quantity = quantity;
}
@Override
public String toString() {
	return "OrderItem [id=" + id + ", oid=" + oid + ", bid=" + bid + ", quantity=" + quantity + "]";
}
 
 
}

  2. Next, write the OrderItemDao layer method

package com.zxy.dao;
 
import com.zxy.entity.OrderItem;
import com.zking.util.BaseDao;
 
public class OrderItemDao extends BaseDao<OrderItem>{
 
	
	public void add(OrderItem t) throws Exception {
		String sql="insert into t_easyui_orderItem(oid,bid,quantity) values(?,?,?)";
		super.executeUpdate(sql, t,new String[] {"oid","bid","quantity"});
	}
}

  3. Next is the code in Action (order receipt and order item receipt)

if (StringUtils.isNotBlank(pageStr) && pageStr.length() > 1) {
				for (String item : pageStr.substring(1).split(",")) {
					OrderItem oi=new OrderItem();
					String[] itemEle=item.split("-");
					oi.setBid(itemEle[0]);
					oi.setOid(newest.getId());
					oi.setQuantity(Integer.valueOf(itemEle[2]));
				    oid.add(oi);
				    Book b=new Book();
				    b.setName(itemEle[0]);
				    b.setSales(Integer.valueOf(itemEle[2]));
				    bd.editSales(b);
				}

Operation results:

  IV   Add order table to database

Order:

package com.zxy.entity;
 
import java.util.Date;
 
public class Order {
   private long id;
   private long uid;
   private Date orderTime;
   private String consignee;
   private String phone;
   private String postalcode;
   private String address;
   private int sendType;
   private Date sendTime;
   private float orderPrice;
   private int orderState;
public long getId() {
	return id;
}
public void setId(long id) {
	this.id = id;
}
public long getUid() {
	return uid;
}
public void setUid(long uid) {
	this.uid = uid;
}
public Date getOrderTime() {
	return orderTime;
}
public void setOrderTime(Date orderTime) {
	this.orderTime = orderTime;
}
public String getConsignee() {
	return consignee;
}
public void setConsignee(String consignee) {
	this.consignee = consignee;
}
public String getPhone() {
	return phone;
}
public void setPhone(String phone) {
	this.phone = phone;
}
public String getPostalcode() {
	return postalcode;
}
public void setPostalcode(String postalcode) {
	this.postalcode = postalcode;
}
public String getAddress() {
	return address;
}
public void setAddress(String address) {
	this.address = address;
}
public int getSendType() {
	return sendType;
}
public void setSendType(int sendType) {
	this.sendType = sendType;
}
public Date getSendTime() {
	return sendTime;
}
public void setSendTime(Date sendTime) {
	this.sendTime = sendTime;
}
public float getOrderPrice() {
	return orderPrice;
}
public void setOrderPrice(float orderPrice) {
	this.orderPrice = orderPrice;
}
public int getOrderState() {
	return orderState;
}
public void setOrderState(int orderState) {
	this.orderState = orderState;
}
@Override
public String toString() {
	return "Order [id=" + id + ", uid=" + uid + ", orderTime=" + orderTime + ", consignee=" + consignee + ", phone="
			+ phone + ", postalcode=" + postalcode + ", address=" + address + ", sendType=" + sendType + ", sendTime="
			+ sendTime + ", orderPrice=" + orderPrice + ", orderState=" + orderState + "]";
}
 
 
 
}

 OrderDao:

package com.zxy.dao;

import java.util.List;

import com.zking.util.BaseDao;
import com.zking.util.PageBean;
import com.zxy.entity.Order;

public class OrderDao extends BaseDao<Order> {
public void add(Order t) throws Exception {
	String sql="insert into t_easyui_order(uid,orderTime,consignee,phone,postalcode,address,sendType,sendTime,orderPrice,orderState) "
			+ "values(?,now(),?,?,?,?,?,?,?,?)";
	super.executeUpdate(sql, t, new String[] {"uid","consignee","phone","postalcode","address","sendType","sendTime","orderPrice","orderState"});
}
/**
 * Reverse query by order time
 * @param order
 * @param pageBean
 * @return
 * @throws Exception
 */
public List<Order> list(Order order,PageBean pageBean) throws Exception {
	String sql = "select * from t_easyui_order order by orderTime desc";
	return super.executeQuery(sql, Order.class, pageBean);
}

}

ShoppingAction:

package com.zxy.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriver;
import com.zking.util.ResponseUtil;
import com.zking.util.StringUtils;
import com.zking.vo.ShoppingVo;
import com.zxy.dao.BookDao;
import com.zxy.dao.OrderDao;
import com.zxy.dao.OrderItemDao;
import com.zxy.entity.Book;
import com.zxy.entity.Order;
import com.zxy.entity.OrderItem;
import com.zxy.entity.User;

public class ShoppingAction extends ActionSupport implements ModelDriver<ShoppingVo>{
    private ShoppingVo vo=new ShoppingVo();
    private OrderDao orderDao=new OrderDao();
	private OrderItemDao orderItemDao=new OrderItemDao();
	private BookDao bookDao=new BookDao();

	public ShoppingVo getModel() {
		return vo;
	}

    //Order receipt and order item receipt
    public void createOrder(HttpServletRequest req, HttpServletResponse resp) {
    	/**
    	 * 1.Data subscription form
    	 * 2.Data entry order item
    	 * 3.After the order is placed, the sales quantity of the goods shall be changed
    	 */
    		User cuser=(User) req.getSession().getAttribute("cuser");
    	    if(cuser == null) {
    	    	return;
    	    }
    		//Assembly data
    		Order order=new Order();
    		order.setAddress(vo.getAddress());
    		order.setConsignee(vo.getConsignee());
    	    //Total order price
    		float orderPrice=0f;
    		String pageStr=vo.getPageStr();
    		if(StringUtils.isNotBlank(pageStr) && pageStr.length() > 1) {
    			for(String item:pageStr.substring(1).split(",")) {
    				String[] itemEle=item.split("-");
    					orderPrice +=Float.valueOf(itemEle[3]);
    				}
    			}
    		
    		order.setOrderPrice(orderPrice);
    		order.setOrderState(1);
    		order.setPhone(vo.getPhone());
    		order.setPostalcode(vo.getPostalcode());
    	    order.setSendType(vo.getSendType());
    	    order.setUid(cuser.getId());
    	    try {
    			orderDao.add(order);
    			Order newest=orderDao.list(null,null).get(0);
    			//You need to set which order the current order item belongs to. The idea is to query the ID of the latest order
    			if(StringUtils.isNotBlank(pageStr) && pageStr.length() > 1) {
    				for(String item:pageStr.substring(1).split(",")) {
    					OrderItem oi=new OrderItem();
    					String[] itemEle=item.split("-");
    				    oi.setBid(itemEle[0]);
    				    //Perform a flashback query according to the order time and get the first data
    				   // oi.setOid(oid);
    				    oi.setOid(newest.getId());
    				    oi.setQuantity(Integer.valueOf(itemEle[2]));
    				    orderItemDao.add(oi);
    				//If the current book is purchased, the sales volume of this book should be + Quantity
    				    Book b=new Book();
    				    b.setName(itemEle[0]);
    				    b.setSales(Integer.valueOf(itemEle[2]));
    				    bookDao.editSales(b);
    				}
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    }	
}

Operation results:

 

  After operation:

   t_easyui_order: order table result

   t_easyui_orderItem: order item table result

 

   5, When changing the quantity in the order item table, the sales volume in the book table will change accordingly:

BookDao: modify the sales volume according to the name

/**
	 * Modify the sales volume according to the name of the book
	 * @param book
	 * @throws Exception
	 */
	public void editSales(Book book) throws Exception {
		super.executeUpdate("update t_easyui_book set sales=sales+?  where name=?", book, new String[] { "sales", "name" });
	}
	

ShoppingAction:

//If the current book is purchased, the sales volume of this book should be + Quantity
				    Book b=new Book();
				    b.setName(itemEle[0]);
				    b.setSales(Integer.valueOf(itemEle[2]));
				    bookDao.editSales(b);

Effect display:

Note that the sales volume of proficient java is 30. The following operation can change the sales volume:

   After operation:

  End Lala

Posted by moonie on Sat, 09 Oct 2021 03:17:12 -0700