1.購物車功能
//用戶最近浏覽商品記錄
把商品的id作為參數來傳遞,在另一個頁面來接收這個參數,然後是創建cookie把這個商品id給保存下來。然後就是頁面就會自動保存這個cookie。
//顯示最近浏覽商品 Cookie cookie = new Cookie("proHistory", getHistory); response.addCookie(cookie); String myid = cookie.getValue(); //將一個數化為一個List對象,這個方法會返回一個ArrayList類型的對象,是把字符串從-號開始分劈 List mylist = Arrays.asList(myid.split("_")); Product pro = null; List<Product> list = new ArrayList<Product>(); //遍歷list對象 for (Object item : mylist) { CommodityDaoImpl_Dong cimpl = new CommodityDaoImpl_Dong(); try { pro = cimpl.getById(Integer.parseInt(item.toString()));//一級商品id list.add(pro); request.getSession().setAttribute("slist", list); } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } request.getRequestDispatcher("/product-view.jsp").forward(request, response); }
//顯示最近浏覽商品 @SuppressWarnings("unchecked") private String getHistory(HttpServletRequest request, String id){ //獲取用戶所有攜帶的cookie //可能情況 //第一次訪問:proHistory=null 1 proHistory=1 //非第一次:proHistory=3_2_5 1 proHistory=1_3_2 //非第一次:proHistory=3_1_5 1 proHistory=1_3_5 //非第一次:proHistory=3_2 1 proHistory=1_3_2 //拆解出cookies集合 Cookie[] cookies = request.getCookies(); //浏覽過的商品形成的大字符串 String proHistory=null; for (int i = 0; cookies!=null&&i < cookies.length; i++) { if(cookies[i].getName().equals("proHistory")){ proHistory=cookies[i].getValue(); break; } } if(proHistory==null){ return id; } //根據_分割數據 //在java中 \代表轉義字符 \n \t 等,而 \\ 代表一個反斜槓 而.代表一個元字符 //要表示一個.就需要用 要用\. //所以"\\." 在實際編譯中 就代表 . List mylist = Arrays.asList(proHistory.split("\\_")); LinkedList<String> list = new LinkedList<String>(); list.addAll(mylist); if(list.contains(id)){//集合中含有該商品 list.remove(id); list.addFirst(id); } else { if(list.size()>=3){ list.removeLast(); list.addFirst(id); } else { list.addFirst(id); } } StringBuffer sb = new StringBuffer(); for (String item : list) { sb.append(item+"_"); } return sb.deleteCharAt(sb.length()-1).toString(); }
css
<div class="spacer"></div> <div class="last-view"> <h2>最近浏覽</h2> <dl class="clearfix"> <c:forEach items="${slist}" var="item"> <dt><img src="<%=path %>/images/product/${item.fileName}" /></dt> <dd><a href="<%=path %>/servlet/IndexServlet?opr=blist&id=${item.id}" target="_self">${item.name}</a> <a href="<%=path %>/servlet/IndexServlet?opr=blist&id=${item.id}"></a></dd> <dt> </dt> <dd> </dd> </c:forEach> </dl> </div> </div>
//將購物車中的商品移除
//將商品移除出購物車 if(opr!=null&&opr.equals("deleteShop")){ String shid = request.getParameter("shid"); Cart cart=(Cart)request.getSession().getAttribute("cart"); cart.getMap().remove(shid); request.getSession().setAttribute("cart", cart); request.getRequestDispatcher("/shopping.jsp").forward(request, response); }
//將商品加入到購物車
if(opr!=null&&opr.equals("plists")){ User userid=(User)request.getSession().getAttribute("user");//獲取用戶名對象 if (userid!=null) {//用戶登錄不等於空 // String cartid = request.getParameter("cartid"); String psid=request.getParameter("psid"); //System.out.println("qqqqq"+psid); List<Product> plist = shop.getShoppingById(Integer.parseInt(psid));//商品id //將商品加入到購物車中 Cart cart=(Cart)request.getSession().getAttribute("cart"); if (cart==null) { cart =new Cart(); } cart.Add(plist.get(0)); request.getSession().setAttribute("cart", cart); //request.getRequestDispatcher("/shopping.jsp").forward(request, response); }else{ request.getRequestDispatcher("/login.jsp").forward(request, response); }
css
<div class="wrap"> <div id="shopping"> <form action='<%=path %>/servlet/IndexServlet?opr=check' method="post"> <table> <tr> <td>商品名稱</td> <td>商品價格</td> <td>購買數量</td> <td>操作</td> </tr> <c:forEach items="${cart.map}" var="item"> <tr id="product_id_0"> <td class="thumb"><img src="<%=path %>/images/product/${item.value.product.fileName}" /> <a href="<%=path %>/servlet/IndexServlet?opr=plists&psid=${item.value.product.id}">${item.value.product.name}</a></td> <td class="price" id="price_id_0"> <span class="price">¥${item.value.product.price}</span> <input type="hidden" value="${item.value.product.price }" /> </td> <td class="number"> <span name="del" >-</span> <input id="number_id_0" type="text" name="number" class="text_box" value="${item.value.quantity}" /> <span name="add" >+</span> </td> <td class="delete"><a href="<%=path %>/servlet/IndexServlet?opr=deleteShop&shid=${item.value.product.id}">刪除</a></td> </tr> </c:forEach> </table> <div class="total"><span id="total">總計:¥0</span></div> <div class="button"><input type="submit" value="" /></div> </form> </div>
//用戶結算購物車商品
if(opr!=null&&opr.equals("check")){ //獲取用戶 User uid=(User) request.getSession().getAttribute("user"); if( uid!=null){ //判定用戶不為空 String address = dao.getAddressByUserId(uid.getUserId());// 根據用戶id獲取收貨地址 request.getSession().setAttribute("address", address); request.getRequestDispatcher("/address.jsp").forward(request, response); // Cart cart=(Cart)request.getSession().getAttribute("cart"); }else { request.getRequestDispatcher("/login.jsp").forward(request, response); } } if(opr!=null&&opr.equals("Order")){ Cart cart = (Cart) request.getSession().getAttribute("cart"); // 獲取用戶名 User uid=(User) request.getSession().getAttribute("user"); //String user_id=uid.getUserId(); // 獲取發貨的地址 String address = request.getParameter("address"); address=new String(address.getBytes("iso-8859-1"),"utf-8"); String number=request.getParameter("number"); User user = new User(); // 給用戶id賦值 user.setUserId(uid.getUserId()); // 給用戶名賦值 user.setUserName(dao.getUsernameByUid(user.getUserId())); //user.setUserName(uid.getUserName()); // 給收貨地址賦值 user.setAddress(address); CartItem cartitm=new CartItem(); cartitm.setQuantity(Integer.parseInt(number)); boolean falg = daoorder.add(cart, user); if (falg) { request.getRequestDispatcher("/shopping-result.jsp").forward(request, response); request.getSession().removeAttribute("cart");//結算購物車商品後清除該商品 } else { request.getRequestDispatcher("/address.jsp").forward(request, response); } }
功能寫的不怎麼樣,如有不足之處,請多多指教!