我通过foreach显示每个商品详情,该怎么显示总额并且把它传给action?
)
3条回答 默认 最新
傻瓜先生DIM 2019-04-23 19:44关注protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,Integer> shopcar = (Map<String,Integer>)request.getSession().getAttribute("shopcar"); Set<String> isbns = shopcar.keySet(); for(String isbn : isbns){ String value = request.getParameter(isbn); shopcar.put(isbn, Integer.parseInt(value)); //更新购物车中的购买数量 } UserBiz biz = new UserBiz(); try { List<TBook> books = null; if(shopcar.keySet().size()>0){ books = biz.getShopcarData(shopcar.keySet()); } double allPrice = 0; for(TBook bk : books){ int buyCount = shopcar.get(bk.getIsbn()); bk.setBuyCount(buyCount); //设置图书数量 allPrice = allPrice + bk.getPrice()*buyCount; } request.setAttribute("allPrice", allPrice); request.setAttribute("books", books); request.getRequestDispatcher("/main/checkout.jsp").forward(request, response); } catch (Exception e) { Log.logger.error(e.getMessage(),e); request.setAttribute("msg", "网络异常,请和管理员联系"); request.getRequestDispatcher("/err/error.jsp").forward(request,response); } }}
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报