<%@pagelanguage="java"contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <Metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"> <title></title> <scripttype="text/javascript"src="${pageContext.request.contextPath}/js/jquery-1.11.1.min.js"></script> <scripttype="text/javascript"> $(function(){ //判断session里是否已经有shoppingCart varisHasCart="${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(); varurl=this.href; varargs={"time":newDate()}; $.getJSON(url,args,function(data){ $("#bookName").text(data.bookName); $("#totalBookNum").text(data.totalBookNum); $("#totalMoney").text(data.totalMoney); }); returnfalse; }); }) </script> </head> <body> <!-- 1.获取当前页面所有的a结点,并为每一个a结点都添加onclick响应事件,同时取消其默认行为。 2.准备发送ajax请求:url(a结点的href属性值)args(时间戳) 3.响应为一个JSON对象,包括:bookNametotalBookNumtotalMoney 4.把对应的属性添加到对应的位置 --> <divid="cartstatus"> 您已将<spanid="bookName"></span>加入到购物车中,购物车中的书有<spanid="totalBookNum"></span>本,总价是<spanid="totalMoney"></span>元 </div> <br/> Java<ahref="${pageContext.request.contextPath}/addToCart?id=Java&price=100">加入购物车</a> <br/> Oracle<ahref="${pageContext.request.contextPath}/addToCart?id=Oracle&price=150">加入购物车</a> <br/> Struts2<ahref="${pageContext.request.contextPath}/addToCart?id=Struts2&price=180">加入购物车</a> <br/> </body> </html>
ShoppingCartItem.java
publicclassShoppingCartItem{ privateintnumber; privateStringbookName; privateintprice; //... }
ShoppingCart.java 购物车
publicclassShoppingCart{ //key:书名value:ShoppingCartItem对象 privateMap<String,ShoppingCartItem>items=newHashMap<String,ShoppingCartItem>(); privateStringbookName; publicvoidaddToCart(StringbookName,intprice){ this.bookName=bookName; if(items.containsKey(bookName)){ ShoppingCartItemshoppingCartItem=items.get(bookName); shoppingCartItem.setNumber(shoppingCartItem.getNumber()+1); }else{ ShoppingCartItemshoppingCartItem=newShoppingCartItem(); shoppingCartItem.setBookName(bookName); shoppingCartItem.setNumber(1); shoppingCartItem.setPrice(price); items.put(bookName,shoppingCartItem); } } publicintgetTotalBookNum(){ inttotal=0; for(ShoppingCartItemitem:items.values()){ total+=item.getNumber(); } returntotal; } publicintgetTotalMoney(){ intmoney=0; for(ShoppingCartItemitem:items.values()){ money+=item.getNumber()*item.getPrice(); } returnmoney; } publicStringgetBookName(){ returnbookName; } }
addToCart.java servlet
StringbookName=request.getParameter("id"); intprice=Integer.parseInt(request.getParameter("price")); ShoppingCartshoppingCart=(ShoppingCart)request.getSession().getAttribute("shoppingCart"); if(shoppingCart==null){ shoppingCart=newShoppingCart(); request.getSession().setAttribute("shoppingCart",shoppingCart); } shoppingCart.addToCart(bookName,price); StringBuilderresult=newStringBuilder(); 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());
补充:Jackson的使用
publicclassCustomer{ privateintid; privateStringname; publicintgetId(){ returnid; } publicvoidsetId(intid){ this.id=id; } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicStringgetCity(){ return"北京"; } //忽略一个getter属性 @JsonIgnore publicStringgetBirth(){ return"1990-12-31"; } publicCustomer(intid,Stringname){ super(); this.id=id; this.name=name; } publicstaticvoidmain(String[]args)throwsJsonGenerationException,JsonMappingException,IOException{ ObjectMappermapper=newObjectMapper(); Customercustomer=newCustomer(0,"umgsai"); StringjsonString=mapper.writeValueAsString(customer); System.out.println(jsonString); //Jackson使用getter方法来定位JSON对象的属性 //{"name":"umgsai","id":0,"city":"北京"} } }
本例使用的Jar包是jackson-all-1.7.6.jar
教程中加的是下面三个包
jackson-annotation-2.2.2.jar
jackson-core-2.2.2.jar
jackson-databind-2.2.2.jar
原文链接:https://www.f2er.com/ajax/162442.html