Shop项目后台--3.查询所有订单

本文介绍了一个简单的订单管理系统的设计与实现过程,包括如何从左导航栏点击进入订单管理页面、通过Servlet查询所有订单信息,并将这些信息展示在JSP页面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析:

查询所有订单在admin/order/list.jsp显示。入口在admin/left.jsp中,点击“订单管理”,跳转到servlet业务。

在业务中,查询到所有的订单集合orderList通过request域,转发到admin/order/list.jsp.


步骤:

在left.jsp

1.修改点击“订单管理”所跳的地址

在AdminServlet

1.通过service层,dao层,查询得到orderList,把orderList传到request域并转发。

在list.jsp

1.导入jstl

2.在所在订单数据位置遍历orderList,得到order,通过order获取需要显示的数据。

3.序号以 varStatus=“vs”  ${vs.count} 遍历的状态显示每个订单的序号。


left.jsp

<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>菜单</title>
<link href="${pageContext.request.contextPath}/css/left.css" rel="stylesheet" type="text/css"/>
<link rel="StyleSheet" href="${pageContext.request.contextPath}/css/dtree.css" type="text/css" />
</head>
<body>
<table width="100" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="12"></td>
  </tr>
</table>
<table width="100%" border="0">
  <tr>
    <td>
<div class="dtree">

	<a href="javascript: d.openAll();">展开所有</a> | <a href="javascript: d.closeAll();">关闭所有</a>
	
	<script type="text/javascript" src="${pageContext.request.contextPath}/js/dtree.js"></script>
	<script type="text/javascript">
	
		d = new dTree('d');
		d.add('01',-1,'系统菜单树');
		d.add('0102','01','分类管理','','','mainFrame');
		d.add('010201','0102','分类管理','${pageContext.request.contextPath}/admin/category/list.jsp','','mainFrame');
		d.add('0104','01','商品管理');
		d.add('010401','0104','商品管理','${pageContext.request.contextPath}/admin/product/list.jsp','','mainFrame');
		d.add('0105','01','订单管理');
		d.add('010501','0105','订单管理','${pageContext.request.contextPath}/admin?method=findAllOrders','','mainFrame');
		document.write(d);
		
	</script>
</div>	</td>
  </tr>
</table>
</body>
</html>

AdminServlet

package com.itheima.web.servlet;

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

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 {
	
	//获取所有的订单集合
	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 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;
	}

}
AdminDao

package com.itheima.dao;

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

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

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;
	}

}
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
				});
				
			});
			
			
			
		</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 onmouseover="this.style.backgroundColor = 'white'"
										onmouseout="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"
											onclick="findOrderInfoByOid('fc86891e-5126-452e-932d-c4fe382ba73f')" />
										</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%">
						<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>
						<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>






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值