当前位置: 首页 > 工具软件 > Shop > 使用案例 >

Shop项目后台--4.所有订单的订单详情/admin/order/list.jsp

苏乐童
2023-12-01

contrl+H 可以快速查找全部工程下,你所提高关键字的位置

分析:

1、在list.jsp订单详情需要弹出层插件,插件代码有表示,触发按钮,弹出的内容,关闭按钮。

AdminServlet

2、根据弹出层插件代码,找到入口,“订单详情”。点击后触发事件,进行ajax异步查询,目的是得到,该订单下的所有订单项与商品详细信息,需要携带oid查询。查询得到的结果要用List<Map<String,Object>> ,其实Map<String,Object>就是一个对象的实体,然后把多个对象实体装在List集合中。List<Map<String,Object>> 本质与List<Product>结构一样。

根据业务需要进行多表查询product、orderitem,多表查询要找到表与表之间的关系。sql语句为:

select p.pimage, p.pname, p.shop_price, i.count, i.subtotal 

from product p, orderitem i 

where p.pid=i.pid and i.oid=oid 

3、查询得到的是一个Map类型的集合 List<Map<String,Object>> mapList

4、利用Gson 把mapList转为json格式字符串,然后返回给ajax

list.jsp

5、通过ajax返回的数据开始拼字符串,动态显示查询到的数据。(最繁琐的拼串)“订单详情”弹出层里所有需要的数据,图片,名字,价格,件数,小计,订单编号。需要把这些写死的东西删除,遍历ajax返回的data数据获取活的数据,拼接字符串。

6、为了用户体现,读取数据库,时候ajax返回可能需要几秒钟,为了这几秒钟不出现“白板页面”,需要添加一个<div>专门显示等待的图片,当ajax有数据返回的时候该,隐藏该<div> 当点击下个"订单详情"清空content,与订单编号内容,同时把该<div>显示。


步骤:

在list.jsp页面

0、编写"订单详情"的单击事件,携带oid 注意携带的数据加上单引号.通过ajax查询数据库,获取所需的数据

在AdminServlet

1、获取oid,根据oid、p.pid=i.pid查询product、orderitem表、返回List<Map<String,Object> mapList

2、把mapList转为json格式字符串,传回给ajax

在list.jsp

3、把返回的数据,进行拼串content,图片、名字、价格、件数、小计。

4、加上友好的图片等待体验。设置默认显示等待图片<div> 显示,ajax成功返回数据时候隐藏。点击“订单详情”事件时,把整个弹出层清空,并显示等待图片<div>


