Java中如何实现网上电子书城
发表于:2025-01-18 作者:千家信息网编辑
千家信息网最后更新 2025年01月18日,这篇文章将为大家详细讲解有关Java中如何实现网上电子书城,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。项目描述: spring mvc +jsp实现的简单书城项目
千家信息网最后更新 2025年01月18日Java中如何实现网上电子书城
这篇文章将为大家详细讲解有关Java中如何实现网上电子书城,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
项目描述: spring mvc +jsp实现的简单书城项目,可以在支付宝沙箱内实现支付
运行环境: jdk8+tomcat9+mysql+IntelliJ IDEA
项目技术: spring+spring mvc+mybatis+jsp+maven
后台管理员图书管理代码:
@Controller@RequestMapping("/admin/book")@RequiresPermissions("book-manage")public class AdminBookController { @Autowired private IBookInfoService bookInfoService; @Autowired private BookDescMapper bookDescMapper; @Autowired private 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) { PageInfobooks = 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 { @Autowired private IBookInfoService bookInfoService; @Autowired private 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); //查询书籍推荐列表 ListrecommendBookList = 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 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 { @Autowired private IUserService userService; @Autowired private IMailService mailService; @Autowired private 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"; } BSResultbsResult = 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") @ResponseBody public 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(), " ---用户激活---", "亲爱的" + user.getUsername() + ",请您点击此链接前往激活"); } 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") @ResponseBody public 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}") @ResponseBody public 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); } }
关于"Java中如何实现网上电子书城"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
用户
书籍
用户名
查询
登录
密码
激活
书城
图片
文件
篇文章
项目
分类
管理
电子
成功
前台
后台
更多
详情
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
物理服务器每天固定时间卡顿
安徽app软件开发费用明细
网络技术能考电工证吗
pyweb服务器那个库好
数据库触发程序
wifi高级网络安全
基岩国际版起床服务器
软件开发主修什么课程
阿里云服务器 登录
网络安全知识读本答题
dgx 服务器
软件系统服务器资源怎么分配
苹果注册id时无法连接服务器
网络安全社会调查报告范文
大连网络安全黑板报
聊天软件开发总工程师
软件开发开多少点的税率
网络安全法存在的漏洞
服务器生存第5期
目前的网络安全现状分析
春运订票软件开发
网络技术的目标分解
网络安全工程师的工作内容是什么
网络安全法法律责任热
易到互联网科技
联想s系列服务器
目前数据库管理产品有哪些
江西戴尔服务器虚拟化价格
派啦网络技术有限公司
服务器产品型号对比