一、项目简述
功能:本系统分用户前台和管理员后台。 前台展示+后台管理,前台界面可实现用户登录,用户注 册,商品展示,商品明细展示,用户信息修改,密码修 改,购物车添加,商品购买,商品结算,订单查询,购物 车查询,后台管理可实现商品管理,订单管理,用户管理
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。
用户相关接口业务:
/** * 用户相关接口 */ @Controller @RequestMapping("/index") public class UserController{ @Resource private UserService userService; @Resource private OrderService orderService; @Resource private GoodService goodService; @Resource private TypeService typeService; @Resource private ShopcartService shopcartService; @Resource private SkuService skuService; /** * 用户注册 * @return */ @GetMapping("/register") public String reg(Model model) { model.addAttribute("flag", -1); // 注册页面 return "/index/register.jsp"; } /** * 用户注册 * @return */ @PostMapping("/register") public String register(Users user, Model model){ if (user.getUsername().isEmpty()) { model.addAttribute("msg", "用户名不能为空!"); return "/index/register.jsp"; }else if (userService.isExist(user.getUsername())) { model.addAttribute("msg", "用户名已存在!"); return "/index/register.jsp"; }else { String password = user.getPassword(); userService.add(user); user.setPassword(password); return "/index/index"; // 注册成功后转去登录 } } /** * 用户登录 * @return */ @GetMapping("/login") public String log() { return "/index/index"; } /** * 用户登录 * @return */ @PostMapping("/login") public String login(@RequestParam(required=false, defaultValue="0")int flag, Users user, HttpSession session, Model model) { model.addAttribute("typeList", typeService.getList()); if(flag==-1) { flag = 6; // 登录页面 return "/index/index"; } if(userService.checkUser(user.getUsername(), user.getPassword())){ Users loginUser = userService.get(user.getUsername()); session.setAttribute("user", loginUser); // 还原购物车数量 session.setAttribute("total", shopcartService.getTotal(loginUser.getId())); return "redirect:index"; } else { model.addAttribute("msg", "用户名或密码错误!"); return "/index/index"; } } /** * 注销登录 * @return */ @RequestMapping("/logout") public String logout(HttpSession session) { session.removeAttribute("user"); session.removeAttribute("order"); return "/index/index"; } /** * 查看购物车 * @return */ @RequestMapping("/shopcart") public String shopcart(Model model, HttpSession session) { Users user = (Users) session.getAttribute("user"); if (user == null) { model.addAttribute("msg", "请先登录!"); return "/index/index"; } model.addAttribute("typeList", typeService.getList()); model.addAttribute("shopcartList", shopcartService.getList(user.getId())); model.addAttribute("totalPrice", shopcartService.getTotalPrice(user.getId())); return "/index/shopcart.jsp"; } /** * 购买 * @return */ @RequestMapping("/buy") public @ResponseBody int buy(Shopcart shopcart, HttpSession session, Model model){ Users user = (Users) session.getAttribute("user"); if (user == null) { return -111; } shopcart.setUserId(user.getId()); shopcart.setGood(goodService.get(shopcart.getGoodId())); // 验证库存 // int stock = skuService.getStock(shopcart.getGoodId(), shopcart.getColorId(), shopcart.getSizeId()); // if(shopcart.getAmount() > stock) { // model.addAttribute("msg", "商品 [ " + shopcart.getGood().getName() + " ] 库存不足! 当前库存只有: " + stock); // } return orderService.save(Arrays.asList(shopcart), user); } /** * 购买 * @return */ @RequestMapping("/cart") public @ResponseBody int cart(Shopcart shopcart, HttpSession session, Model model){ Users user = (Users) session.getAttribute("user"); if (user == null) { return -111; } shopcart.setUserId(user.getId()); shopcartService.save(shopcart); int total = shopcartService.getTotal(user.getId()); session.setAttribute("total", total); return total; } /** * 添加 */ @RequestMapping("/add") public @ResponseBody boolean add(int skuid, HttpSession session){ Users user = (Users) session.getAttribute("user"); if (user == null) { return false; } return shopcartService.add(skuid); } /** * 减少 */ @RequestMapping("/less") public @ResponseBody boolean less(int skuid, HttpSession session){ Users user = (Users) session.getAttribute("user"); if (user == null) { return false; } return shopcartService.less(skuid); } /** * 删除 */ @RequestMapping("/delete") public @ResponseBody boolean delete(int skuid, HttpSession session){ Users user = (Users) session.getAttribute("user"); if (user == null) { return false; } shopcartService.delete(skuid); session.setAttribute("total", shopcartService.getTotal(user.getId())); return true; } /** * 总金额 * @return */ @RequestMapping("/total") public @ResponseBody int total(HttpSession session){ Users user = (Users) session.getAttribute("user"); if (user == null) { return -111; } return shopcartService.getTotalPrice(user.getId()); } /** * 提交订单 * @return */ @RequestMapping("/save") public String save(ServletRequest request, HttpSession session, Model model){ Users user = (Users) session.getAttribute("user"); if (user == null) { model.addAttribute("msg", "请先登录!"); return "/index/index"; } ListshopcartList = shopcartService.getList(user.getId()); if(Objects.isNull(shopcartList) || shopcartList.isEmpty()) { model.addAttribute("msg", "购物车没有商品"); return shopcart(model, session); } // 验证库存 for(Shopcart cart : shopcartList) { int stock = skuService.getStock(cart.getGoodId(), cart.getColorId(), cart.getSizeId()); if(cart.getAmount() > stock) { model.addAttribute("msg", "商品 [ " + cart.getGood().getName() + " ] 库存不足! 当前库存只有: " + stock); return shopcart(model, session); } } int orderid = orderService.save(shopcartList, user); if(orderid > 0) { // 清空购物车 session.setAttribute("total", shopcartService.getTotal(user.getId())); // 跳转支付 return "redirect:topay?orderid="+orderid; } model.addAttribute("msg", "出了点问题"); return shopcart(model, session); } /** * 支付页面 * @return */ @RequestMapping("/topay") public String topay(int orderid, ServletRequest request, HttpSession session) { Users user = (Users) session.getAttribute("user"); if (user == null) { request.setAttribute("msg", "请先登录!"); return "/index/index"; } request.setAttribute("typeList", typeService.getList()); request.setAttribute("order", orderService.get(orderid)); return "/index/pay.jsp"; } /** * 支付(模拟) * @return */ @RequestMapping("/pay") public String pay(Orders order, ServletRequest request, HttpSession session) { Users user = (Users) session.getAttribute("user"); if (user == null) { request.setAttribute("msg", "请先登录!"); return "/index/index"; } // 模拟支付 orderService.pay(order); request.setAttribute("typeList", typeService.getList()); request.setAttribute("order", orderService.get(order.getId())); request.setAttribute("msg", "支付成功! 即将跳转到订单列表"); return "/index/pay.jsp"; } /** * 查看订单 * @return */ @RequestMapping("/order") public String order(HttpSession session, Model model){ model.addAttribute("flag", 12); Users user = (Users) session.getAttribute("user"); if (user == null) { model.addAttribute("msg", "请登录后查看订单!"); return "/index/index"; } model.addAttribute("typeList", typeService.getList()); model.addAttribute("orderList", orderService.getListByUserid(user.getId())); return "/index/order.jsp"; } /** * 个人信息 * @return */ @RequestMapping("/my") public String my(HttpSession session, Model model){ model.addAttribute("flag", 11); model.addAttribute("typeList", typeService.getList()); Users user = (Users) session.getAttribute("user"); if (user == null) { model.addAttribute("msg", "请先登录!"); return "/index/index"; } model.addAttribute("user", user); return "/index/my.jsp"; } /** * 修改信息 * @return */ @RequestMapping("/updateUser") public String updateUser(Users user, HttpSession session, Model model){ model.addAttribute("flag", 11); model.addAttribute("typeList", typeService.getList()); Users userLogin = (Users) session.getAttribute("user"); if (userLogin == null) { model.addAttribute("msg", "请先登录!"); return "/index/index"; } // 修改资料 Users u = userService.get(userLogin.getId()); u.setName(user.getName()); u.setPhone(user.getPhone()); u.setAddress(user.getAddress()); userService.update(u); // 更新数据库 session.setAttribute("user", u); // 更新session model.addAttribute("msg", "信息修改成功!"); return "/index/my.jsp"; } /** * 修改信息 * @return */ @RequestMapping("/updatePassword") public String updatePassword(Users user, HttpSession session, Model model){ model.addAttribute("flag", 11); model.addAttribute("typeList", typeService.getList()); Users userLogin = (Users) session.getAttribute("user"); if (userLogin == null) { model.addAttribute("msg", "请先登录!"); return "/index/index"; } // 修改密码 Users u = userService.get(userLogin.getId()); if(user.getPasswordNew()!=null && !user.getPasswordNew().trim().isEmpty()) { if (user.getPassword()!=null && !user.getPassword().trim().isEmpty() && SafeUtil.encode(user.getPassword()).equals(u.getPassword())) { if (user.getPasswordNew()!=null && !user.getPasswordNew().trim().isEmpty()) { u.setPassword(SafeUtil.encode(user.getPasswordNew())); } userService.update(u); // 更新数据库 session.setAttribute("user", u); // 更新session model.addAttribute("msg", "密码修改成功!"); return "redirect:logout"; }else { model.addAttribute("msg", "原密码错误!"); } } return "/index/index"; } }
后台登录验证拦截器:
/** * 后台登录验证拦截器 */ public class AdminInterceptor extends HandlerInterceptorAdapter{ /** * 检测登录状态 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String uri = request.getRequestURI(); if(uri.contains("css/") || uri.contains("js/") || uri.contains("img/") || uri.contains("login") || uri.contains("logout")) { return true; // 不拦截路径 } Object username = request.getSession().getAttribute("username"); if (Objects.nonNull(username) && !username.toString().trim().isEmpty()) { return true; // 登录验证通过 } response.sendRedirect("login.jsp"); return false; // 其他情况一律拦截 } }
后台相关接口:
/** * 后台相关接口 */ @Controller @RequestMapping("/admin") public class AdminController { private static final int rows = 10; @Autowired private AdminService adminService; @Autowired private OrderService orderService; @Autowired private UserService userService; @Autowired private GoodService goodService; @Autowired private TopService topService; @Autowired private TypeService typeService; @Autowired private SkuService skuService; /** * 管理员登录 * @return */ @GetMapping("/login") public String log() { return "/admin/login.jsp"; } /** * 管理员登录 * @return */ @PostMapping("/login") public String login(Admins admin, HttpServletRequest request, HttpSession session) { if (adminService.checkUser(admin.getUsername(), admin.getPassword())) { session.setAttribute("username", admin.getUsername()); return "redirect:typeList"; } request.setAttribute("msg", "用户名或密码错误!"); return "/admin/login.jsp"; } /** * 退出 * @return */ @RequestMapping("/logout") public String logout(HttpSession session) { session.removeAttribute("admin"); return "/admin/login.jsp"; } /** * 后台首页 * @return */ @RequestMapping("/index") public String index(HttpServletRequest request) { request.setAttribute("msg", "恭喜你! 登录成功了"); return "/admin/index.jsp"; } /** * 类目列表 * * @return */ @RequestMapping("/typeList") public String typeList(HttpServletRequest request) { request.setAttribute("flag", 1); request.setAttribute("typeList", typeService.getList()); return "/admin/type_list.jsp"; } /** * 类目添加 * * @return */ @RequestMapping("/typeAdd") public String typeAdd(HttpServletRequest request) { request.setAttribute("flag", 1); return "/admin/type_add.jsp"; } /** * 类目添加 * * @return * @throws Exception */ @RequestMapping("/typeSave") public String typeSave(Types type, MultipartFile file, @RequestParam(required=false, defaultValue="1") int page) throws Exception { type.setCover(UploadUtil.upload(file)); typeService.add(type); return "redirect:typeList?flag=1&page="+page; } /** * 类目更新 * * @return */ @RequestMapping("/typeEdit") public String typeEdit(int id, HttpServletRequest request) { request.setAttribute("flag", 1); request.setAttribute("type", typeService.get(id)); return "/admin/type_edit.jsp"; } /** * 类目更新 * * @return * @throws Exception */ @RequestMapping("/typeUpdate") public String typeUpdate(Types type, MultipartFile file, @RequestParam(required=false, defaultValue="1") int page) throws Exception { if (Objects.nonNull(file) && !file.isEmpty()) { type.setCover(UploadUtil.upload(file)); } typeService.update(type); return "redirect:typeList?flag=1&page="+page; } /** * 类目删除 * * @return */ @RequestMapping("/typeDelete") public String typeDelete(Types type, @RequestParam(required=false, defaultValue="1") int page) { typeService.delete(type); return "redirect:typeList?flag=1&page="+page; } /** * sku列表 * * @return */ @RequestMapping("/skuList") public String skuList(@RequestParam(required=false, defaultValue="0")byte status, HttpServletRequest request) { request.setAttribute("flag", 2); request.setAttribute("status", status); request.setAttribute("skuList", status>0 ? skuService.getSizeList() : skuService.getColorList()); return "/admin/sku_list.jsp"; } /** * sku添加 * * @return */ @RequestMapping("/skuSave") public String skuSave(String name, int status) { if(status > 0) { skuService.addSize(name); }else { skuService.addColor(name); } return "redirect:skuList?status="+status; } /** * sku删除 * * @return */ @RequestMapping("/skuDelete") public String skuDelete(int id, int status) { if(status > 0) { skuService.deleteSize(id); }else { skuService.deleteColor(id); } return "redirect:skuList?status="+status; } /** * 产品列表 * * @return */ @RequestMapping("/goodList") public String goodList(@RequestParam(required=false, defaultValue="0")byte status, HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { request.setAttribute("flag", 3); request.setAttribute("page", page); request.setAttribute("status", status); request.setAttribute("goodList", goodService.getList(status, page, rows)); request.setAttribute("pageTool", PageUtil.getPageTool(request, goodService.getTotal(status), page, rows)); return "/admin/good_list.jsp"; } /** * 产品添加 * * @return */ @RequestMapping("/goodAdd") public String goodAdd(HttpServletRequest request) { request.setAttribute("flag", 3); request.setAttribute("typeList", typeService.getList()); request.setAttribute("colorList", skuService.getColorList()); request.setAttribute("sizeList", skuService.getSizeList()); return "/admin/good_add.jsp"; } /** * 产品添加 * * @return * @throws Exception */ @RequestMapping("/goodSave") public String goodSave(Goods good, MultipartFile file, @RequestParam(required=false, defaultValue="1") int page) throws Exception { good.setCover(UploadUtil.upload(file)); goodService.add(good); return "redirect:goodList?flag=3&page="+page; } /** * 产品更新 * * @return */ @RequestMapping("/goodEdit") public String goodEdit(int id, HttpServletRequest request) { request.setAttribute("flag", 3); request.setAttribute("typeList", typeService.getList()); request.setAttribute("colorList", skuService.getColorList()); request.setAttribute("sizeList", skuService.getSizeList()); request.setAttribute("good", goodService.get(id)); return "/admin/good_edit.jsp"; } /** * 产品更新 * * @return * @throws Exception */ @RequestMapping("/goodUpdate") public String goodUpdate(Goods good, MultipartFile file, @RequestParam(required=false, defaultValue="1") int page) throws Exception { if (Objects.nonNull(file) && !file.isEmpty()) { good.setCover(UploadUtil.upload(file)); } goodService.update(good); return "redirect:goodList?flag=3&page="+page; } /** * 产品删除 * * @return */ @RequestMapping("/goodDelete") public String goodDelete(int id, @RequestParam(required=false, defaultValue="1") int page) { goodService.delete(id); return "redirect:goodList?flag=3&page="+page; } /** * 添加推荐 * @return */ @RequestMapping("/topSave") public @ResponseBody String topSave(Tops tops, @RequestParam(required=false, defaultValue="0")byte status, @RequestParam(required=false, defaultValue="1") int page) { int id = topService.add(tops); return id > 0 ? "ok" : null; } /** * 删除推荐 * @return */ @RequestMapping("/topDelete") public @ResponseBody String topDelete(Tops tops, @RequestParam(required=false, defaultValue="0")byte status, @RequestParam(required=false, defaultValue="1") int page) { boolean flag = topService.delete(tops); return flag ? "ok" : null; } /** * 订单列表 * * @return */ @RequestMapping("/orderList") public String orderList(@RequestParam(required=false, defaultValue="0")byte status, HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { request.setAttribute("flag", 4); request.setAttribute("status", status); request.setAttribute("orderList", orderService.getList(status, page, rows)); request.setAttribute("pageTool", PageUtil.getPageTool(request, orderService.getTotal(status), page, rows)); return "/admin/order_list.jsp"; } /** * 订单发货 * * @return */ @RequestMapping("/orderDispose") public String orderDispose(int id, byte status, @RequestParam(required=false, defaultValue="1") int page) { orderService.dispose(id); return "redirect:orderList?flag=4&status="+status+"&page="+page; } /** * 订单完成 * * @return */ @RequestMapping("/orderFinish") public String orderFinish(int id, byte status, @RequestParam(required=false, defaultValue="1") int page) { orderService.finish(id); return "redirect:orderList?flag=4&status="+status+"&page="+page; } /** * 订单删除 * * @return */ @RequestMapping("/orderDelete") public String orderDelete(int id, byte status, @RequestParam(required=false, defaultValue="1") int page) { orderService.delete(id); return "redirect:orderList?flag=4&status="+status+"&page="+page; } /** * 顾客管理 * * @return */ @RequestMapping("/userList") public String userList(HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { request.setAttribute("flag", 5); request.setAttribute("userList", userService.getList(page, rows)); request.setAttribute("pageTool", PageUtil.getPageTool(request, userService.getTotal(), page, rows)); return "/admin/user_list.jsp"; } /** * 顾客添加 * * @return */ @RequestMapping("/userAdd") public String userAdd(HttpServletRequest request) { request.setAttribute("flag", 5); return "/admin/user_add.jsp"; } /** * 顾客添加 * * @return */ @RequestMapping("/userSave") public String userSave(Users user, HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { if (userService.isExist(user.getUsername())) { request.setAttribute("msg", "用户名已存在!"); return "/admin/user_add.jsp"; } userService.add(user); return "redirect:userList?flag=5&page="+page; } /** * 顾客密码重置页面 * * @return */ @RequestMapping("/userRe") public String userRe(int id, HttpServletRequest request) { request.setAttribute("flag", 5); request.setAttribute("user", userService.get(id)); return "/admin/user_reset.jsp"; } /** * 顾客密码重置 * * @return */ @RequestMapping("/userReset") public String userReset(Users user, @RequestParam(required=false, defaultValue="1") int page) { String password = SafeUtil.encode(user.getPassword()); user = userService.get(user.getId()); user.setPassword(password); userService.update(user); return "redirect:userList?page="+page; } /** * 顾客更新 * * @return */ @RequestMapping("/userEdit") public String userEdit(int id, HttpServletRequest request) { request.setAttribute("flag", 5); request.setAttribute("user", userService.get(id)); return "/admin/user_edit.jsp"; } /** * 顾客更新 * * @return */ @RequestMapping("/userUpdate") public String userUpdate(Users user, @RequestParam(required=false, defaultValue="1") int page) { userService.update(user); return "redirect:userList?flag=5&page="+page; } /** * 顾客删除 * * @return */ @RequestMapping("/userDelete") public String userDelete(Users user, @RequestParam(required=false, defaultValue="1") int page) { userService.delete(user); return "redirect:userList?flag=5&page="+page; } /** * 管理员列表 * * @return */ @RequestMapping("/adminList") public String adminList(HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { request.setAttribute("flag", 6); request.setAttribute("adminList", adminService.getList(page, rows)); request.setAttribute("pageTool", PageUtil.getPageTool(request, adminService.getTotal(), page, rows)); return "/admin/admin_list.jsp"; } /** * 管理员添加 * * @return */ @RequestMapping("/adminAdd") public String adminAdd(HttpServletRequest request) { request.setAttribute("flag", 6); return "/admin/admin_add.jsp"; } /** * 管理员密码重置 * * @return */ @RequestMapping("/adminRe") public String adminRe(int id, HttpServletRequest request) { request.setAttribute("flag", 6); request.setAttribute("admin", adminService.get(id)); return "/admin/admin_reset.jsp"; } /** * 管理员密码重置 * * @return */ @RequestMapping("/adminReset") public String adminReset(Admins admin, HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { String password = SafeUtil.encode(admin.getPassword()); admin = adminService.get(admin.getId()); admin.setPassword(password); adminService.update(admin); return "redirect:adminList?page="+page; } /** * 管理员添加 * * @return */ @RequestMapping("/adminSave") public String adminSave(Admins admin, HttpServletRequest request, @RequestParam(required=false, defaultValue="1") int page) { if (adminService.isExist(admin.getUsername())) { request.setAttribute("msg", "用户名已存在!"); return "/admin/admin_add.jsp"; } adminService.add(admin); return "redirect:adminList?flag=6&page="+page; } /** * 管理员修改 * * @return */ @RequestMapping("/adminEdit") public String adminEdit(int id, HttpServletRequest request) { request.setAttribute("flag", 6); request.setAttribute("admin", adminService.get(id)); return "/admin/admin_edit.jsp"; } /** * 管理员更新 * * @return */ @RequestMapping("/adminUpdate") public String adminUpdate(Admins admin, @RequestParam(required=false, defaultValue="1") int page) { admin.setPassword(SafeUtil.encode(admin.getPassword())); adminService.update(admin); return "redirect:adminList?flag=6&page="+page; } /** * 管理员删除 * * @return */ @RequestMapping("/adminDelete") public String adminDelete(Admins admin, @RequestParam(required=false, defaultValue="1") int page) { adminService.delete(admin); return "redirect:adminList?flag=6&page="+page; } }
到此这篇关于Java 文创商城系统的实现流程的文章就介绍到这了,更多相关Java 商城系统内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!