源码获取:博客首页 "资源" 里下载!

项目描述:

spring mvc +jsp实现的简单书城项目,可以在支付宝沙箱内实现支付

运行环境:

jdk8+tomcat9+mysql+IntelliJ IDEA

项目技术:

spring+spring mvc+mybatis+jsp+maven

后台管理员图书管理代码:

@Controller
@RequestMapping("/admin/book")
@RequiresPermissions("book-manage")
public class AdminBookController {@Autowiredprivate IBookInfoService bookInfoService;@Autowiredprivate BookDescMapper bookDescMapper;@Autowiredprivate IStoreService storeService;@Value("${image.url.prefix}")private String urlPrefix;@RequestMapping("toAddition")@RequiresPermissions("book-add")public String toAddition() {return "admin/book/add";}/*** 添加书籍**/@RequestMapping("/addition")@RequiresPermissions("book-add")public String addBook(BookInfo bookInfo, String bookDesc, MultipartFile pictureFile, HttpServletRequest request) throws Exception {uploadPicture(bookInfo, pictureFile, request);bookInfoService.saveBook(bookInfo, bookDesc);return "redirect:/admin/book/list";}/*** 书籍列表**/@RequestMapping(value = "/list")@RequiresPermissions("book-query")public String bookList(@RequestParam(defaultValue = "", required = false) String keywords,@RequestParam(value = "page", defaultValue = "1", required = false) int page,HttpSession session,Model model) {keywords = keywords.trim();Store store = (Store) session.getAttribute("loginStore");if (store != null) {PageInfo<BookInfo> books = bookInfoService.findBookListByCondition(keywords, 0, page, 10, store.getStoreId());model.addAttribute("bookPageInfo", books);model.addAttribute("keywords", keywords);} else {model.addAttribute("exception", "您请求的资源不存在");return "exception";}return "admin/book/list";}/*** 更新页面回显** @param bookId* @param model* @return* @throws Exception*/@RequestMapping("/echo")@RequiresPermissions("book-edit")public String echo(int bookId, Model model) throws BSException {BookInfo bookInfo = bookInfoService.adminFindById(bookId);BookDesc bookDesc = bookDescMapper.selectByPrimaryKey(bookInfo.getBookId());model.addAttribute("bookInfo", bookInfo);model.addAttribute("bookDesc", bookDesc);return "admin/book/edit";}@RequestMapping("/update")@RequiresPermissions("book-edit")public String updateBook(BookInfo bookInfo, String bookDesc, String keywords, MultipartFile pictureFile, HttpServletRequest request, RedirectAttributes ra) throws Exception {uploadPicture(bookInfo, pictureFile, request);BookInfo originBook = bookInfoService.findById(bookInfo.getBookId());bookInfoService.updateBook(bookInfo, bookDesc);//更新图片后,删除原来的图片String realPath = request.getServletContext().getRealPath("/");File uploadPic = new File(realPath + originBook.getImageUrl());uploadPic.delete();//重定向到书籍列表ra.addAttribute("keywords", keywords);return "redirect:/admin/book/list";}@RequestMapping("/deletion/{bookId}")@RequiresPermissions("book-delete")public String deletion(@PathVariable("bookId") int bookId, String keywords, RedirectAttributes ra, HttpServletRequest request) throws BSException {BookInfo bookInfo = bookInfoService.findById(bookId);String realPath = request.getServletContext().getRealPath("/");File uploadPic = new File(realPath + bookInfo.getImageUrl());uploadPic.delete();bookInfoService.deleteBook(bookId);ra.addAttribute("keywords", keywords);return "redirect:/admin/book/list";}@RequestMapping("/shelf")@RequiresPermissions("book-shelf")public String bookOffShelf(int bookId, int isShelf, String keywords, RedirectAttributes ra) {bookInfoService.changeShelfStatus(bookId, isShelf);ra.addAttribute("keywords", keywords);return "redirect:/admin/book/list";}private void uploadPicture(BookInfo bookInfo, MultipartFile pictureFile, HttpServletRequest request) throws IOException {if (pictureFile != null) {if (!StringUtils.isEmpty(pictureFile.getOriginalFilename())) {String realPath = request.getServletContext().getRealPath("/" + urlPrefix);//原始文件名称String pictureFileName = pictureFile.getOriginalFilename();//新文件名称String newFileName = IDUtils.genShortUUID() + pictureFileName.substring(pictureFileName.lastIndexOf("."));//上传图片File uploadPic = new File(realPath + File.separator + newFileName);//向磁盘写文件pictureFile.transferTo(uploadPic);bookInfo.setImageUrl(urlPrefix + File.separator + newFileName);}}}}

图书信息控制层:

@Controller
@RequestMapping("/book")
public class BookInfoController {@Autowiredprivate IBookInfoService bookInfoService;@Autowiredprivate BookDescMapper bookDescMapper;/*** 查询某一本书籍详情** @param bookId* @param model* @return*/@RequestMapping("/info/{bookId}")public String bookInfo(@PathVariable("bookId") Integer bookId, Model model) throws BSException {//查询书籍BookInfo bookInfo = bookInfoService.findById(bookId);//查询书籍推荐列表List<BookInfo> recommendBookList = bookInfoService.findBookListByCateId(bookInfo.getBookCategoryId(), 1, 5);//查询书籍详情BookDesc bookDesc = bookDescMapper.selectByPrimaryKey(bookId);//增加访问量bookInfoService.addLookMount(bookInfo);Collections.shuffle(recommendBookList);model.addAttribute("bookInfo", bookInfo);model.addAttribute("bookDesc", bookDesc);model.addAttribute("recommendBookList", recommendBookList);return "book_info";}/*** 通过关键字和书籍分类搜索书籍列表** @param keywords* @return*/@RequestMapping("/list")public String bookSearchList(@RequestParam(defaultValue = "", required = false) String keywords,@RequestParam(defaultValue = "0", required = false) int cateId,//分类Id,默认为0,即不按照分类Id查@RequestParam(defaultValue = "1", required = false) int page,@RequestParam(defaultValue = "6", required = false) int pageSize,Model model) {keywords = keywords.trim();PageInfo<BookInfo> bookPageInfo = bookInfoService.findBookListByCondition(keywords, cateId, page, pageSize,0);//storeId为0,不按照商店Id查询model.addAttribute("bookPageInfo", bookPageInfo);model.addAttribute("keywords", keywords);model.addAttribute("cateId", cateId);return "book_list";}}

用户管理控制层:

@Controller
@RequestMapping("/user")
public class UserController {@Autowiredprivate IUserService userService;@Autowiredprivate IMailService mailService;@Autowiredprivate IStoreService storeService;@Value("${mail.fromMail.addr}")private String from;@Value("${my.ip}")private String ip;private final String USERNAME_PASSWORD_NOT_MATCH = "用户名或密码错误";private final String USERNAME_CANNOT_NULL = "用户名不能为空";@RequestMapping("/login")public String login(@RequestParam(value = "username", required = false) String username,@RequestParam(value = "password", required = false) String password,HttpServletRequest request, Model model) {if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {return "login";}//未认证的用户Subject userSubject = SecurityUtils.getSubject();if (!userSubject.isAuthenticated()) {UsernamePasswordToken token = new UsernamePasswordToken(username, password);token.setRememberMe(false);//禁止记住我功能try {//登录成功userSubject.login(token);User loginUser = (User) userSubject.getPrincipal();request.getSession().setAttribute("loginUser", loginUser);Store store = storeService.findStoreByUserId(loginUser.getUserId());request.getSession().setAttribute("loginStore", store);SavedRequest savedRequest = WebUtils.getSavedRequest(request);String url = "/";if (savedRequest != null) {url = savedRequest.getRequestUrl();if(url.contains(request.getContextPath())){url = url.replace(request.getContextPath(),"");}}if(StringUtils.isEmpty(url) || url.equals("/favicon.ico")){url = "/";}return "redirect:" + url;} catch (UnknownAccountException | IncorrectCredentialsException uae) {model.addAttribute("loginMsg", USERNAME_PASSWORD_NOT_MATCH);return "login";} catch (LockedAccountException lae) {model.addAttribute("loginMsg", "账户已被冻结!");return "login";} catch (AuthenticationException ae) {model.addAttribute("loginMsg", "登录失败!");return "login";}} else {//用户已经登录return "redirect:/index";}}@RequestMapping("/info")public String personInfo(){return "user_info";}/* @RequestMapping("/login1")public String login1(@RequestParam(value = "username", required = false) String username,@RequestParam(value = "password", required = false) String password,Model model, HttpServletRequest request) {if (StringUtils.isEmpty(username)) {model.addAttribute("loginMsg", USERNAME_CANNOT_NULL);return "login";}if (StringUtils.isEmpty(password)) {model.addAttribute("loginMsg", "密码不能为空");return "login";}BSResult<User> bsResult = userService.login(username, password);//登录校验失败if (bsResult.getData() == null) {model.addAttribute("loginMsg", bsResult.getMessage());return "login";}//登录校验成功,重定向到首页User user = bsResult.getData();//置密码为空user.setPassword("");request.getSession().setAttribute("user", user);return "redirect:/";}*///shiro框架帮我们注销@RequestMapping("/logout")@CacheEvict(cacheNames="authorizationCache",allEntries = true)public String logout() {SecurityUtils.getSubject().logout();return "redirect:/page/login";}/*** 注册 检验用户名是否存在** @param username* @return*/@RequestMapping("/checkUserExist")@ResponseBodypublic BSResult checkUserExist(String username) {if (StringUtils.isEmpty(username)) {return BSResultUtil.build(200, USERNAME_CANNOT_NULL, false);}return userService.checkUserExistByUsername(username);}/*** 注册,发激活邮箱** @param user* @return*/@RequestMapping("/register")public String register(User user, Model model) {BSResult isExist = checkUserExist(user.getUsername());//尽管前台页面已经用ajax判断用户名是否存在,// 为了防止用户不是点击前台按钮提交表单造成的错误,后台也需要判断if ((Boolean) isExist.getData()) {user.setActive("1");BSResult bsResult = userService.saveUser(user);//获得未激活的用户User userNotActive = (User) bsResult.getData();/*  try {mailService.sendHtmlMail(user.getEmail(), "<dd书城>---用户激活---","<html><body><a href='http://"+ip+"/user/active?activeCode=" + userNotActive.getCode() + "'>亲爱的" + user.getUsername() +",请您点击此链接前往激活</a></body></html>");} catch (Exception e) {e.printStackTrace();model.addAttribute("registerError", "发送邮件异常!请检查您输入的邮箱地址是否正确。");return "fail";}*/model.addAttribute("username", user.getUsername());return "register_success";} else {//用户名已经存在,不能注册model.addAttribute("registerError", isExist.getMessage());return "register";}}@RequestMapping("/active")public String activeUser(String activeCode, Model model) {BSResult bsResult = userService.activeUser(activeCode);if (!StringUtils.isEmpty(bsResult.getData())) {model.addAttribute("username", bsResult.getData());return "active_success";} else {model.addAttribute("failMessage", bsResult.getMessage());return "fail";}}@RequestMapping("/update")@ResponseBodypublic BSResult updateUser(User user, HttpSession session){User loginUser = (User) session.getAttribute("loginUser");loginUser.setNickname(user.getNickname());loginUser.setLocation(user.getLocation());loginUser.setDetailAddress(user.getDetailAddress());loginUser.setGender(user.getGender());loginUser.setUpdated(new Date());loginUser.setPhone(user.getPhone());loginUser.setIdentity(user.getIdentity());loginUser.setPhone(user.getPhone());BSResult bsResult = userService.updateUser(loginUser);session.setAttribute("loginUser", loginUser);return bsResult;}@RequestMapping("/password/{userId}")@ResponseBodypublic BSResult changePassword(@PathVariable("userId") int userId,String oldPassword,String newPassword){if(StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)){return BSResultUtil.build(400, "密码不能为空");}return userService.compareAndChange(userId,oldPassword,newPassword);}}

订单管理控制层:

@Controller
@RequestMapping("/order")
public class OrderController {@Autowiredprivate IOrderService orderService;@Autowiredprivate ICartService cartService;@Autowiredprivate IBookInfoService bookInfoService;/*** 填写订单信息页面** @param bookId* @param buyNum* @param request* @return*/@GetMapping("/info")public String orderInfo(@RequestParam(required = false, defaultValue = "0") int bookId,@RequestParam(required = false, defaultValue = "0") int buyNum,HttpServletRequest request) throws BSException {if (bookId != 0) {//点了立即购买,放到request域中,也session的立即购买域中以区分购物车中的书籍BookInfo bookInfo = bookInfoService.findById(bookId);if (bookInfo != null) {BSResult bsResult = cartService.addToCart(bookInfo, null, buyNum);request.getSession().setAttribute("buyNowCart", bsResult.getData());request.setAttribute("cart", bsResult.getData());return "order_info";} else {request.setAttribute("exception", "不好意思,书籍库存不足或不存在了!");return "exception";}}//没有点立即购买,购物车中的总金额大于0才让填写订单信息Cart cart = (Cart) request.getSession().getAttribute("cart");if (cart != null && cart.getTotal() > 0) {return "order_info";} else {return "cart";}}@GetMapping("/payPage/{orderId}")public String toPay(@PathVariable("orderId") String orderId, Model model) {BSResult bsResult = orderService.findOrderById(orderId);if (bsResult.getCode() == 200) {model.addAttribute("order", bsResult.getData());return "payment";}return "exception";}@RequestMapping("/deletion/{orderId}")public String deletion(@PathVariable("orderId") String orderId) {BSResult bsResult = orderService.deleteOrder(orderId);if (bsResult.getCode() == 200) {return "redirect:/order/list";}return "exception";}/*** 订单列表** @return*/@GetMapping("/list")public String orderList(HttpServletRequest request) {User loginUser = (User) request.getSession().getAttribute("loginUser");List<OrderCustom> orderCustoms = orderService.findOrdersByUserId(loginUser.getUserId());request.setAttribute("orderCustoms", orderCustoms);return "order_list";}/*** 创建订单** @return*/@PostMapping("/creation")public String createOrder(User userDTO, String express, int payMethod, HttpServletRequest request) {//立即购买,优先创建订单Cart buyNowCart = (Cart) request.getSession().getAttribute("buyNowCart");User loginUser = (User) request.getSession().getAttribute("loginUser");userDTO.setUserId(loginUser.getUserId());userDTO.setZipCode(loginUser.getZipCode());if (buyNowCart != null) {BSResult bsResult = orderService.createOrder(buyNowCart, userDTO, express, payMethod);if (bsResult.getCode() == 200) {request.setAttribute("order", bsResult.getData());cartService.clearCart(request, "buyNowCart");return "payment";} else {request.setAttribute("exception", bsResult.getMessage());return "exception";}}//普通购物车Cart cart = (Cart) request.getSession().getAttribute("cart");if (cart != null) {BSResult bsResult = orderService.createOrder(cart, userDTO, express, payMethod);if (bsResult.getCode() == 200) {request.setAttribute("order", bsResult.getData());cartService.clearCart(request, "cart");return "payment";} else {request.setAttribute("exception", bsResult.getMessage());return "exception";}} else {request.setAttribute("exception", "购物车为空!");return "exception";}}/*** 确认收货** @param orderId* @return*/@RequestMapping("/confirm/{orderId}")public String confirmReceiving(@PathVariable("orderId") String orderId, Model model) {BSResult bsResult = orderService.confirmReceiving(orderId);if (bsResult.getCode() == 200) {return "redirect:/order/list";} else {model.addAttribute("exception", bsResult.getMessage());return "exception";}}
}

源码获取:博客首页 "资源" 里下载!

Java项目:网上电子书城项目(java+SSM+JSP+maven+Mysql)相关推荐

