一、后台管理系统概述
后台管理系统的页面框架是通过标签来组织的
二、商品管理模块
2.1.商品管理模块简介
传智书城中的商品管理是指对图书信息的管理,比如图书名、图书价格、图书分类等,通过后台系统中的商品管理模块可以实现图书信息在前台网站上的动态展示。后台商品管理模块的功能结构如下图所示
2.2.实现查询功能
/**
* 后台系统
* 查询所有商品信息的servlet
*/
public class ListProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1.创建service层的对象
ProductService service = new ProductService();
try {
// 2.调用service层用于查询所有商品的方法
List<Product> ps = service.listAll();
// 3.将查询出的所有商品放进request域中
request.setAttribute("ps", ps);
// 4.重定向到list.jsp页面
request.getRequestDispatcher("/admin/products/list.jsp").forward(
request, response);
return;
} catch (ListProductException e) {
e.printStackTrace();
response.getWriter().write(e.getMessage());
return;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class ProductDao {
// 查找所有商品
public List<Product> listAll() throws SQLException {
String sql = "select * from products";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new BeanListHandler<Product>(Product.class));
}
}
2.3.实现添加商品信息功能
/**
* 后台系统
* 用于添加商品的servlet
*/
public class AddProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建javaBean,将上传数据封装.
Product p = new Product();
Map<String, String> map = new HashMap<String, String>();
// 封装商品id
map.put("id", IdUtils.getUUID());
DiskFileItemFactory dfif = new DiskFileItemFactory();
// 设置临时文件存储位置
dfif.setRepository(new File(this.getServletContext().getRealPath(
"/temp")));
// 设置上传文件缓存大小为10m
dfif.setSizeThreshold(1024 * 1024 * 10);
// 创建上传组件
ServletFileUpload upload = new ServletFileUpload(dfif);
// 处理上传文件中文乱码
upload.setHeaderEncoding("utf-8");
try {
// 解析request得到所有的FileItem
List<FileItem> items = upload.parseRequest(request);
// 遍历所有FileItem
for (FileItem item : items) {
// 判断当前是否是上传组件
if (item.isFormField()) {
// 不是上传组件
String fieldName = item.getFieldName(); // 获取组件名称
String value = item.getString("utf-8"); // 解决乱码问题
map.put(fieldName, value);
} else {
// 是上传组件
// 得到上传文件真实名称
String fileName = item.getName();
fileName = FileUploadUtils.subFileName(fileName);
// 得到随机名称
String randomName = FileUploadUtils
.generateRandonFileName(fileName);
// 得到随机目录
String randomDir = FileUploadUtils
.generateRandomDir(randomName);
// 图片存储父目录
String imgurl_parent = "/productImg" + randomDir;
File parentDir = new File(this.getServletContext()
.getRealPath(imgurl_parent));
// 验证目录是否存在,如果不存在,创建出来
if (!parentDir.exists()) {
parentDir.mkdirs();
}
String imgurl = imgurl_parent + "/" + randomName;
map.put("imgurl", imgurl);
IOUtils.copy(item.getInputStream(), new FileOutputStream(
new File(parentDir, randomName)));
item.delete();
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
try {
// 将数据封装到javaBean中
BeanUtils.populate(p, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
ProductService service = new ProductService();
try {
// 调用service完成添加商品操作
service.addProduct(p);
response.sendRedirect(request.getContextPath()
+ "/listProduct");
return;
} catch (AddProductException e) {
e.printStackTrace();
response.getWriter().write("添加商品失败");
return;
}
}
}
2.4.实现编辑商品信息功能
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<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}/admin/css/Style.css" type="text/css" rel="stylesheet">
<script language="javascript" src="${pageContext.request.contextPath}/admin/js/public.js"></script>
<script language="javascript" src="${pageContext.request.contextPath}/admin/js/check.js"></script>
<script type="text/javascript">
//设置类别的默认值
function setProductCategory(t) {
var category = document.getElementById("category");
var ops = category.options;
for ( var i = 0; i < ops.length; i++) {
if (ops[i].value == t) {
ops[i].selected = true;
return;
}
}
};
</script>
</HEAD>
<body onload="setProductCategory('${p.category}')">
<form id="userAction_save_do" name="Form1"
action="${pageContext.request.contextPath}/editProduct" method="post"
enctype="multipart/form-data">
<input type="hidden" name="id" value="${p.id}" />
<table cellSpacing="1" cellPadding="5" width="100%" align="center"
bgColor="#eeeeee" style="border: 1px solid #8ba7e3" border="0">
<tr>
<td class="ta_01" align="center" bgColor="#afd1f3" colSpan="4" height="26">
<strong>编辑商品</strong>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">商品名称:</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="name" class="bg" value="${p.name }" />
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">商品价格:</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="price" class="bg" value="${p.price }" />
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">商品数量:</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="pnum" class="bg" value="${p.pnum}" />
</td>
<td align="center" bgColor="#f5fafe" class="ta_01">商品类别:</td>
<td class="ta_01" bgColor="#ffffff">
<select name="category" id="category">
<option value="">--选择商品类加--</option>
<option value="文学">文学</option>
<option value="生活">生活</option>
<option value="计算机">计算机</option>
<option value="外语">外语</option>
<option value="经营">经营</option>
<option value="励志">励志</option>
<option value="社科">社科</option>
<option value="学术">学术</option>
<option value="少儿">少儿</option>
<option value="艺术">艺术</option>
<option value="原版">原版</option>
<option value="科技">科技</option>
<option value="考试">考试</option>
<option value="生活百科">生活百科</option>
</select>
</td>
</tr>
<tr>
<td align="center" bgColor="#f5fafe" class="ta_01">商品图片:</td>
<td class="ta_01" bgColor="#ffffff" colSpan="3">
<input type="file" name="upload" size="30" value="" /></td>
</tr>
<TR>
<TD class="ta_01" align="center" bgColor="#f5fafe">商品描述:</TD>
<TD class="ta_01" bgColor="#ffffff" colSpan="3">
<textarea name="description" cols="30" rows="3" style="WIDTH: 96%">${p.description}</textarea>
</TD>
</TR>
<TR>
<td align="center" colSpan="4" class="sep1">
<img src="${pageContext.request.contextPath}/admin/images/shim.gif">
</td>
</TR>
<tr>
<td class="ta_01" style="WIDTH: 100%" align="center" bgColor="#f5fafe" colSpan="4">
<input type="submit" class="button_ok" value="确定">
<FONT face="宋体"> </FONT>
<input type="reset" value="重置" class="button_cancel" />
<FONT face="宋体"> </FONT>
<INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回" />
<span id="Label1"> </span>
</td>
</tr>
</table>
</form>
</body>
</HTML>
2.5.实现删除商品信息功能
/**
* 后台系统
* 删除商品信息的servlet
*/
public class DeleteProductServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取请求参数,产品id
String id = request.getParameter("id");
ProductService service = new ProductService();
// 调用service完成添加商品操作
service.deleteProduct(id);
response.sendRedirect(request.getContextPath() + "/listProduct");
return;
}
}//后台系统,根据id删除商品信息
public void deleteProduct(String id) {
try {
dao.deleteProduct(id);
} catch (SQLException e) {
throw new RuntimeException("后台系统根据id删除商品信息失败!");
}
}
//后台系统,根据id删除商品信息
public void deleteProduct(String id) throws SQLException {
String sql = "DELETE FROM products WHERE id = ?";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
runner.update(sql, id);
}
三、销售榜单模块
创建下载页面
<form id="Form1" name="Form1" action="${pageContext.request.contextPath}/download" 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>
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01">
请输入年份
</td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="year" size="15" value="" id="Form1_userName" class="bg" />
</td>
<td height="22" align="center" bgColor="#f5fafe" class="ta_01">
请选择月份
</td>
<td class="ta_01" bgColor="#ffffff">
<select name="month" id="month">
<option value="0">--选择月份--</option>
<option value="1">一月</option>
<option value="2">二月</option>
<option value="3">三月</option>
<option value="4">四月</option>
<option value="5">五月</option>
<option value="6">六月</option>
<option value="7">七月</option>
<option value="8">八月</option>
<option value="9">九月</option>
<option value="10">十月</option>
<option value="11">十一月</option>
<option value="12">十二月</option>
</select>
</td>
</tr>
<tr>
<td width="100" height="22" align="center" bgColor="#f5fafe" class="ta_01">
</td>
<td class="ta_01" bgColor="#ffffff">
<font face="宋体" color="red"> </font>
</td>
<td align="right" bgColor="#ffffff" class="ta_01">
<br /><br />
</td>
<td align="center" bgColor="#ffffff" class="ta_01">
<input type="submit" id="search" name="search" value="下载" class="button_view">
<input type="reset" name="reset" value="重置" class="button_view" />
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
</form>
创建Servlet
public class DownloadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String year = request.getParameter("year");
String month = request.getParameter("month");
ProductService service = new ProductService();
List<Object[]> ps = service.download(year,month);
String fileName=year+"年"+month+"月销售榜单.csv";
response.setContentType(this.getServletContext().getMimeType(fileName));
response.setHeader("Content-Disposition", "attachement;filename="+new String(fileName.getBytes("GBK"),"iso8859-1"));
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
out.println("商品名称,销售数量");
for (int i = 0; i < ps.size(); i++) {
Object[] arr=ps.get(i);
out.println(arr[0]+","+arr[1]);
}
out.flush();
out.close();
}
}
编写Service层代码
// 下载销售榜单
public List<Object[]> download(String year, String month) {
List<Object[]> salesList = null;
try {
salesList = dao.salesList(year, month);
} catch (SQLException e) {
e.printStackTrace();
}
return salesList;
}
编写DAO层的代码
// 销售榜单
public List<Object[]> salesList(String year, String month)
throws SQLException {
String sql = "SELECT products.name,SUM(orderitem.buynum) totalsalnum "
+ "FROM orders,products,orderItem "
+ "WHERE orders.id=orderItem.order_id "
+ "AND products.id=orderItem.product_id "
+ "AND orders.paystate=1 "
+ "and year(ordertime)=? "
+ "and month(ordertime)=? "
+ "GROUP BY products.name "
+ "ORDER BY totalsalnum DESC";
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
return runner.query(sql, new ArrayListHandler(), year, month);
}
四、订单管理模块
4.1.订单管理模块简介
传智书城中的订单管理是对订单信息,比如订单编号、订单收件人、订单总价和订单所属用户等内容的管理,这些订单信息是由传智书城网站前台用户提交订单时生成的。后台订单管理模块的功能结构如下图所示。
4.2.实现查询订单列表功能
4.3.实现查看订单详情功能
4.4.实现删除订单功能