一、商品添加的类别ajax显示
add.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <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" type="text/css" rel="stylesheet"> <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script> <script type="text/javascript"> $(function(){ //需要页面加载完毕后去异步获得分类数据 $.post( "${pageContext.request.contextPath}/admin?method=findAllCategory", function(data){ //[{"cid":"xxx","cname":"xxx" },{},{}] //<option value="">手机数码</option> //拼接<option value=""></option>放到select中 var content = ""; for(var i=0;i<data.length;i++){ content+="<option value='"+data[i].cid+"'>"+data[i].cname+"</option>"; } $("#cid").html(content); }, "json" ); }); </script> </HEAD> <body> <!-- --> <form id="userAction_save_do" name="Form1" action="${pageContext.request.contextPath}/adminAddProduct" method="post" enctype="multipart/form-data"> <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>添加商品</STRONG> </strong> </td> </tr> <tr> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 商品名称: </td> <td class="ta_01" bgColor="#ffffff"> <input type="text" name="pname" value="" id="userAction_save_do_logonName" class="bg"/> </td> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 是否热门: </td> <td class="ta_01" bgColor="#ffffff"> <select name="is_hot"> <option value="1">是</option> <option value="0">否</option> </select> </td> </tr> <tr> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 市场价格: </td> <td class="ta_01" bgColor="#ffffff"> <input type="text" name="market_price" value="" id="userAction_save_do_logonName" class="bg"/> </td> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 商城价格: </td> <td class="ta_01" bgColor="#ffffff"> <input type="text" name="shop_price" value="" id="userAction_save_do_logonName" class="bg"/> </td> </tr> <tr> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 商品图片: </td> <td class="ta_01" bgColor="#ffffff" colspan="3"> <input type="file" name="upload" /> </td> </tr> <tr> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 所属分类: </td> <td class="ta_01" bgColor="#ffffff" colspan="3"> <select name="cid" id="cid"> <!-- <option value="">大型电器</option> <option value="">手机数码</option> <option value="">衣帽箱包</option> --> </select> </td> </tr> <tr> <td width="18%" align="center" bgColor="#f5fafe" class="ta_01"> 商品描述: </td> <td class="ta_01" bgColor="#ffffff" colspan="3"> <textarea name="pdesc" rows="5" cols="30"></textarea> </td> </tr> <tr> <td class="ta_01" style="WIDTH: 100%" align="center" bgColor="#f5fafe" colSpan="4"> <button type="submit" id="userAction_save_do_submit" value="确定" class="button_ok"> 确定 </button> <FONT face="宋体"> </FONT> <button type="reset" value="重置" class="button_cancel">重置</button> <FONT face="宋体"> </FONT> <INPUT class="button_ok" type="button" onclick="history.go(-1)" value="返回"/> <span id="Label1"></span> </td> </tr> </table> </form> </body> </HTML>
AdminServlet
public class AdminServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String methodName = request.getParameter("method"); if("findAllCategory".equals(methodName)){ findAllCategory(request,response); }else if("findAllOrders".equals(methodName)){ findAllOrders(request,response); }else if("findOrderInfoByOid".equals(methodName)){ findOrderInfoByOid(request,response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } //获得所有类别 public void findAllCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //提供一个集合List<Category> 转成json字符串 AdminService service = new AdminService(); List<Category> categoryList = service.findAllCategory(); Gson gson = new Gson(); String json = gson.toJson(categoryList); response.getWriter().write(json); } }
AdminService
public class AdminService { //获得所有类别 public List<Category> findAllCategory() { AdminDao dao =new AdminDao(); try { return dao.findAllCategory(); } catch (SQLException e) { e.printStackTrace(); return null; } } }
AdminDao
public class AdminDao { //获得所有类别 public List<Category> findAllCategory() throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from category"; return runner.query(sql, new BeanListHandler<Category>(Category.class)); } }
二、商品添加
AdminAddServlet
public class AdminAddProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); //收集表单的数据,封装一个Product实体,将上传图片存到服务器磁盘上 Product product = new Product(); //收集数据的容器 Map<String,Object> map = new HashMap<String,Object>(); try { //创建磁盘文件项工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //创建文件上传的核心对象 ServletFileUpload upload = new ServletFileUpload(factory); //解析request获得文件项对象集合 List<FileItem> parseRequest = upload.parseRequest(request); for(FileItem item : parseRequest){ //判断是否是普通表单项 boolean formFiled = item.isFormField(); if(formFiled){ //是普通表单项,封装到Product实体中 String fieldName = item.getFieldName(); String fieldValue = item.getString("UTF-8"); map.put(fieldName,fieldValue); }else{ //是文件上传项,获得文件项名称,获得文件内容 String fileName = item.getName(); String path = this.getServletContext().getRealPath("upload"); InputStream in = item.getInputStream(); OutputStream out = new FileOutputStream(new File(path+"/"+fileName));//E:/xxx/xxx/xxx/xxx.jpg IOUtils.copy(in, out); in.close(); out.close(); item.delete(); map.put("pimage", "upload/"+fileName); } } BeanUtils.populate(product, map); //是否product对象封装数据完全 //private String pid; product.setPid(CommonsUtils.getUUID()); //private String pimage; //private Date pdate; product.setPdate(new Date()); //private int pflag; product.setPflag(0); //private Category category; Category category = new Category(); category.setCid((String)map.get("cid")); product.setCategory(category); //将product传给service层 AdminService service = new AdminService(); service.saveProduct(product); } catch (Exception e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
AdminService
public class AdminService { //添加并保存商品 public void saveProduct(Product product) { AdminDao dao = new AdminDao(); try { dao.saveProduct(product); } catch (SQLException e) { e.printStackTrace(); } } }
AdminDao
public class AdminDao { ////添加并保存商品 public void saveProduct(Product product) throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)"; runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),product.getShop_price(), product.getPimage(),product.getPdate(),product.getIs_hot(),product.getPdesc(), product.getPflag(),product.getCategory().getCid()); } }
三、全部订单查询
AdminServlet
public class AdminServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String methodName = request.getParameter("method"); if("findAllCategory".equals(methodName)){ findAllCategory(request,response); }else if("findAllOrders".equals(methodName)){ findAllOrders(request,response); }else if("findOrderInfoByOid".equals(methodName)){ findOrderInfoByOid(request,response); } } //获得所有订单 private void findAllOrders(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得所有的订单信息 --List<Order> AdminService service = new AdminService(); List<Order> orderList = service.findAllOrders(); request.setAttribute("orderList", orderList); request.getRequestDispatcher("/admin/order/list.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
AdminService
public class AdminService { //获得所有订单 public List<Order> findAllOrders() { AdminDao dao = new AdminDao(); List<Order> orderList = null; try { orderList = dao.findAllOrders(); } catch (SQLException e) { e.printStackTrace(); } return orderList; } }
AdminDao
public class AdminDao { //获得所有订单 public List<Order> findAllOrders() throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from orders"; return runner.query(sql, new BeanListHandler<Order>(Order.class)); } }
四、订单详情查询
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",//触发点 点击谁弹出div popupBlk:"#showDiv",//弹出哪个div closeBtn:"#closeBtn",//关闭按钮 useOverlay:true }); }); //点击按钮查询某个订单的详情 function findOrderInfoByOid(oid){ //清理上一次显示的内容覆盖 $("#showDivTab").html(""); $("#shodDivOid").html(""); $("#loading").css("display","block"); //ajax异步查询 $.post( "${pageContext.request.contextPath}/admin?method=findOrderInfoByOid", {"oid":oid}, function(data){ //隐藏加载图片 $("#loading").css("display","none"); //{"shop_price":4499,"count":2,"pname":"联想","pimage":"product/1/c...","subtotal":2534}, //{} 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>"; 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>"; } $("#showDivTab").html(content); //订单编号 $("#shodDivOid").html(oid); }, "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> <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('${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"></span></h2> <a href="javascript:void(0);" id="closeBtn" class="closeBtn">关闭</a> <div id="loading" style="padding-top:30px;text-align: center;"> <img alt="" style="width:100px;height:100px" 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> --%> </table> </div> </div> </div> </body> </HTML>
AdminServlet
public class AdminServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); String methodName = request.getParameter("method"); if("findAllCategory".equals(methodName)){ findAllCategory(request,response); }else if("findAllOrders".equals(methodName)){ findAllOrders(request,response); }else if("findOrderInfoByOid".equals(methodName)){ findOrderInfoByOid(request,response); } } //根据订单id查询所有订单项和商品信息 protected void findOrderInfoByOid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } String oid = request.getParameter("oid"); AdminService service = new AdminService(); List<Map<String,Object>> mapList = service.findOrderInfoByOid(oid); Gson gson = new Gson(); String json = gson.toJson(mapList); System.out.println(json); response.getWriter().write(json); } }
AdminService
public class AdminService { //根据订单id查询所有订单项和商品信息 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
public class AdminDao { //根据订单id查询所有订单项和商品信息 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 orderitem i,product p "+ "where i.pid=p.pid AND i.oid=?"; return runner.query(sql, new MapListHandler(),oid); } }