Ajax修改购物车示例

掉漆的栏杆上,反射着夕阳的余晖渐渐消逝。我们的青春就随这样随着夕阳落幕。绮丽多姿扮靓妆,繁花锦簇沁心香。长歌游宝地,徒倚对珠林。家门口的风景,其实眼前的就是最好的,爱你选择的一切!

1.购物车类的设计

ShoppingCartItem:书的封装,包括书名,数量,价格三个属性,以及对应的getter和setter方法。

ShoppingCart:购物车封装类,items为 Map<String, ShoppingCartItem> ,以及加入购物车,得到购物车中书的总数量以及总价格三个函数。

2:jsp加入购物车,超链接中带入书名以及价格

<body> 
<!-- 加入span的目的是为了定位 --> 
<div id="cartstatus"> 
您已经将 
<span id="bookName"></span>加入到购物车中,购物车中有 
<span id="totalBookNumber"></span>本书,总价格是 
<span id="totalMoney"></span> 
</div> 
<br> 
<br> 
java 
<a 
rel="nofollow noopener noreferrer" href="${pageContext.request.contextPath}/addToCart?id=java&price=100">加入购物车</a> 
<br> 
ajax 
<a 
rel="nofollow noopener noreferrer" href="${pageContext.request.contextPath}/addToCart?id=ajax&price=200">加入购物车</a> 
<br> 
jquery 
<a 
rel="nofollow noopener noreferrer" href="${pageContext.request.contextPath}/addToCart?id=jquery&price=300">加入购物车</a> 
<br> 
</body>
<!--${pageContext.request.contextPath}获取该项目的绝对路径 -->

3:addToCart -----servlet的设计

步骤如下:

1) :获取请求参数 id(bookName),price,是从jsp页面中的超链接来获取的

2):在session中获取购物车对象,如果session属性中没有购物车,则新建一个购物车对象放置在session属性中

3) : 加入购物车操作Shopping.addToCart(bookName, price);

4):想ajax传递Json对象,该对象包括 :{""bookName"":"totalBookNumber","totalMoney" },若从服务器端返回json对象,则属性名必须使用双引号!!

5):响应json请求,response.getWriter().print(json);

public class AddToCartServlet extends HttpServlet { 

public void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
this.doPost(request, response); 
} 

public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
//1:获取请求参数 id(bookName),price 
String bookName =request.getParameter("id"); 
int price =Integer.parseInt(request.getParameter("price")); 
//2:获取购物车对象,在session中 
ShoppingCart sc=(ShoppingCart) request.getSession().getAttribute("sc"); 
if(sc==null){ 
sc=new ShoppingCart(); 
request.getSession().setAttribute("sc",sc); 
} 

//3;将点击的对象加入到购物车中 
sc.addToCart(bookName, price); 
//4:准备响应的Json对象:{""bookName"":"totalBookNumber","totalMoney" } 
//若从服务器端返回json对象,则属性名必须使用双引号!! 
StringBuilder sBuilder=new StringBuilder(); 
sBuilder.append("{") 
.append("\"bookName\":\""+bookName+"\"") 
.append(",") 
.append("\"totalBookNumber\":\""+sc.getTotalBookNumber()+"\"") 
.append(",") 
.append("\"totalMoney\":\""+sc.getTotalMoney()+"\"") 
.append("}"); 

//响应json请求 
response.setContentType("text/javascript"); 
response.getWriter().print(sBuilder.toString()); 
} 

}
上述中的用StringBuilder来拼接JSON字符串的方式可以借助第三方开源Jackson来简化实现:
String jsonStr=null; 
ObjectMapper objectMapper=new ObjectMapper(); 
jsonStr=objectMapper.writeValueAsString(sc);

4:ajax接受从服务器传来的参数{""bookName"":"totalBookNumber","totalMoney" }

步骤:

1):为加入购物车这个超链接增加单击响应函数,并取消默认行为(return false)

2):通过 HTTP GET 请求载入 JSON 数据。$.getJSON(url, [data], [callback])

准备url.agrs,并在回调函数内部将购物车中的内容显示在Jsp页面中。

3):通过jquery中的hide(),show()方法,判断是不是第一使用购物车,如果是第一次使用,则jsp页面不显示购物车。

<head> 
<!--${pageContext.request.contextPath}获取该项目的绝对路径 --> 
<script type="text/javascript" 
src="${pageContext.request.contextPath}/scripts/jquery-1.7.2.js"></script> 
<script type="text/javascript"> 
$(function(){ 
var isHasCart="${sessionScope.sc==null}"; 
if(isHasCart=="true"){ 
$("#cartstatus").hide();//隐藏显示的元素 
}else{ 
$("#cartstatus").show(); //显示隐藏的匹配元素 
$("#bookName").text("${sessionScope.sc.bookName}"); 
$("#totalBookNumber").text("${sessionScope.sc.totalBookNumber}"); 
$("#totalMoney").text("${sessionScope.sc.totalMoney}"); 
} 

$("a").click(function(){ 
$("#cartstatus").show(); 
var url=this.href; //url属性 
var agrs={"time":new Date()}; //时间戳 
$.getJSON(url,agrs,function(data){ 
$("#bookName").text(data.bookName); 
$("#totalBookNumber").text(data.totalBookNumber); 
$("#totalMoney").text(data.totalMoney); 
}); 
return false; 
}); 
}); 


</script> 
</head>

以上就是Ajax修改购物车示例。长大,就要学会,心平气和的面对兵荒马乱。早安更多关于Ajax修改购物车示例请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Ajax + PHP session制作购物车

使用Ajax、json实现京东购物车结算界面的数据交互实例

微信小程序利用swiper+css实现购物车商品删除功能

小程序二次贝塞尔曲线实现购物车商品曲线飞入效果

jQuery实现购物车的总价计算和总价传值功能