list.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<HTML>
	<HEAD>
		<meta http-equiv="Content-Language" content="zh-cn">
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<link href="${pageContext.request.contextPath}/css/Style1.css" rel="stylesheet" type="text/css" />
		<script language="javascript" src="${pageContext.request.contextPath}/js/public.js"></script>
		
		<!-- 弹出层插件 -->
		<link href="${pageContext.request.contextPath}/css/popup_layer.css" type="text/css" rel="stylesheet"/>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script>
		<script type="text/javascript" src="${pageContext.request.contextPath}/js/popup_layer.js"></script>		
		<!-- 调用插件弹出层的方法 -->
		<script type="text/javascript">
			$(function(){
				//弹出层插件调用
				new PopupLayer({
					trigger:".clickedElement",//点击哪个地方,触发弹出层
					popupBlk:"#showDiv",//触发后显示哪些页面内容
					closeBtn:"#closeBtn",//关闭弹出层的按钮
					useOverlay:true
				});
				
			});
			
			//点击按钮查询某个订单详情
			function findOrderInfoByOid(oid){
				//清理上一次显示的内容覆盖
				$("#showDivTab").html("");
				$("#shodDivOid").html("");
				$("#loading").css("display","block");
				//ajax异步访问数据
				$.post(
					//1.执行的url
					"${pageContext.request.contextPath}/admin?method=findOrderInfoByOid",
					//2.传递的数据,数据以json格式传递
					{"oid":oid},
					//3.返回数据后执行的函数
					function(data){
						//隐藏加载图片
						$("#loading").css("display","none");
						//拼串,双印变为单引
						var content = "<tr id='showTableTitle'><th width='20%'>图片</th><th width='25%'>商品</th><th width='20%'>价格</th><th width='15%'>数量</th><th width='20%'>小计</th></tr>";
						//遍历data,开始拼接content
						for(var i=0;i<data.length;i++){
							content+="<tr style='text-align: center;'>"+
										"<td>"+
											"<img src='${pageContext.request.contextPath }/"+data[i].pimage+"' width='70' height='60'>"+
										"</td>"+
										"<td><a target='_blank'>"+data[i].pname+"</a></td>"+
										"<td>¥"+data[i].shop_price+"</td>"+
										"<td>"+data[i].count+"</td>"+
										"<td><span class='subtotal'>¥"+data[i].subtotal+"</span></td>"+
									"</tr>";
						}
						//把拼好的content放到showDivTab
						$("#showDivTab").html(content);
						//把订单编号改为活的
						$("#shodDivOid").html(oid);
					},
					//4.返回数据的格式
					"json"
				);
			}
			
			
			
		</script>
		
	</HEAD>
	<body>
	
		<form id="Form1" name="Form1" action="${pageContext.request.contextPath}/user/list.jsp" method="post">
			<table cellSpacing="1" cellPadding="0" width="100%" align="center" bgColor="#f5fafe" border="0">
				<TBODY>
					<tr>
						<td class="ta_01" align="center" bgColor="#afd1f3">
							<strong>订单列表</strong>
						</TD>
					</tr>
					
					<tr>
						<td class="ta_01" align="center" bgColor="#f5fafe">
							<table cellspacing="0" cellpadding="1" rules="all"
								bordercolor="gray" border="1" id="DataGrid1"
								style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
								<tr
									style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">

									<td align="center" width="10%">
										序号
									</td>
									<td align="center" width="10%">
										订单编号
									</td>
									<td align="center" width="10%">
										订单金额
									</td>
									<td align="center" width="10%">
										收货人
									</td>
									<td align="center" width="10%">
										订单状态
									</td>
									<td align="center" width="50%">
										订单详情
									</td>
								</tr>
								<!-- 动态显示order所有信息 -->
								<c:forEach items="${orderList }" var="order" varStatus="vs">
									<tr οnmοuseοver="this.style.backgroundColor = 'white'"
										οnmοuseοut="this.style.backgroundColor = '#F5FAFE';">
										<td style="CURSOR: hand; HEIGHT: 22px" align="center"
											width="18%">${vs.count }</td>
										<td style="CURSOR: hand; HEIGHT: 22px" align="center"
											width="17%">${order.oid }</td>
										<td style="CURSOR: hand; HEIGHT: 22px" align="center"
											width="17%">${order.total }</td>
										<td style="CURSOR: hand; HEIGHT: 22px" align="center"
											width="17%">${order.name }</td>
										<td style="CURSOR: hand; HEIGHT: 22px" align="center"
											width="17%">${order.state==0?"未付款":"已付款" }</td>
										<td align="center" style="HEIGHT: 22px"><input
											type="button" value="订单详情" class="clickedElement"
											οnclick="findOrderInfoByOid('${order.oid}')" />
										</td>
									</tr>

							</c:forEach>
								
							</table>
						</td>
					</tr>
					
				</TBODY>
			</table>
		</form>
		
		<!-- 弹出层 HaoHao added -->
        <div id="showDiv" class="blk" style="display:none;">
            <div class="main">
                <h2>订单编号:<span id="shodDivOid" style="font-size: 13px;color: #999">123456789</span></h2>
                <a href="javascript:void(0);" id="closeBtn" class="closeBtn">关闭</a>
                <div id="loading" style="padding-top:30px;text-align: center;">
                <!-- 等待图片 -->
                	<img alt="" src="${pageContext.request.contextPath }/images/loading.gif">
                </div>
				<div style="padding:20px;">
					<table id="showDivTab" style="width:100%">
						<!-- 在这里开始拼串 -->
						<!-- 把固定的这个头,先移去拼串的开始content -->
						<!-- <tr id='showTableTitle'>
							<th width='20%'>图片</th>
							<th width='25%'>商品</th>
							<th width='20%'>价格</th>
							<th width='15%'>数量</th>
							<th width='20%'>小计</th>
						</tr> -->
						<!-- 循环拼串部分 -->
						<%-- <tr style='text-align: center;'>
							<td>
								<img src='${pageContext.request.contextPath }/products/1/c_0014' width='70' height='60'>
							</td>
							<td><a target='_blank'>电视机</a></td>
							<td>¥3000</td>
							<td>3</td>
							<td><span class='subtotal'>¥9000</span></td>
						</tr> --%>
						
						
					</table>
				</div>
            </div>
            
        </div>
		
		
	</body>
</HTML>

AdminServlet

package com.itheima.web.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.itheima.domain.Category;
import com.itheima.domain.Order;
import com.itheima.service.AdminService;

public class AdminServlet extends BaseServlet {
	
