package cn.itcast.estore.web.servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.itcast.estore.domain.Order;
import cn.itcast.estore.domain.User;
import cn.itcast.estore.service.OrderService;
public class ShowOrderServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.获取当前用户
User user = (User) request.getSession().getAttribute("existUser");
// 2.调用service中查看订单方法
OrderService service = new OrderService();
List<Order> lists = null;
try {
lists = service.findOrder(user);
if (lists != null) {
// 3.显示订单信息
request.setAttribute("orders", lists);
request.getRequestDispatcher("/showOrder.jsp").forward(request,
response);
return;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>订单列表</title>
<script type="text/javascript">
function findProductByOrder_id(order_id) {
var btn = document.getElementById("btn_" + order_id);
var div = document.getElementById("div_" + order_id);
//1.得到xmlHttpRequest对象
var xmlhttp = null;
if (window.XMLHttpRequest) { // code for all new browsers
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {// code for IE5 and IE6
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
if (btn.value != "关闭") {
div.style.display = "block";
//2.注册回调函数
xmlhttp.onreadystatechange = function() {
//5.在回调函数里处理返回数据
//处理响应数据 当信息全部返回,并且成功
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var jsonPs = eval("(" + xmlhttp.responseText + ")");
String
table = "<table border='2px' align='center'><tr><td>商品名称</td><td>类别</td><td>价钱</td><td>描述</td></tr>";
for ( var i = 0; i < jsonPs.length; i++) {
table += "<tr><td>" + jsonPs[i].pname + "</td><td>"
+ jsonPs[i].category + "</td><td>"
+ jsonPs[i].price + "</td><td>"
+ jsonPs[i].desc + "</td></tr>";
}
table += "</table>";
div.innerHTML = table;
}
}
//3.设置连接方式,建立服务器连接
xmlhttp.open("get",
"${pageContext.request.contextPath}/findProductByOrderId?id="
+ order_id);
//4.发送请求
xmlhttp.send(null);
btn.value = "关闭";
} else {
btn.value = "查看";
div.style.display = "none";
}
};
</script>
</head>
<body>
<table border="2px" align="center">
<tr><td colspan="8"><a href="${pageContext.request.contextPath}/downLoad">下载销售榜单</a></td></tr>
<tr>
<td>订单编号</td>
<td>订单总价</td>
<td>下单时间</td>
<td>收货地址</td>
<td>收货人</td>
<td>支付状态</td>
<td>订单详情</td>
<td>删除订单</td>
</tr>
<c:forEach items="${orders}" var="order">
<tr>
<td>${order.id}</td>
<td>${order.money}</td>
<td>${order.ordertime}</td>
<td>${order.receiverinfo}</td>
<td>${order.username}【${order.nickname}】</td>
<td><c:if test="${order.paystate==0}">
<a
href="${pageContext.request.contextPath}/pay.jsp?orderid=${order.id}&money=${order.money}">未支付</a>
</c:if> <c:if test="${order.paystate!=0}">
已支付
</c:if></td>
<td><input type="button" value="查看" id="btn_${order.id}"
οnclick="findProductByOrder_id('${order.id}');">
<div id="div_${order.id}"></div></td>
<td><c:if test="${order.paystate==0}">
<a
href="${pageContext.request.contextPath}/cancelOderById?id=${order.id}">删除</a>
</c:if> <c:if test="${order.paystate!=0}">
删除
</c:if></td>
</tr>
</c:forEach>
</table>
</body>
</html>
来源:CSDN
作者:live801
链接:https://blog.csdn.net/live801/article/details/103951387