目标效果:
目标:
1,解决上节课乱码问题
2,结算购物车成功后,购物车清空(结算成功,生成订单,订单项与书籍销量的改变)
一,解决乱码问题
1.1 原因:
1.2 效果util包中的EncodingFiter类(注掉不必要的字段即可)
效果展示:
二,结算购物车
1.1 建立Order与Orderitem实体类
package com.zking.entity; import java.util.Date; //订单实体类 public class Oeder { 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 "Oeder [id=" + id + ", uid=" + uid + ", orderTime=" + orderTime + ", consignee=" + consignee + ", phone=" + phone + ", postalcode=" + postalcode + ", address=" + address + ", sendType=" + sendType + ", sendTime=" + sendTime + ", orderPrice=" + orderPrice + ", orderState=" + orderState + "]"; } }
package com.zking.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.2 OrderDao与OrderItemDao
package com.zking.dao; import java.util.List; import com.zking.entity.Order; import com.zking.util.BaseDao; import com.zking.util.PageBean; //订单 public class OrderDao extends BaseDao<Order>{ /** * 购物车结算生成订单 */ public void add(Order order) throws Exception { String sql = " insert into t_easyui_order(uid,orderTime,consignee,phone,postalcode,address,sendType,sendTime,orderPrice,orderState) values(?,now(),?,?,?,?,?,?,?,?) "; super.executeUpdate(sql, order, new String[] {"uid","consignee","phone","postalcode","address","sendType","sendTime","orderPrice","orderState"}); } /** * 按照订单时间倒序查询(查询出最新订单的id,根据id知道订单中有哪些订单项) */ 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); } }
package com.zking.dao; import com.zking.entity.OrderItem; import com.zking.util.BaseDao; //订单项 public class OrderItemDao extends BaseDao<OrderItem>{ /** * 购物车结算生成订单项 */ public void add(OrderItem orderItem) throws Exception { String sql = " insert into t_easyui_orderitem(oid,bid,quantity) values (?,?,?) "; super.executeUpdate(sql, orderItem, new String[] {"oid","bid","quantity"}); } }
2.3 ShoppingVoAction
购物车结算思路: 1、数据入订单表 2、数据入订单项表 3、下单结束后,要将商品的销售数量发生改变
// 购物车结算 public void createOrder(HttpServletRequest req, HttpServletResponse resp) { //获取用户id(uid) User cuser = (User) req.getSession().getAttribute("cuser"); //判断是否登录 if(cuser==null) { return; } //组装数据 Order order=new Order(); order.setAddress(shoppingVo.getAddress()); order.setConsignee(shoppingVo.getConsignee()); //订单价格总计 float orderPrice=0; String pageStr = shoppingVo.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(shoppingVo.getPhone()); order.setPostalcode(shoppingVo.getPostalcode()); order.setSendType(shoppingVo.getSendType()); order.setUid(cuser.getId()); try { orderDao.add(order); //取最新的一条订单 Order newest = orderDao.list(null, null).get(0); if(StringUtils.isNotBlank(pageStr)&&pageStr.length()>1) { for (String item:pageStr.substring(1).split(",")) { OrderItem orderItem=new OrderItem(); String[] itemEle = item.split("-"); orderItem.setBid(itemEle[0]); orderItem.setOid(newest.getId()); orderItem.setQuantity(Integer.valueOf(itemEle[2])); orderItemdDao.add(orderItem); //修改销量 Book book=new Book(); book.setName(itemEle[0]); book.setSales(Integer.valueOf(itemEle[2])); bookDao.editSales(book); } } } catch (Exception e) { e.printStackTrace(); } }
2.4 前台js代码
$(".order_Create").click(function () { var consignee = $("#consignee").val(); var phone = $("#phone").val(); var postalcode = $("#postalcode").val(); var address = $("#address").val(); var sendType = $("#sendType").val(); var single_tr = ""; $(".table tr:gt(0)").not(":last").each(function (index, node) { console.log(index); single_tr = single_tr + "," + $(this).find("th").eq(0).html() + "-" + $(this).find("td").eq(0).html() + "-" + $(this).find("td").eq(1).find("input").val() + "-" + $(this).find("td").eq(2).html(); }) console.log(single_tr.substring(1)); var param = "consignee=" + consignee + "&phone=" + phone + "&postalcode=" + postalcode + "&address=" + address + "&sendType=" + sendType + "&pageStr=" + single_tr; $.ajax({ url: '${pageContext.request.contextPath}/shopping.action?methodName=createOrder', type: "POST", data: param, success: function (data) { var consignee = $("#consignee").val(""); var phone = $("#phone").val(""); var postalcode = $("#postalcode").val(""); var address = $("#address").val(""); var sendType = $("#sendType").val(""); $('.shop-modal').addClass('modal'); $(".table tr:gt(0)").not(":last").remove(); $.ajax({ url: '${pageContext.request.contextPath}/shopping.action?methodName=clear', success: function (data) { <%--location.href = '${pageContext.request.contextPath}/shopping.action?methodName=list';--%> } }); } }); });
效果展示:
![]()
OK ! 到这就结束了,希望能帮到你!!!