add<订单确认>
parent
42923c1a6a
commit
1a225dff58
|
@ -37,9 +37,11 @@ public class CartServlet extends HttpServlet {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "remove":
|
case "remove":
|
||||||
String removeId = req.getParameter("id");
|
try {
|
||||||
req.getSession().removeAttribute(removeId);
|
removeFromCart(req, resp);
|
||||||
resp.sendRedirect("/cart");
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
req.getRequestDispatcher("/WEB-INF/jsp/cart.jsp").forward(req,resp);
|
req.getRequestDispatcher("/WEB-INF/jsp/cart.jsp").forward(req,resp);
|
||||||
|
@ -47,6 +49,17 @@ public class CartServlet extends HttpServlet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void removeFromCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
||||||
|
User user = (User) req.getSession().getAttribute("user");
|
||||||
|
String id = req.getParameter("id");
|
||||||
|
cartService.removeFromCart(Integer.parseInt(id),user.getId());
|
||||||
|
try {
|
||||||
|
toCart(req, resp);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void addCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
private void addCart(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
||||||
String id = req.getParameter("id");
|
String id = req.getParameter("id");
|
||||||
User user = (User) req.getSession().getAttribute("user");
|
User user = (User) req.getSession().getAttribute("user");
|
||||||
|
|
|
@ -22,6 +22,13 @@ public class OrdersServlet extends HttpServlet {
|
||||||
case "all":
|
case "all":
|
||||||
ordersService.allOrder(req, resp);
|
ordersService.allOrder(req, resp);
|
||||||
break;
|
break;
|
||||||
|
case "add":
|
||||||
|
try {
|
||||||
|
ordersService.checkOrder(req, resp);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
break;
|
||||||
case "delete":
|
case "delete":
|
||||||
// ordersService.delete(req, resp);
|
// ordersService.delete(req, resp);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -8,4 +8,6 @@ public interface ICartService {
|
||||||
List<Computer> myCart(Integer userId) throws Exception;
|
List<Computer> myCart(Integer userId) throws Exception;
|
||||||
|
|
||||||
void addCart(int i, int id) throws Exception;
|
void addCart(int i, int id) throws Exception;
|
||||||
|
|
||||||
|
void removeFromCart(int i, int id) throws Exception;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,4 +9,6 @@ import java.util.List;
|
||||||
public interface IOrdersService {
|
public interface IOrdersService {
|
||||||
|
|
||||||
void allOrder(HttpServletRequest req, HttpServletResponse resp);
|
void allOrder(HttpServletRequest req, HttpServletResponse resp);
|
||||||
|
|
||||||
|
void checkOrder(HttpServletRequest req, HttpServletResponse resp) throws Exception;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,4 +19,9 @@ public class CartServiceImpl implements ICartService {
|
||||||
public void addCart(int productId, int userId) throws Exception {
|
public void addCart(int productId, int userId) throws Exception {
|
||||||
cartDao.addCart(userId, productId);
|
cartDao.addCart(userId, productId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeFromCart(int i, int id) throws Exception {
|
||||||
|
cartDao.deleteCart(id, i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package example.service.impl;
|
package example.service.impl;
|
||||||
|
|
||||||
|
import example.dao.CartDao;
|
||||||
import example.dao.OrdersDao;
|
import example.dao.OrdersDao;
|
||||||
|
import example.model.Computer;
|
||||||
import example.model.Orders;
|
import example.model.Orders;
|
||||||
|
import example.model.User;
|
||||||
import example.service.IOrdersService;
|
import example.service.IOrdersService;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
@ -12,6 +15,8 @@ public class OrdersServiceImpl implements IOrdersService {
|
||||||
|
|
||||||
OrdersDao ordersDao=new OrdersDao();
|
OrdersDao ordersDao=new OrdersDao();
|
||||||
|
|
||||||
|
CartDao cartDao = new CartDao();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void allOrder(HttpServletRequest req, HttpServletResponse resp) {
|
public void allOrder(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
List<Orders> allOrders = ordersDao.getAllOrders();
|
List<Orders> allOrders = ordersDao.getAllOrders();
|
||||||
|
@ -23,4 +28,22 @@ public class OrdersServiceImpl implements IOrdersService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void checkOrder(HttpServletRequest req, HttpServletResponse resp) throws Exception {
|
||||||
|
// 订单确认
|
||||||
|
User user = (User) req.getSession().getAttribute("user");
|
||||||
|
List<Computer> cart = cartDao.getCart(user.getId());
|
||||||
|
Orders orders = new Orders();
|
||||||
|
StringBuilder remark = new StringBuilder();
|
||||||
|
double total_price=0;
|
||||||
|
for (Computer computer : cart) {
|
||||||
|
total_price+=computer.getPrice();
|
||||||
|
remark.append("商品ID:").append(computer.getId()).append("\n").append("商品名称:").append(computer.getName()).append("\n").append("商品价格:").append(computer.getPrice()).append("\n");
|
||||||
|
}
|
||||||
|
orders.setRemark(remark.toString());
|
||||||
|
orders.setTotalPrice(total_price);
|
||||||
|
req.setAttribute("order",orders);
|
||||||
|
req.getRequestDispatcher("checkOrder.jsp").forward(req,resp);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>购物车</h1>
|
<h1>购物车</h1>
|
||||||
|
<a href="/">主页</a>
|
||||||
<table border="1">
|
<table border="1">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
@ -25,14 +26,19 @@
|
||||||
<td>${computer.name}</td>
|
<td>${computer.name}</td>
|
||||||
<td>${computer.price}</td>
|
<td>${computer.price}</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="/deleteComputer?id=${computer.id}">移出</a>
|
<a href="/cart?action=remove&id=${computer.id}">移出</a>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</c:forEach>
|
</c:forEach>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
<hr>
|
||||||
|
<a href="/orders?action=add">
|
||||||
|
<button>一键下单</button>
|
||||||
|
</a>
|
||||||
|
<br>
|
||||||
<br/>
|
<br/>
|
||||||
<br>
|
<br>
|
||||||
<a href="/">主页</a>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
<%@ page contentType="text/html; charset=UTF-8" language="java" %>
|
||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>订单确认</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>订单确认</h1>
|
||||||
|
<form action="/orders" method="post">
|
||||||
|
<label for="name">总金额: </label><br>
|
||||||
|
<input type="text" id="name" name="name" value="${order.totalPrice}" disabled><br><br>
|
||||||
|
<label for="price">地址: </label><br>
|
||||||
|
<input type="text" id="price" name="price" value="${order.address}" required><br><br>
|
||||||
|
<label for="stock">商品信息: </label><br>
|
||||||
|
<textarea id="stock" name="stock" rows="15" cols="30" disabled>${order.remark}</textarea><br><br>
|
||||||
|
<input type="submit" value="我已确认">
|
||||||
|
</form>
|
||||||
|
<br>
|
||||||
|
<a href="/">主页</a>
|
||||||
|
</body>
|
||||||
|
</html>
|
Reference in New Issue