	//根据订单id查询订单项与商品
	public void findOrderInfoByOid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//ajax访问,目的:获取该订单下的订单项
		//获得oid
		String oid = request.getParameter("oid");
		//根据oid查询product表orderitem表
		AdminService service = new AdminService();
		List<Map<String,Object>> mapList =  service.findOrderInfoByOid(oid);
		//把mapList转为json的字符串格式
		Gson gson = new Gson();
		String json = gson.toJson(mapList);
		//可以直接打印到控制台查看格式,以下为例子,返回一个数组,数组里装有json格式对象
		/*[{"pimage":"products/1/c_0048.jpg","shop_price":1888.0,"pname":"Apple iPad mini 2 ME279CH/A","subtotal":1888.0,"count":1},{"pimage":"products/1/c_0050.jpg","shop_price":2299.0,"pname":"Apple iPad Air 2 MGLW2CH/A","subtotal":2299.0,"count":1},{"pimage":"products/1/c_0032.jpg","shop_price":6688.0,"pname":"Apple MacBook Air MJVE2CH/A 13.3英寸","subtotal":6688.0,"count":1},{"pimage":"products/1/c_0026.jpg","shop_price":6088.0,"pname":"Apple iPhone 6s (A1700) 64G 玫瑰金色","subtotal":6088.0,"count":1},{"pimage":"products/1/c_0015.jpg","shop_price":4288.0,"pname":"Apple iPhone 6 (A1586)","subtotal":4288.0,"count":1},{"pimage":"products/1/c_0039.jpg","shop_price":10288.0,"pname":"Apple 配备 Retina 显示屏的 MacBook","subtotal":10288.0,"count":1}]*/
		System.out.println(json);
		//把json字符串格式的mapList传回给ajax,记得设置UTF-8格式
		response.setContentType("text/json;charset=UTF-8");
		response.getWriter().write(json);
	}
	
	//获取所有的订单集合
	public void findAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//目的:获取所有的订单,存到list集合 orderList
		AdminService service = new AdminService();
		List<Order> orderList = service.findAllOrders();
		//把orderList传到request域
		request.setAttribute("orderList", orderList);
		//转发到list.jsp显示
		request.getRequestDispatcher("/admin/order/list.jsp").forward(request, response);
		
	}
	
	//获取商品的所有分类
	public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//提供一个List<Category> 转成jason字符串
		AdminService service = new AdminService();
		List<Category> categoryList =  service.findAllCategory();
		//把categoryList转为jason字符串
		Gson gson = new Gson();
		String json = gson.toJson(categoryList);
		//因为可能有中文,需要指定编码
		response.setContentType("text/json;charset=UTF-8");
		//把json字符串传回给ajax
		response.getWriter().write(json);
		
	}

}
AdminService

package com.itheima.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.itheima.dao.AdminDao;
import com.itheima.domain.Category;
import com.itheima.domain.Order;
import com.itheima.domain.Product;

public class AdminService {

	//获取商品所有分类列表
	public List<Category> findAllCategory() {
		AdminDao dao = new AdminDao();
		List<Category> categoryList=null;
		try {
			categoryList = dao.findAllCategory();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return categoryList;
	}

	//把商品存到数据库
	public void saveProduct(Product product) {
		AdminDao dao = new AdminDao();
		try {
			dao.saveProduct(product);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	//获取所有的订单
	public List<Order> findAllOrders() {
		AdminDao dao = new AdminDao();
		List<Order> orderList =null;
		try {
			orderList = dao.findAllOrders();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderList;
	}

	//根据oid多表查询product,orderitem
	public List<Map<String, Object>> findOrderInfoByOid(String oid) {
		AdminDao dao = new AdminDao();
		List<Map<String, Object>> mapList =null;
		try {
			mapList = dao.findOrderInfoByOid(oid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mapList;
	}

}
AdminDao

package com.itheima.dao;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.itheima.domain.Category;
import com.itheima.domain.Order;
import com.itheima.domain.Product;
import com.itheima.utils.DataSourceUtils;

public class AdminDao {

	//获取所有商品分类列表集合
	public List<Category> findAllCategory() throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select * from category";
		List<Category> query = runner.query(sql, new BeanListHandler<Category>(Category.class));
		return query;
		
	}

	//把商品存到数据库
	public void saveProduct(Product product) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String dateStr = format.format(product.getPdate());
		runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),
				product.getShop_price(),product.getPimage(),dateStr,
				product.getIs_hot(),product.getPdesc(),product.getPflag(),product.getCategory().getCid());
	}

	//获取所有的订单
	public List<Order> findAllOrders() throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql ="select * from orders";
		List<Order> query = runner.query(sql, new BeanListHandler<Order>(Order.class));
		return query;
	}

	//根据oid查询product,orderitem多表查询
	public List<Map<String, Object>> findOrderInfoByOid(String oid) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select p.pimage,p.pname,p.shop_price,i.count,i.subtotal "+
					"from product p,orderitem i "+
					"where p.pid=i.pid and i.oid=? ";
		List<Map<String, Object>> query = runner.query(sql, new MapListHandler(), oid);
		return query;
	}

}





 类似资料: