网上图书商城项目学习笔记-013 添加购物车及我的购物车
2016-01-28 12:35
549 查看
一、流程分析
1.购物车模块
2.我的购物车分析
3.添加条目到购物车
二、代码
1.view层
(1)top.jsp
(2)desc.jsp
(3)list.jsp
2.servlet层
(1)CartItemServlet.java
3.service层
(1)CartItemService.java
4.dao层
(1)CartItem.java
1.购物车模块
2.我的购物车分析
3.添加条目到购物车
二、代码
1.view层
(1)top.jsp
<a href="<c:url value='/CartItemServlet?method=myCart'/>" target="body">我的购物车</a>
(2)desc.jsp
<form id="form1" action="<c:url value='/CartItemServlet'/>" method="post"> <input type="hidden" name="method" value="add"/> <input type="hidden" name="bid" value="${book.bid }"/> 我要买:<input id="cnt" style="width: 40px;text-align: center;" type="text" name="quantity" value="1"/>件 </form>
(3)list.jsp
<c:choose> <c:when test="${items eq null }"> <table width="95%" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="right"> <img align="top" src="<c:url value='/images/icon_empty.png'/>"/> </td> <td> <span class="spanEmpty">您的购物车中暂时没有商品</span> </td> </tr> </table> <br/> </c:when> <c:otherwise> <br/> <table width="95%" align="center" cellpadding="0" cellspacing="0"> <tr align="center" bgcolor="#efeae5"> <td align="left" width="50px"> <input type="checkbox" id="selectAll" checked="checked"/><label for="selectAll">全选</label> </td> <td colspan="2">商品名称</td> <td>单价</td> <td>数量</td> <td>小计</td> <td>操作</td> </tr> <c:forEach items="${items }" var="item"> <tr align="center"> <td align="left"> <input value="12345" type="checkbox" name="checkboxBtn" checked="checked"/> </td> <td align="left" width="70px"> <a class="linkImage" href="<c:url value='/jsps/book/desc.jsp'/>"><img border="0" width="54" align="top" src="<c:url value='${item.book.image_b }'/>"/></a> </td> <td align="left" width="400px"> <a href="<c:url value='/jsps/book/desc.jsp'/>"><span>${item.book.bname }</span></a> </td> <td><span>¥<span class="currPrice" id="12345CurrPrice">${item.book.currPrice }</span></span></td> <td> <a class="jian" id="12345Jian"></a><input class="quantity" readonly="readonly" id="12345Quantity" type="text" value="${item.quantity }"/><a class="jia" id="12345Jia"></a> </td> <td width="100px"> <span class="price_n">¥<span class="subTotal" id="12345Subtotal">${item.getSubTotal() }</span></span> </td> <td> <a href="<c:url value='/jsps/cart/list.jsp'/>">删除</a> </td> </tr> </c:forEach> <tr> <td colspan="4" class="tdBatchDelete"> <a href="javascript:alert('批量删除成功');">批量删除</a> </td> <td colspan="3" align="right" class="tdTotal"> <span>总计:</span><span class="price_t">¥<span id="total"></span></span> </td> </tr> <tr> <td colspan="7" align="right"> <a href="<c:url value='/jsps/cart/showitem.jsp'/>" id="jiesuan" class="jiesuan"></a> </td> </tr> </table> <form id="form1" action="<c:url value='/jsps/cart/showitem.jsp'/>" method="post"> <input type="hidden" name="cartItemIds" id="cartItemIds"/> <input type="hidden" name="method" value="loadCartItems"/> </form> </c:otherwise> </c:choose>
2.servlet层
(1)CartItemServlet.java
public class CartItemServlet extends BaseServlet { private CartItemService service = new CartItemService(); /** * 添加购物车条目 * @param req * @param resp * @return * @throws ServletException * @throws IOException */ public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Map map = req.getParameterMap(); CartItem item = CommonUtils.toBean(map, CartItem.class); Book book = CommonUtils.toBean(map, Book.class); User user = (User) req.getSession().getAttribute("sessionUser"); item.setBook(book); item.setUser(user); service.add(item); return myCart(req, resp); } /** * 我的购物车 * @param req * @param resp * @return */ public String myCart(HttpServletRequest req, HttpServletResponse resp) { // 1. 得到uid User user = (User)req.getSession().getAttribute("sessionUser"); String uid = user.getUid(); // 2. 通过service得到当前用户的所有购物车条目 List<CartItem> items = service.myCart(uid); // 3. 保存起来,转发到/cart/list.jsp req.setAttribute("items", items); return "f:/jsps/cart/list.jsp"; } }
3.service层
(1)CartItemService.java
public class CartItemService { private CartItemDao dao = new CartItemDao(); /** * 添加条目 * @param item */ public void add(CartItem item) { try { // 1. 使用uid和bid去数据库中查询这个条目是否存在 CartItem _item = dao.findByUidAndBid(item.getUser().getUid(), item.getBook().getBid()); if(_item == null){//如果原来没有这个条目,那么添加条目 item.setCartItemId(CommonUtils.uuid()); dao.add(item); }else{//如果原来有这个条目,修改数量 // 使用原有数量和新条目数量之各,来做为新的数量 _item.setQuantity(_item.getQuantity() + item.getQuantity()); // 修改这个老条目的数量 dao.updateQuantity(_item); } } catch (SQLException e) { throw new RuntimeException(e); } } /** * 我的购物车功能 * @param uid * @return */ public List<CartItem> myCart(String uid) { try { return dao.findByUser(uid); } catch (SQLException e) { throw new RuntimeException(e); } } }
4.dao层
(1)CartItem.java
public class CartItemDao { private QueryRunner qr = new TxQueryRunner(); /** * 添加条目 * @param item * @throws SQLException */ public void add(CartItem item) throws SQLException { String sql = "insert into t_cartitem(cartItemId, quantity, bid, uid) values (?,?,?,?)"; Object [] params = {item.getCartItemId(), item.getQuantity(), item.getBook().getBid(), item.getUser().getUid()}; qr.update(sql, params); } /** * 通过用户查询购物车条目 * @param uid * @return * @throws SQLException */ public List<CartItem> findByUser(String uid) throws SQLException { String sql = "select * from t_cartitem c,t_book b where uid=? and c.bid=b.bid"; List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), uid); return toCartItemList(mapList); } /** * 把多个Map(List<Map>)映射成多个CartItem(List<CartItem>) * @param mapList * @return */ private List<CartItem> toCartItemList(List<Map<String, Object>> mapList) { List<CartItem> items = new ArrayList<CartItem>(); CartItem item = null; for(Map<String, Object> map : mapList) { item = toCartItem(map); items.add(item); } item = null; return items; } /** * 把一个Map映射成一个Cartitem * @param map * @return */ private CartItem toCartItem(Map<String, Object> map) { if(map == null || map.size() == 0) return null; CartItem item = CommonUtils.toBean(map, CartItem.class); User user = CommonUtils.toBean(map, User.class); Book book = CommonUtils.toBean(map, Book.class); item.setUser(user); item.setBook(book); return item; } /** * 查询某个用户的某本图书的购物车条目是否存在 * @param uid * @param bid * @return * @throws SQLException */ public CartItem findByUidAndBid(String uid, String bid) throws SQLException { String sql= "SELECT * FROM t_cartitem c, t_user u, t_book b WHERE c.uid=u.uid AND c.bid=b.bid AND c.uid=? AND c.bid=?"; Map<String,Object> map = qr.query(sql, new MapHandler(), uid, bid); return toCartItem(map); } /** * 修改指定条目的数量 * @param _item * @throws SQLException */ public void updateQuantity(CartItem _item) throws SQLException { String sql = "update t_cartitem set quantity=? where cartItemId=?"; qr.update(sql, _item.getQuantity(), _item.getCartItemId()); } }
相关文章推荐
- C语言文件操作解析(一)
- 在unity中序列化储存游戏元素
- ubuntu 14.04 no valid active connections found
- 对象的序列化
- android学习笔记——BroadcastReceiver实现强制下线
- 逻辑的封闭性
- 第二章 TestNG环境搭建
- jdk动态代理实现原理
- springmvc找不到静态资源文件等
- 三.分支与合并
- Vijos 1035 贪婪的送礼者
- java 反编译
- Chrome DevTools的使用
- MySQL中KEY与INDEX的区别
- java读写Excel(JXL)
- 不读完你不会成功
- iOS添加左右导航按钮
- NSUrlSession NSUrlConnection https连接时证书无效的解决办法
- 8168开机自动挂载nfs
- 学习随笔笔记一