学习过程:
完成主页后,我们就可以做购物车了,当然我们这个购物车实现相对比较简单,因为要实现像京东网站,淘宝网站那些购物车设计的技术比较多,其实所谓的购物车,其实关键一点就是如何保持购物信息,有些网站是保存在客户端的cookie里,再结合session和数据库进行保存的,那么我们这里就采用最简单的保存在session中,虽然这种实现比较简单,但是问题也有很多,比如保存在session中需要使用服务器的资源,用户下次登录所有的购物信息也不会保存,所有很多网站都会把购物信息保存在数据库或者cookie中。大家也可以根据这个思路改写我们的购物车。
1、定义一个购物车对象
public class Cart implements Serializable{
private Goods goods;//商品信息
private int num;//购买数量
public Goods getGoods() {
return goods;
}
public void setGoods(Goods goods) {
this.goods = goods;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
2、实现购物车的servlet。由于购物车信息只是保存session中,所有也比较简单,涉及的功能主要就是添加购物信息,删除购物信息和清空购物信息,在添加的时候注意要判断一下购物车是否已经由此商品,其他实现比较简单,代码如下:
public class CartServet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String ope=request.getParameter("ope");
GoodsDao goodsDao=new GoodsDao();
HttpSession session=request.getSession();
if(ope==null||ope.equals("add")){
//如果是第一次放置,就要判断购物车是否为空
Object carttemp=session.getAttribute("carts");
List<Cart> carts=null;
if(carttemp==null){
carts=new ArrayList<Cart>();//购物车
}else{
carts=(List<Cart>)carttemp;
}
//当前要购买的商品的Id
int goodsId=Integer.parseInt(request.getParameter("gid"));
Goods goods=goodsDao.getById(goodsId);
//先判断购物车是否已经有此商品,有拿到商品,数量加1,没有new一个新的购物信息
boolean flag=false;//false 没有此商品, true 有次商品
int i=0;//已经有商品的位置
for(;i<carts.size();i++){
Cart carttemp1=carts.get(i);
if(carttemp1.getGoods().getGoodsId()==goodsId){
flag=true;
break;
}
}
if(flag){//有
carts.get(i).setNum(carts.get(i).getNum()+1);
}else{//没有
Cart cart=new Cart();//单购物信息
cart.setGoods(goods);
cart.setNum(1);
carts.add(cart);
}
double count=count(carts);//计算总价
session.setAttribute("count", count);
session.setAttribute("carts", carts);
response.sendRedirect("cart.jsp");
return;
}else if("del".equals(ope)){//删除
//当前要删除的商品的Id
int goodsId=Integer.parseInt(request.getParameter("gid"));
List<Cart> carts=(List<Cart>)session.getAttribute("carts");
for(int i=0;i<carts.size();i++){
Cart carttemp1=carts.get(i);
if(carttemp1.getGoods().getGoodsId()==goodsId){
carts.remove(carttemp1);
break;
}
}
double count=count(carts);//计算总价
session.setAttribute("count", count);
response.sendRedirect("cart.jsp");
return;
}else if("clear".equals(ope)){//清空
session.setAttribute("carts", null);
response.sendRedirect("cart.jsp");
return;
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
private double count(List<Cart> carts){
double result=0.0;
for(Cart cart:carts){
result+=(cart.getGoods().getCash()*cart.getNum());
}
return result;
}
}
3、显示购物车信息。展示就更简单了,由于购物车信息都是在session中所有直接在页面通过EL表达式获得就可以了。打开cart.jsp页面,代码如下:
<!-- content -->
<div id="manage_content_wrapper">
<table width="922" border="1" cellspacing="0" bordercolor="#000000"
align="center">
<tr>
<td width="166" height="53">商品图片</td>
<td width="272">商品名称</td>
<td width="127">商品单价</td>
<td width="104">购买数量</td>
<td width="114">操作</td>
</tr>
<c:forEach items="${carts }" var="cart">
<tr>
<td height="67"><img src="goodsImage/${cart.goods.pic }" onerror="this.src=''" />
</td>
<td>${cart.goods.goodsName }</td>
<td>${cart.goods.cash }</td>
<td>${cart.num }</td>
<td><a href="cartServet?ope=del&gid=${cart.goods.goodsId }">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="6" height="50">总价:${count }元 <span id="count"> </span></td>
</tr>
</table>
<div class="car_pay_btn" style="text-align: center">
<a href="cartServet?ope=clear">清空购物车</a> <a
href="orderServlet?ope=add">结帐</a>
</div>
</div>
<!-- end of content -->