概念
前面通过多篇文章以完全实现了用户在网上蛋糕商城平台上的所有功能和操作,从本文开始,实现网站的后台管理功能的介绍和操作。
订单列表
想要进入后台管理系统,则登入的用户一定是管理员账号,这个账号和密码只有管理员才知道,因此不用担心顾客会登录后台管理系统。我们在数据库中已存储一个管理员账号和密码,可以进行使用。当通过管理员账号和密码登录成功后,head.jsp头部页面中会出现后台管理选项。
点击“后台管理”进入到后台管理系统首页
因此需要在web文件夹下创建admin文件夹,并在该文件夹下创建index.jsp页面,并且放入需要关联的js,css以及图片资源等等。
并且还需要单独创建分离出来的后台管理的头部页面
head.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="index.jsp">蛋糕店后台</a>
</div>
<div>
<ul class="nav navbar-nav">
<li ><a href="/admin/order_list">订单管理</a></li>
<li ><a href="/admin/user_list">客户管理</a></li>
<li ><a href="/admin/goods_list">商品管理</a></li>
<li ><a href="/admin/type_list">类目管理</a></li>
<li><a href="/user_logout">退出</a></li>
</ul>
</div>
</div>
</nav>
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<title>后台管理</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">
<jsp:include page="header.jsp"></jsp:include>
<br><br>
<div class="alert alert-success" role="alert">恭喜你! 登录成功了</div>
</div>
</body>
</html>
点击“订单管理”,向服务器发送请求,获取所有订单信息
因此需要在servlet包中创建AdminOrderListServlet类
package servlet;
import model.Page;
import service.OrderService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "admin_order_list",urlPatterns = "/admin/order_list")
public class AdminOrderListServlet extends HttpServlet {
private OrderService oService = new OrderService();
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int status = 0;
if(request.getParameter("status") != null) {
status=Integer.parseInt(request.getParameter("status") ) ;
}
request.setAttribute("status", status);
int pageNumber = 1;
if(request.getParameter("pageNumber") != null) {
try {
pageNumber=Integer.parseInt(request.getParameter("pageNumber") ) ;
}
catch (Exception e)
{
}
}
if(pageNumber<=0)
pageNumber=1;
Page p = oService.getOrderPage(status,pageNumber);
if(p.getTotalPage()==0)
{
p.setTotalPage(1);
p.setPageNumber(1);
}
else {
if(pageNumber>=p.getTotalPage()+1)
{
p = oService.getOrderPage(status,pageNumber);
}
}
request.setAttribute("p", p);
request.getRequestDispatcher("/admin/order_list.jsp").forward(request, response);
}
}
以上代码中,接收浏览器的请求后,判断用户是否传递状态参数,如果没有传递,则默认查询所有订单,如果有传递状态参数,根据状态参数来查询“已付款”,“未付款”,“配送中”,“已完成”等不同状态的订单,接着需要获得用户是否传递页码,如果没有默认为查询第一页的订单,并将状态参数和页码参数作为条件传递给订单业务逻辑层执行操作。
public Page getOrderPage(int status,int pageNumber) {
Page p = new Page();
p.setPageNumber(pageNumber);
int pageSize = 10;
int totalCount = 0;
try {
totalCount = oDao.getOrderCount(status);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.SetPageSizeAndTotalCount(pageSize, totalCount);
List list=null;
try {
list = oDao.selectOrderList(status, pageNumber, pageSize);
for(Order o :(List<Order>)list) {
List<OrderItem> l = oDao.selectAllItem(o.getId());
o.setItemList(l);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p.setList(list);
return p;
}
以上代码中,根据状态查询当前状态下的订单总数,接着根据状态,页码以及记录数作为条件查询订单信息
因此需要在OrderDao类中定义对应的执行方法
public int getOrderCount(int status) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "";
if(status==0) {
sql = "select count(*) from `order`";
return r.query(sql, new ScalarHandler<Long>()).intValue();
}else {
sql = "select count(*) from `order` where status=?";
return r.query(sql, new ScalarHandler<Long>(),status).intValue();
}
}
public List<Order> selectOrderList(int status, int pageNumber, int pageSize) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
if(status==0) {
String sql = "select o.id,o.total,o.amount,o.status,o.paytype,o.name,o.phone,o.address,o.datetime,u.username from `order` o,user u where o.user_id=u.id order by o.datetime desc limit ?,?";
return r.query(sql, new BeanListHandler<Order>(Order.class), (pageNumber-1)*pageSize,pageSize );
}else {
String sql = "select o.id,o.total,o.amount,o.status,o.paytype,o.name,o.phone,o.address,o.datetime,u.username from `order` o,user u where o.user_id=u.id and o.status=? order by o.datetime desc limit ?,?";
return r.query(sql, new BeanListHandler<Order>(Order.class),status, (pageNumber-1)*pageSize,pageSize );
}
}
最后将查询的信息发送给admin的order_list.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<title>订单列表</title>
<link rel="stylesheet" href="css/bootstrap.css"/>
</head>
<body>
<div class="container-fluid">
<jsp:include page="header.jsp"></jsp:include>
<br>
<ul role="tablist" class="nav nav-tabs">
<li <c:if test="${status==0 }">class="active"</c:if> role="presentation"><a href="/admin/order_list">全部订单</a></li>
<li <c:if test="${status==1 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=1">未付款</a></li>
<li <c:if test="${status==2 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=2">已付款</a></li>
<li <c:if test="${status==3 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=3">配送中</a></li>
<li <c:if test="${status==4 }">class="active"</c:if> role="presentation"><a href="/admin/order_list?status=4">已完成</a></li>
</ul>
<br>
<table class="table table-bordered table-hover">
<tr>
<th width="5%">ID</th>
<th width="5%">总价</th>
<th width="15%">商品详情</th>
<th width="20%">收货信息</th>
<th width="10%">订单状态</th>
<th width="10%">支付方式</th>
<th width="10%">下单用户</th>
<th width="10%">下单时间</th>
<th width="10%">操作</th>
</tr>
<c:forEach items="${p.list }" var="order">
<tr>
<td><p>${order.id }</p></td>
<td><p>${order.total }</p></td>
<td>
<c:forEach items="${order.itemList }" var="item">
<p>${item.goodsName }(${item.price }) x ${item.amount}</p>
</c:forEach>
</td>
<td>
<p>${order.name }</p>
<p>${order.phone }</p>
<p>${order.address }</p>
</td>
<td>
<p>
<c:if test="${order.status==2 }"><span style="color:red;">已付款</span></c:if>
<c:if test="${order.status==3 }"><span style="color:green;">已发货</span></c:if>
<c:if test="${order.status==4 }"><span style="color:black;">已完成</span></c:if>
</p>
</td>
<td>
<p>
<c:if test="${order.paytype==1 }">微信</c:if>
<c:if test="${order.paytype==2 }">支付宝</c:if>
<c:if test="${order.paytype==3 }">货到付款</c:if>
</p>
</td>
<td><p>${order.user.username }</p></td>
<td><p>${order.datetime }</p></td>
<td>
<c:if test="${order.status==2 }">
<a class="btn btn-success" href="/admin/order_status?id=${order.id }&status=3">发货</a>
</c:if>
<c:if test="${order.status==3 }">
<a class="btn btn-warning" href="/admin/order_status?id=${order.id }&status=4">完成</a>
</c:if>
<a class="btn btn-danger" href="/admin/order_delete?id=${order.id }&pageNumber=${p.pageNumber}&status=${status}">删除</a>
</td>
</tr>
</c:forEach>
</table>
<br>
<jsp:include page="/page.jsp">
<jsp:param value="/admin/order_list" name="url"/>
<jsp:param value="&status=${status}" name="param"/>
</jsp:include>
<br>
</div>
</body>
</html>
当点击发货按钮时,想服务器发送请求
因此在servlet包中创建AdminOrderStatusServlet类
package servlet;
import service.OrderService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "admin_order_status",urlPatterns = "/admin/order_status")
public class AdminOrderStatusServlet extends HttpServlet {
private OrderService oService = new OrderService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
int status = Integer.parseInt(request.getParameter("status"));
oService.updateStatus(id, status);
response.sendRedirect("/admin/order_list?status="+status);
}
}
以上代码中,接收到浏览器的请求,并接收订单编号以及状态参数作为执行条件发送给业务逻辑
public void updateStatus(int id,int status) {
try {
oDao.updateStatus(id, status);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
该方法将两个参数传递给dao层的OrderDao类执行
public void updateStatus(int id,int status) throws SQLException {
QueryRunner r = new QueryRunner(DataSourceUtils.getDataSource());
String sql ="update `order` set status=? where id = ?";
r.update(sql,status,id);
}
当状态更新成功后,则刷新订单管理页面。并且跳转至配送中状态
当点击“完成”按钮时,向服务器的AdminOrderStatusServlet类发送请求,修改状态即可。
删除订单
在订单管理功能中,不管处于任何状态,只要管理员点击删除按钮,则向服务器发出请求,执行删除当前订单操作。
因此需要在servlet包中创建AdminOrderDeleteServlet类完成删除订单操作
package servlet;
import service.OrderService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "admin_order_delete",urlPatterns = "/admin/order_delete")
public class AdminOrderDeleteServlet extends HttpServlet {
private OrderService oService = new OrderService();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
oService.delete(id);
request.getRequestDispatcher("/admin/order_list").forward(request, response);
}
}