提交订单
实现功能:
用户在购物车页面点击“提交订单”,向数据库中写入用户信息、购买的商品、数量等。
cart.jsp
为提交订单链接添加onclick属性并完成showOrder()方法。
<div style="text-align:right;margin-top:10px;margin-bottom:10px;">
<a href="#" id="clear" class="clear" onclick="clearCart()">清空购物车</a>
<a href="#" onclick="showOrder()">
<input type="submit" width="100" value="提交订单" name="submit" border="0" style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0);
height:35px;width:100px;color:white;">
</a>
</div>
OrderItem.java
package com.oracle.bean;
public class OrderItem {
private String itemid;
private int count;
private double subtotal;
private String pid;
private String oid;
private Product pro;
public Product getPro() {
return pro;
}
public void setPro(Product pro) {
this.pro = pro;
}
public OrderItem() {
super();
// TODO Auto-generated constructor stub
}
public OrderItem(String itemid, int count, double subtotal, String pid, String oid) {
super();
this.itemid = itemid;
this.count = count;
this.subtotal = subtotal;
this.pid = pid;
this.oid = oid;
}
public String getItemid() {
return itemid;
}
public void setItemid(String itemid) {
this.itemid = itemid;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getSubtotal() {
return subtotal;
}
public void setSubtotal(double subtotal) {
this.subtotal = subtotal;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
}
Order.java
package com.oracle.bean;
import java.util.ArrayList;
import java.util.List;
public class Order {
private String oid;
private String ordertime;
private double total;
private int state;
private String address;
private String name;
private String telephone;
private String uid;
private List<OrderItem> list = new ArrayList<>();
public Order() {
super();
// TODO Auto-generated constructor stub
}
public Order(String oid, String ordertime, double total, int state, String address, String name, String telephone,
String uid, List<OrderItem> list) {
super();
this.oid = oid;
this.ordertime = ordertime;
this.total = total;
this.state = state;
this.address = address;
this.name = name;
this.telephone = telephone;
this.uid = uid;
this.list = list;
}
public String getOid() {
return oid;
}
public void setOid(String oid) {
this.oid = oid;
}
public String getOrdertime() {
return ordertime;
}
public void setOrdertime(String ordertime) {
this.ordertime = ordertime;
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public List<OrderItem> getList() {
return list;
}
public void setList(List<OrderItem> list) {
this.list = list;
}
}
ProductServlet.java
在ProductServlet的doGet方法中添加showOrder的判断并完成showOrder方法。
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
if("getProById".equals(method)) {
getProById(request, response);
}else if("findListByCate".equals(method)) {
findListByCate(request, response);
}else if("cart".equals(method)) {
cart(request, response);
}else if("delCart".equals(method)) {
delCart(request, response);
}else if("clearCart".equals(method)) {
clearCart(request, response);
}else if("showOrder".equals(method)) {
showOrder(request, response);
}
}
public void showOrder(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
HttpSession session = request.getSession();
Cart cart = (Cart)session.getAttribute("cart");
User user = (User)session.getAttribute("user");
if(user==null) {
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
//订单表的id
String oid = UUID.randomUUID().toString();
//向订单项中封装数据
OrderItem oi = new OrderItem();
Map<String, CartItem> map = cart.getMap();
Product p = new Product();
ProductService ps = new ProductService();
ArrayList<OrderItem> arr = new ArrayList<>();
for(String s:map.keySet()) {
String id = UUID.randomUUID().toString();
oi.setItemid(id);
CartItem ci = map.get(s);
oi.setCount(ci.getCount());
oi.setPid(ci.getPro().getPid());
Product pro = ps.getProById(ci.getPro().getPid());
oi.setPro(pro);
oi.setSubtotal(ci.getSubTotal());
oi.setOid(oid);
arr.add(oi);
}
//向订单中封装数据
Order od = new Order();
od.setOid(oid);
//获取当前时间
DateFormat df = DateFormat.getDateInstance();
String dateTime = df.format(new Date());
od.setOrdertime(dateTime);
od.setState(0);
od.setTotal(cart.getSubMoney());
od.setUid(user.getUid());
od.setList(arr);
//向service发送请求
ps.insertOrderAndItem(od);
//把订单放入session
session.setAttribute("order", od);
//跳转到order_info.jsp
request.getRequestDispatcher("order_info.jsp").forward(request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ProductService.java
在ProductService中添加insertOrderAndItem方法。
public void insertOrderAndItem(Order od) {
// TODO Auto-generated method stub
try {
//开启事务
DataSourceUtils.startTransaction();
ProductDao pd = new ProductDao();
pd.insertOrder(od);
List<OrderItem> list = od.getList();
for(OrderItem o:list) {
pd.insertItem(o);
}
DataSourceUtils.commitAndRelease();
} catch (Exception e) {
// TODO Auto-generated catch block
try {
DataSourceUtils.rollback();
e.printStackTrace();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
ProductDao.java
在ProductDao中添加insertOrder和insertItem方法。
public void insertOrder(Order od) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner();
String sql = "insert into orders(oid,ordertime,total,state,uid)values(?,?,?,?,?)";
qr.update(DataSourceUtils.getConnection(), sql, od.getOid(),od.getOrdertime(),od.getTotal(),od.getState(),od.getUid());
}
public void insertItem(OrderItem o) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr = new QueryRunner();
String sql = "insert into orderitem(itemid,count,subtotal,pid,oid)values(?,?,?,?,?)";
qr.update(DataSourceUtils.getConnection(), sql, o.getItemid(), o.getCount(), o.getSubtotal(), o.getPid(), o.getOid());
}
order_info.jsp
在order_info页面解析订单信息。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员登录</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />
<style>
body {
margin-top: 20px;
margin: 0 auto;
}
.carousel-inner .item img {
width: 100%;
height: 300px;
}
</style>
</head>
<body>
<!-- 引入header.jsp -->
<jsp:include page="/header.jsp"></jsp:include>
<div class="container">
<div class="row">
<div style="margin: 0 auto; margin-top: 10px; width: 950px;">
<strong>订单详情</strong>
<table class="table table-bordered">
<tbody>
<tr class="warning">
<th colspan="5">订单编号:9005</th>
</tr>
<tr class="warning">
<th>图片</th>
<th>商品</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
</tr>
<c:forEach items="${order.list}" var="oi">
<tr class="active">
<td width="60" width="40%"><input type="hidden" name="id"
value="22"> <img src="${pageContext.request.contextPath}/${oi.pro.pimage}" width="70"
height="60"></td>
<td width="30%"><a target="_blank">${oi.pro.pname}</a></td>
<td width="20%">¥${oi.pro.shop_price}</td>
<td width="10%">${oi.count}</td>
<td width="15%"><span class="subtotal">¥${oi.subtotal}</span></td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div style="text-align: right; margin-right: 120px;">
商品金额: <strong style="color: #ff6600;">¥${order.total}元</strong>
</div>
</div>
<div>
<hr />
<form class="form-horizontal"
style="margin-top: 5px; margin-left: 150px;">
<div class="form-group">
<label for="username" class="col-sm-1 control-label">地址</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="username"
placeholder="请输入收货地址">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-1 control-label">收货人</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="inputPassword3"
placeholder="请输收货人">
</div>
</div>
<div class="form-group">
<label for="confirmpwd" class="col-sm-1 control-label">电话</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="confirmpwd"
placeholder="请输入联系方式">
</div>
</div>
</form>
<hr />
<div style="margin-top: 5px; margin-left: 150px;">
<hr />
<p style="text-align: right; margin-right: 100px;">
<a href="javascript:document.getElementById('orderForm').submit();">
<img src="./images/finalbutton.gif" width="204" height="51"
border="0" />
</a>
</p>
<hr />
</div>
</div>
</div>
<!-- 引入footer.jsp -->
<jsp:include page="/footer.jsp"></jsp:include>
</body>
</html>