概述
一.本期目标
1、解决上期内容乱码
2、结算功能
二.名字乱码
分析乱码问题:
传递到后台的值,乱码的原因可能有两种
1、前台-->浏览器那一端就已经产生了乱码
2、后端-->在子控制器接受前端传递到后台的过程中出现乱码
解决思路:
前端的代码格式是UTF-8,后台是从mvc进来之后,会进入编码过滤器。
UTF-8----- ISO-8859-1就会进行编码错误
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;/**
* 中文乱码处理
*
*/
@WebFilter("*.action")
public class EncodingFiter implements Filter {private String encoding = "UTF-8";// 默认字符集
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;// 中文处理必须放到 chain.doFilter(request, response)方法前面
res.setContentType("text/html;charset=" + this.encoding);
if (req.getMethod().equalsIgnoreCase("post")) {
req.setCharacterEncoding(this.encoding);
}//删除这一段
/*else {
Map map = req.getParameterMap();// 保存所有参数名=参数值(数组)的Map集合
Set set = map.keySet();// 取出所有参数名
Iterator it = set.iterator();
while (it.hasNext()) {
String name = (String) it.next();
String[] values = (String[]) map.get(name);// 取出参数值[注:参数值为一个数组]
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");// 读取web.xml文件中配置的字符集
if (null != s && !s.trim().equals("")) {
this.encoding = s.trim();
}
}}
展示效果:
三、结算功能 (需要用到订单表和订单项表)
需求:1.数据入订表单 2.数据入订单项 3.下单结束后,要将商品的销售数量发生改变
第一步:首先要将关于订单表的属性创建一个实体类
Order 订单表
package com.zking.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.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 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"});
}
/**
* 按照订单时间倒叙查询
* @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);
}
}
订单项 同上:订单表的方法步骤
第一步:OrderItem实体类
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 + "]";
}
}
第二步:OrderItemDao
package com.zking.dao;
import com.zking.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"});
}
}
第三步:子控制器shoppingAction(订单入库以及订单项入库)
package com.zking.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.dao.BookDao;
import com.zking.dao.OrderDao;
import com.zking.dao.OrderItemDao;
import com.zking.entity.Book;
import com.zking.entity.Order;
import com.zking.entity.OrderItem;
import com.zking.entity.User;
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;
public class ShoppingAction extends ActionSupport implements ModelDriver<ShoppingVo>{
//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;
}
public void createOrder(HttpServletRequest req, HttpServletResponse resp) {
/**
* 1.数据入订表单
* 2.数据入订单项
* 3.下单结束后,要将商品的销售数量发生改变
*/
User cuser=(User) req.getSession().getAttribute("cuser");
if(cuser == null) {
return;
}
//组装数据
Order order=new Order();
order.setAddress(vo.getAddress());
order.setConsignee(vo.getConsignee());
//订单价格总计
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);
//需要设置当前订单项是属于哪个订单的,思路是查询出最新订单的ID
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(oid);
oi.setOid(newest.getId());
oi.setQuantity(Integer.valueOf(itemEle[2]));
orderItemDao.add(oi);
//当前书籍被购买,那么这本书的销量就应该+Quantity
Book b=new Book();
b.setName(itemEle[0]);
b.setSales(Integer.valueOf(itemEle[2]));
bookDao.editSales(b);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
展示效果:
t_easyui_order:订单表结果
t_easyui_orderItem:订单项表结果
四.销量的改变
BookDao:根据名字进行销量的修改
/**
* 根据书籍的名称修改销量
* @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:
//当前书籍被购买,那么这本书的销量就应该+Quantity
Book b=new Book();
b.setName(itemEle[0]);
b.setSales(Integer.valueOf(itemEle[2]));
bookDao.editSales(b);
销量添加之前:
销量添加之后:
最后
以上就是怕黑羊为你收集整理的购物车下之结算功能的全部内容,希望文章能够帮你解决购物车下之结算功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复