本文主要是介绍网上书城的购物车功能(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
购物车功能
- 前言
- 所需要的拦截器辅助类,web包
- 拦截器辅助类
- web包
- 购物车(拦截器,查询功能,删除功能,更新功能)多个功能代码
- 效果图
- 下单结算功能
- css样式
前言
本次我们学习购物车功能中的拦截器,查询功能,删除功能,更新功能,下单结算功能等这多个功能
所需要的拦截器辅助类,web包
拦截器辅助类
ShoppingFilter 在要把商品加入购物车的时候需要先登录,才能把商品加入购物车
package com.tang.filter;import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.tang.entity.User;import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;/*** 除去login.jsp、register.jsp、index.jsp、、/static/*、/user.action*/
@WebFilter(filterName = "urlFiter",urlPatterns = "/*")
public class ShoppingFilter implements Filter {public ShoppingFilter(){super();}// 只要以下urls是不需要session的private boolean validateUrl(String uri){boolean flag = true;String[] urls = new String[]{"login.jsp","register.jsp","ndex.jsp","static","user.action","category.action","book.action"};for (String url : urls) {if (uri.contains(url)){flag = false;break;}}return flag;}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {HttpServletRequest req = (HttpServletRequest) request;HttpServletResponse resp = (HttpServletResponse) response;// 这些请求路径之外都要登录后才能访问String uri = req.getRequestURI();if((!"/".equals(uri)) && validateUrl(uri)) {;String methodName = req.getParameter("methodName");
// if ("add".equals(methodName) || "pay".equals(methodName) || "list".equals(methodName)){User user = (User) req.getSession().getAttribute("currentUser");if(user == null){resp.sendRedirect("/login.jsp");return;}
// }}chain.doFilter(request, response);}public void destroy() {}@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}
}
web包
ShoppingAction (购物列表查询, 清空购物车, 删除购物车, 购物车结算)
package com.tang.web;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.fasterxml.jackson.databind.ObjectMapper;
import com.tang.dao.OrderDao;
import com.tang.dao.OrderItemDao;
import com.tang.entity.Order;
import com.tang.entity.OrderItem;
import com.tang.entity.User;
import com.tang.util.EasyuiResult;
import com.tang.util.ResponseUtil;
import com.tang.util.StringUtils;
import com.tang.vo.ShopGoodsVo;
import com.zking.framework.ActionSupport;
import com.zking.framework.ModelDriven;public class ShoppingAction extends ActionSupport implements ModelDriven<ShopGoodsVo>{private ShopGoodsVo shopGoodsVo = new ShopGoodsVo();private OrderDao orderDao = new OrderDao();private OrderItemDao orderItemDao = new OrderItemDao();// 加入购物车public String add(HttpServletRequest request, HttpServletResponse response) {ObjectMapper om = new ObjectMapper();User currentUser = (User) request.getSession().getAttribute("currentUser");long uid = currentUser.getId();String shopId = "shopId_" + uid;String shopVo = (String) request.getServletContext().getAttribute(shopId);List<ShopGoodsVo> shopGoodsVos = null;try {if (StringUtils.isNotBlank(shopVo)) {
// 购物车没有数据处理逻辑shopGoodsVos = (List<ShopGoodsVo>) om.readValue(shopVo, List.class);shopGoodsVos.add(shopGoodsVo);} else {
// 购物车有数据处理逻辑shopGoodsVos = new ArrayList<>();shopGoodsVos.add(shopGoodsVo);} request.getServletContext().setAttribute(shopId,om.writeValueAsString(shopGoodsVos));request.setAttribute("shopGoodsVos",shopGoodsVos);} catch (IOException e) {e.printStackTrace();}return "shoppingCar";}// 购物列表查询public String list(HttpServletRequest request, HttpServletResponse response) {ObjectMapper om = new ObjectMapper();try {User currentUser = (User) request.getSession().getAttribute("currentUser");long uid = currentUser.getId();String shopId = "shopId_" + uid;String shopVoStr = (String) request.getServletContext().getAttribute(shopId);List<ShopGoodsVo> shopGoodsVos = null;if (StringUtils.isNotBlank(shopVoStr)){shopGoodsVos = om.readValue(shopVoStr, List.class);}else{shopGoodsVos = new ArrayList<>();}request.setAttribute("shopGoodsVos",shopGoodsVos);} catch (IOException e) {e.printStackTrace();}return "shoppingCar";}// 清空购物车public String clear(HttpServletRequest reque
这篇关于网上书城的购物车功能(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!