2.2 查看我的订单
①底层代码
package com.zking.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; 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 javax.servlet.http.HttpSession; import com.zking.biz.GoodsBiz; import com.zking.biz.IGoodsBiz; import com.zking.biz.IOrderBiz; import com.zking.biz.IUserBiz; import com.zking.biz.OrderBiz; import com.zking.biz.UserBiz; import com.zking.entity.Goods; import com.zking.entity.Order; import com.zking.entity.User; /** * 订单们控制器 多个订单 * @author PC * */ @WebServlet("/orders.do") public class OrdersServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置编码 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html; charset=UTF-8"); //拿session和out HttpSession session = req.getSession(); PrintWriter out = resp.getWriter(); //从session中拿用户 User u =(User)session.getAttribute("user"); //servlet调用biz IOrderBiz iob = new OrderBiz(); //调用获取订单集合方法 List<Order> ordersls = iob.getAllByUser(u); //存起来 req.setAttribute("ordersls", ordersls); //转发到myorder.jsp进行绑值 req.getRequestDispatcher("myorder.jsp?index=6").forward(req, resp); } }
②前端绑值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html lang="en"> <head> <title>我的订单</title> <!-- 支持手机端 --> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- 引入CSS --> <link rel="stylesheet" href="CSS/bootstrap.css" /> <!-- 引入Bootstrap图标的样式 --> <link rel="stylesheet" href="font/bootstrap-icons.css" /> <style> .navbar-collapse { /*1代表弹性扩大占用父容器剩余部分 0代表不占用*/ flex-grow: 0; } .active { /*加粗*/ font-weight: bold; } .text-right button{ width: 120px;/*宽度*/ height: 30px;/*高度*/ line-height: 10px;/*行高*/ margin-top: 10px;/*上间距*/ } </style> </head> <body> <!--页面包含 --> <jsp:include page="header.jsp"></jsp:include> <!-- 我的订单 --> <div class="container"> <!--当订单集合为空时 --> <c:if test="${empty ordersls}"> <div style="margin-top: 50px;margin-bottom:50px" class="h3 text-center text-pink">亲爱的${user.uname},订单空空如也喔<a href="index.do?index=1">快去奖励自己吧!</a></div> <div class="text-center"> <img width="700px" src="img/bz11.jpg"> </div> </c:if> <!--当订单集合不为空时 --> <c:if test="${not empty ordersls}"> <!-- text-center居中 --> <table style="margin-top: 20px;" class="table table-hover text-center "> <!-- class="bg-primary" 加颜色 --> <thead class="bg-primary" style="color: aliceblue;"> <tr> <th scope="col">订单编号</th> <th scope="col">收货地址</th> <th scope="col">订单总价</th> <th scope="col">购买者</th> <th scope="col">操作</th> </tr> </thead> <tbody> <!--绑定订单的值 --> <c:forEach items="${ordersls}" var="o"> <tr style="height:80px;line-height: 50px"> <th scope="row">${o.oid}</th> <td>${o.oaddress}</td> <td>${o.osumprice}¥</td> <td>${o.user.uname}</td> <td> <a href="deleteOrder.do?oid=${o.oid}" onclick="return confirm('确定删除嘛?(^.^)')" >删除订单 </a> <a href="orderInfo.do?oid=${o.oid}">查看订单详情 </a> </td> </tr> </c:forEach> </tbody> </table> </c:if> </div> <!-- 引入jQuery的类库 --> <!-- 再引入Bootstrap的类库 --> <script src="js/jquery-3.6.4.js"></script> <script src="js/bootstrap.js"></script> </body> </html>
2.3 查看订单详情
①页面跳转,传参
//跳转 转发到订单详情页面进行绑值
req.getRequestDispatcher("orderinfo.jsp").forward(req, resp);
②底层代码
package com.zking.servlet; import java.io.IOException; import java.io.PrintWriter; 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 javax.servlet.http.HttpSession; import com.zking.biz.IOrderBiz; import com.zking.biz.IUserBiz; import com.zking.biz.OrderBiz; import com.zking.biz.UserBiz; import com.zking.entity.Order; import com.zking.entity.User; /** * 查看订单详情控制器 * @author PC * */ @WebServlet("/orderInfo.do") public class OrderInfoServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置编码 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html; charset=UTF-8"); //拿session和out HttpSession session = req.getSession(); PrintWriter out = resp.getWriter(); //接收oid String oid=req.getParameter("oid"); //servlet调biz层 IOrderBiz iob = new OrderBiz(); //调用单个查询的方法 Order order = iob.getOrder(oid); //存起来 req.setAttribute("myorder",order);//键 值 //跳转 转发到订单详情页面进行绑值 req.getRequestDispatcher("orderinfo.jsp").forward(req, resp); //两种思路 /** * select * from tb_orderitem where oid=?; * 1.根据订单编号查找到对应的订单项集合 存起来 去订单详情页面绑定值 * * select * from tb_order where oid=?; 单个查询 在order实体类写了一个items * 2.根据订单编号查找到对应的订单 把订单存起来 去订单详情界面 即可以绑定该订单的值又可以绑定订单详情的值 √ * * */ } }
③前端绑值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html lang="en"> <head> <title>查看订单详情</title> <!-- 支持手机端 --> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <!-- 引入CSS --> <link rel="stylesheet" href="CSS/bootstrap.css" /> <!-- 引入Bootstrap图标的样式 --> <link rel="stylesheet" href="font/bootstrap-icons.css" /> <style> .navbar-collapse { /*1代表弹性扩大占用父容器剩余部分 0代表不占用*/ flex-grow: 0; } .active { /*加粗*/ font-weight: bold; } .text-right button{ width: 120px;/*宽度*/ height: 30px;/*高度*/ line-height: 10px;/*行高*/ margin-top: 10px;/*上间距*/ } </style> </head> <body> <!-- 订单详情 --> <div class="container" style="margin-top: 40px"> <!--当订单为空时 --> <c:if test="${empty myorder}"> <jsp:forward page="orders.do?index=6"></jsp:forward> </c:if> <!--当购物车集合不为空时 --> <c:if test="${not empty myorder}"> <!-- text-center居中 --> <table style="margin-top: 20px;" class="table table-hover text-center "> <!-- class="bg-primary" 加颜色 --> <thead class="bg-primary" style="color: aliceblue;"> <tr> <th scope="col">订单项编号</th> <th scope="col">订单编号</th> <th scope="col">商品名称</th> <th scope="col">商品价格</th> <th scope="col">商品图片</th> <th scope="col">购买数量</th> <th scope="col">小计</th> </tr> </thead> <tbody> <!--对象不能自己遍历 foreach 跟实体类一致 --> <!--绑对应订单项的值 --> <c:forEach items="${myorder.items}" var="oi"> <tr style="height:80px;line-height: 50px"> <th scope="row">${oi.oiid}</th> <td>${oi.oid}</td> <td>${oi.goods.gname}</td> <td>${oi.goods.gprice}元</td> <td><img width="80px" src="${oi.goods.gpath}"></td> <td>${oi.oinum}</td> <td><span class="xx">${oi.oiprice}</span>元</td> </tr> </c:forEach> </tbody> </table> <!-- 合计 --> <div class="text-right"> <h2>合计:<span id="sums">229.3</span>元</h2> <a href="orders.do?index=6"> <button type="button" class="btn btn-success" >点击返回</button></a> </div> </c:if> </div> <!-- 引入jQuery的类库 --> <!-- 再引入Bootstrap的类库 --> <script src="js/jquery-3.6.4.js"></script> <script src="js/bootstrap.js"></script> <script type="text/javascript"> //加载DOM jquery $(function(){ //统计价格 var sum = 0.0; //遍历 $(".xx").each(function(i,v){//下标 值 //累计求和 sum+=parseFloat(v.innerHTML); }); //保留1位小数 sum=sum.toFixed(2); //给span标签赋值 $("#sums").html(sum); }) //即要拿id 就直接把id设置成gid function myf(gid) { //拿文本框的数量 var num=$("#"+gid).val(); //跳转到servlet location.href="updateCart.do?gid="+gid+"&num="+num; } /* //如果仍然出现很多位小数 则采用以下方法保留1位小数 String s = String.format("%.1f",this.oiprice); this.oiprice=Double.parseDouble(s); */ </script> </body> </html>