结算功能

// 实现结算功能
    @RequestMapping(value = "/checkout", method = RequestMethod.POST)
    public String checkout(HttpServletRequest request) {
        // 获取到从购物车里选择想要支付的鲜花的ID
        String strs = request.getParameter("strs");
        // 因为支付的时候需要当前的值,所以要把strs存储到session中
        request.getSession().setAttribute("strs", strs);
        // 动态获取每个鲜花的总价,如果想要动态获取总价,那么name必然是动态的
        // 因为name值为选择的花的id的值,所以,name的来源需要基于strs
        // 分隔strs
        String[] str = strs.split(",");
        double sum = 0.0;
        Map<String, Integer> flowercount = new HashMap<String , Integer>();
        Map<String, Double> flowersum = new HashMap<String , Double>();
        if (str != null) {
            for (String id : str) {
                String s = request.getParameter("p" + id);
                // 获取鲜花的数量
                String c = request.getParameter("count" + id);
                int count = 0;
                if (c != null) {
                    count = Integer.parseInt(c);
                }
                // 因为此项数据需要在后面获取,所以需要先存储到 Map集合中
                flowercount.put(id, count);
                double sumone = Double.parseDouble(request.getParameter("p" + id));
                // 因为订单详情需要此总价,所以需要将此数据存储到map中
                flowersum.put(id, sumone);
                sum += sumone;
            }
        }
        // 将以上花的数量存储到session
        request.getSession().setAttribute("flowercount", flowercount);
        // 将以上花的总价存储到session中
        request.getSession().setAttribute("flowersum", flowersum);
        request.getSession().setAttribute("sum", sum);
        // 在进行页面跳转之前,此处从数据库中查询到某认的地址,存储到地址对象中,并将地址对象存储到request对象中,那么页面跳转的时候就可以在checkout.jsp页面获取到对象信息
        // 注意,因为页面显示的是默认地址,所以不需要使用forEach
        // 所谓的默认地址即为地址设置的标识为默认(实现方式可以添加一个字段,当为1的时候表示默认,为0的时候是非默认)
        // request.getRequestDispatcher("checkout.jsp").forward(request, response);

        return "checkout.jsp";
    }

 

posted on 2019-10-28 11:54  年夜饭  阅读(203)  评论(0编辑  收藏  举报

导航