標籤:function 購物車
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4B/F9/wKioL1Q2SYPRBh7ZAACQ5cOHFcE328.jpg" title="QQ20141009163718.png" alt="wKioL1Q2SYPRBh7ZAACQ5cOHFcE328.jpg" />
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title><script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.1.min.js"></script><script type="text/javascript">$(function(){//判斷session裡是否已經有shoppingCartvar isHasCart = "${sessionScope.shoppingCart == null}";//如果session裡沒有shoppingCart,說明是第一次來到這個頁面if(isHasCart == "true"){$("#cartstatus").hide();}else{$("#cartstatus").show();$("#bookName").text("${sessionScope.shoppingCart.bookName}");$("#totalBookNum").text("${sessionScope.shoppingCart.totalBookNum}");$("#totalMoney").text("${sessionScope.shoppingCart.totalMoney}");}$("a").click(function(){$("#cartstatus").show();var url = this.href;var args = {"time":new Date()};$.getJSON(url, args, function(data){$("#bookName").text(data.bookName);$("#totalBookNum").text(data.totalBookNum);$("#totalMoney").text(data.totalMoney);});return false;});})</script></head><body><!-- 1.擷取當前頁面所有的a結點,並為每一個a結點都添加onclick響應事件,同時取消其預設行為。2.準備發送ajax請求:url(a結點的href屬性值) args(時間戳記)3.響應為一個JSON對象,包括:bookName totalBookNum totalMoney4.把對應的屬性添加到對應的位置 --> <div id="cartstatus">您已將<span id="bookName"></span>加入到購物車中,購物車中的書有<span id="totalBookNum"></span>本,總價是<span id="totalMoney"></span>元</div><br/>Java<a href="${pageContext.request.contextPath}/addToCart?id=Java&price=100">加入購物車</a><br/>Oracle<a href="${pageContext.request.contextPath}/addToCart?id=Oracle&price=150">加入購物車</a><br/>Struts2<a href="${pageContext.request.contextPath}/addToCart?id=Struts2&price=180">加入購物車</a><br/></body></html>
ShoppingCartItem.java
public class ShoppingCartItem {private int number;private String bookName;private int price;//...}
ShoppingCart.java 購物車
public class ShoppingCart {//key:書名 value:ShoppingCartItem對象private Map<String, ShoppingCartItem> items = new HashMap<String, ShoppingCartItem>();private String bookName;public void addToCart(String bookName, int price) {this.bookName = bookName;if (items.containsKey(bookName)) {ShoppingCartItem shoppingCartItem = items.get(bookName);shoppingCartItem.setNumber(shoppingCartItem.getNumber() + 1);}else{ShoppingCartItem shoppingCartItem = new ShoppingCartItem();shoppingCartItem.setBookName(bookName);shoppingCartItem.setNumber(1);shoppingCartItem.setPrice(price);items.put(bookName, shoppingCartItem);}}public int getTotalBookNum() {int total = 0;for (ShoppingCartItem item:items.values()) {total += item.getNumber();}return total;}public int getTotalMoney() {int money = 0;for (ShoppingCartItem item:items.values()) {money += item.getNumber() * item.getPrice();}return money;}public String getBookName() {return bookName;}}
addToCart.java servlet
String bookName = request.getParameter("id");int price = Integer.parseInt(request.getParameter("price"));ShoppingCart shoppingCart = (ShoppingCart) request.getSession().getAttribute("shoppingCart");if (shoppingCart == null) {shoppingCart = new ShoppingCart();request.getSession().setAttribute("shoppingCart", shoppingCart);}shoppingCart.addToCart(bookName, price);StringBuilder result = new StringBuilder();result.append("{")//必須用雙引號.append("\"bookName\":\"" + bookName + "\"").append(",").append("\"totalBookNum\":" + shoppingCart.getTotalBookNum()).append(",").append("\"totalMoney\":" + shoppingCart.getTotalMoney()).append("}");response.setContentType("text/javascript");response.getWriter().print(result.toString());
本文出自 “阿凡達” 部落格,請務必保留此出處http://shamrock.blog.51cto.com/2079212/1561834
Ajax學習筆記-購物車