前面两篇中已经介绍了部分功能,这篇中将会介绍最后的购物车功能。
购物车功能共有添加、删除、清空购物车、计算总价的功能。
添加到购物车:
每个商品都有自己的pid且商品之间没有重复的,所以,可以获取每个商品的pid,通过每个商品的pid,通过web层——service层——dao层逐步传递信息。
web层;获取jsp中商品的pid以及购买的数量:
String pid = request.getParameter("pid");
int buyNum = Integer.parseInt(request.getParameter("buyNum"));
int buyNum = Integer.parseInt(request.getParameter("buyNum"));
获得product对象:
Product product = service.findProductByPid(pid);
在购物车中需要计算每种零食所花费的金额以及购物车中物品的总价值。每类零食的价格为单价*数量。而总价格即为各类零食价格的总和。
每类零食价格:
double subtotal=product.getDisc_price()*buyNum;
由于商品添加一次后,用户可能还会添加同类商品,所以需要进行相加。可以设置之前添加的为oldBuyNum,新添加的为newBuyNum。每次添加商品,相加即为当前商品的个数。而此类商品的总价格即为当前个数*单价。
int oldBuyNum = cartItem.getBuyNum();
oldBuyNum+=buyNum;
cartItem.setBuyNum(oldBuyNum);
购物车中有对象:数量、单价、总价。封装购物车内容
CartItem item = new CartItem();
item.setProduct(product);
item.setBuyNum(buyNum);
item.setSubtotal(subtotal);
新买商品的花费
newsubtotal = buyNum*product.getDisc_price();
而总价:
cartItem.setSubtotal(oldsubtotal+newsubtotal);
double total = cart.getTotal()+newsubtotal;
service层:web层创建方法到service层,service层同样创建方法到dao层:
public Product findProductByPid(String pid) {
ProductDao dao=new ProductDao();
Product product=null;
try {
product = dao.findProductByPid(pid);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return product;
// TODO Auto-generated method stub
}
public Product findProductByPid(String pid) throws SQLException {
// TODO Auto-generated method stub
QueryRunner runner=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select * from [product] where pid=? ";
return runner.query(sql, new BeanHandler<Product>(Product.class), pid);
}
web层获得查询结果后,直接跳到cart.jsp页面中。
response.sendRedirect(request.getContextPath()+"/cart.jsp");
来到cart.jsp中,引入JSTL标签:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
使用forEach完成商品信息的动态显示:
<c:forEach items="${cart.cartItems }" var="entry">
<tr class="active">
<td width="60" width="40%">
<input type="hidden" name="id" value="22">
<img src="${pageContext.request.contextPath }/${entry.value.product.pimage}" width="70" height="60">
</td>
<td width="30%">
<a target="_blank">${entry.value.product.pname}</a>
</td>
<td width="20%">
¥${entry.value.product.disc_price}
</td>
<td width="10%">
${entry.value.buyNum }
</td>
<td width="15%">
<span class="subtotal">¥${entry.value.subtotal }</span>
</td>
<td>
<a href="javascript:;" onclick="delProFromCart('${entry.value.product.pid}')" class="delete">删除</a>
</td>
</tr>
</c:forEach>
删除功能:删除与清空购物车,当点击时需要进行提示:是否删除或者是否清空。HTML无法实现这个功能,使用JS创建个函数,实现这两个功能:
<script type="text/javascript">
function delProFromCart(pid){
if(confirm("是否确定要删除该项?")){
location.href="${pageContext.request.contextPath }/product?method=delProFromCart&pid="+pid;
}
}
function clearCart(){
if(confirm("是否确定要清空购物车?")){
location.href="${pageContext.request.contextPath }/product?method=clearCart";
}
}
</script>
前端界面已完成,接下来则是编写Java代码来实现功能。
删除:移除当前项商品、修改总金额、重新返回cart.jsp页面。
移除当前项需要获取当前项的pid才可以移除指定的商品:
String pid = request.getParameter("pid");
而移除之后,总金额应当相应地减去已移除项的价格。最终再重定向回到cart.jsp中。
public class delServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得要删除的item的pid
String pid = request.getParameter("pid");
//删除session中的购物车中的购物项集合中的item
HttpSession session = request.getSession();
Cart cart = (Cart) session.getAttribute("cart");
if(cart!=null){
Map<String, CartItem> cartItems = cart.getCartItems();
//需要修改总价
cart.setTotal(cart.getTotal()-cartItems.get(pid).getSubtotal());
//删除
cartItems.remove(pid);
cart.setCartItems(cartItems);
}
//将车再次访问session
session.setAttribute("cart", cart);
//直接跳转到购物车页面
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
}
相比于删除商品来说,清空购物车更为简单,删除商品需要获得商品的pid来确保操作的正确,不会发生误删的结果。而清空购物车的话,只需要remove掉cart中的信息,那么就可以完成清空的操作了。
public class clearCartServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("cart");
//跳转回cart.jsp
response.sendRedirect(request.getContextPath()+"/cart.jsp");
}
}