  1. Java项目:校园外卖点餐系统(java+SSM+JSP+maven+mysql)

    源码获取:博客首页 "资源" 里下载! 一.项目简述 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclisp ...

  2. 基于javaweb+SSM的校园外卖点餐系统(java+SSM+JSP+maven+mysql)

    一.项目简述 环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持) 项目技术: JSP ...

  3. Java项目:停车位租赁系统(java+SSM+JSP+Maven+mysql)

    源码获取:博客首页 "资源" 里下载! 项目介绍 该系统采用了经典的springmvc,spring,mybatis的框架组合,对于物业公司来说,有助于管理车位信息.系统分为了两个 ...

  4. SSM毕设项目网上书店1xy76(java+VUE+Mybatis+Maven+Mysql)

    SSM毕设项目网上书店1xy76(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Web ...

  5. JAVA毕设项目网上书店管理系统(java+VUE+Mybatis+Maven+Mysql)

    JAVA毕设项目网上书店管理系统(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Web ...

  6. JAVA毕设项目网上租房管理(java+VUE+Mybatis+Maven+Mysql)

    JAVA毕设项目网上租房管理(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Webst ...

  7. JAVA毕设项目网上投稿管理系统(java+VUE+Mybatis+Maven+Mysql)

    JAVA毕设项目网上投稿管理系统(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + HBuilderX(Web ...

  8. JAVA毕设项目html5在线医疗系统(Vue+Mybatis+Maven+Mysql+sprnig+SpringMVC)

    JAVA毕设项目html5在线医疗系统(Vue+Mybatis+Maven+Mysql+sprnig+SpringMVC) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql ...

  9. JAVA毕设项目公立医院绩效考核系统(Vue+Mybatis+Maven+Mysql+sprnig+SpringMVC)

    JAVA毕设项目公立医院绩效考核系统(Vue+Mybatis+Maven+Mysql+sprnig+SpringMVC) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 + Mysql + ...

最新文章

  1. ajax点赞只能点一次,php+mysql+ajax局部刷新点赞取消点赞功能(每个账号只点赞一次).pdf...
  2. Xamarin iOS教程之编辑界面编写代码
  3. squid代理服务器详解
  4. [开源] .Net ORM FreeSql 1.10.0 稳步向前
  5. css实现鼠标覆盖显示大图
  6. python将py文件编译成二进制文件 加密
  7. UI 假死的可能性和处理方法总结
  8. 重磅!阿里首推的“SpringBoot+Vue全栈项目”有多牛X?
  9. linux上机考试题(Linux基础)
  10. C#编译时提示未能解析引用的程序(被引用项目编译成功,但引用项目编译时却不能正常引用)
  11. chm打开秒退_【CHM+】CHM+下载_CHM+教程 _正版CHM+下载 -爱应用
  12. C语言每日一练---移动数组中的零元素
  13. 二十、观音、文殊两位菩萨变态大比拼
  14. 金融行业网络架构与技术探讨
  15. Ethyl 2-azidoacetate,637-81-0,叠氮乙酸乙酯MDL: MFCD00190177的分子量是129.117
  16. Codec2类的解析
  17. python microbit typeerror,在MicroPython中使用microbit模块时出现索引错误
  18. 背了黑锅以后,我找到了二师兄帮忙...
  19. 电机控制反Park变换和反Clarke变换公式推导
  20. 【Python游戏】基于化学方程式的基础上,用Python实现一个消灭泡泡小游戏 | 附源码

热门文章

  1. ATS 5.3.0日志字段分析(续)
  2. Unity 3D游戏开发学习教程
  3. Rocksdb Ribbon Filter : 结合 XOR-filter 以及 高斯消元算法 实现的 高效filter
  4. ceph osd混合部署和普通部署
  5. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学
  6. ntpdate[31915]: the NTP socket is in use, exiting
  7. PLSQL创建Oracle定时任务
  8. Moss/Sharepoint 一些很重要的API备忘
  9. 提高C#编程水平的50个要点
  10. CentOS装机必备-基本设置以及缺失文件