欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)戰(zhàn)網(wǎng)上電子書城的實(shí)現(xiàn)流程

 更新時(shí)間:2022年01月18日 17:14:52   作者:qq_1334611189  
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+JSP+maven+Mysql實(shí)現(xiàn)一個(gè)網(wǎng)上電子書城,大家可以在過程中查缺補(bǔ)漏,提升水平

項(xiàng)目描述: spring mvc +jsp實(shí)現(xiàn)的簡(jiǎn)單書城項(xiàng)目,可以在支付寶沙箱內(nèi)實(shí)現(xiàn)支付

運(yùn)行環(huán)境: jdk8+tomcat9+mysql+IntelliJ IDEA

項(xiàng)目技術(shù): spring+spring mvc+mybatis+jsp+maven

 后臺(tái)管理員圖書管理代碼:

@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) {
            PageInfo<BookInfo> books = bookInfoService.findBookListByCondition(keywords, 0, page, 10, store.getStoreId());
            model.addAttribute("bookPageInfo", books);
            model.addAttribute("keywords", keywords);
        } else {
            model.addAttribute("exception", "您請(qǐng)求的資源不存在");
            return "exception";
        }
 
        return "admin/book/list";
    }
 
    /**
     * 更新頁(yè)面回顯
     *
     * @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);
        //查詢書籍推薦列表
        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";
    }
 
 
    /**
     * 通過關(guān)鍵字和書籍分類搜索書籍列表
     *
     * @param keywords
     * @return
     */
    @RequestMapping("/list")
    public String bookSearchList(@RequestParam(defaultValue = "", required = false) String keywords,
                                 @RequestParam(defaultValue = "0", required = false) int cateId,//分類Id,默認(rèn)為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 {
 
 
    @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 = "用戶名或密碼錯(cuò)誤";
 
    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";
        }
        //未認(rèn)證的用戶
        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", "賬戶已被凍結(jié)!");
                return "login";
            } catch (AuthenticationException ae) {
                model.addAttribute("loginMsg", "登錄失敗!");
                return "login";
            }
 
        } else {
            //用戶已經(jīng)登錄
            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);
         //登錄校驗(yàn)失敗
         if (bsResult.getData() == null) {
             model.addAttribute("loginMsg", bsResult.getMessage());
             return "login";
         }
         //登錄校驗(yàn)成功,重定向到首頁(yè)
         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";
    }
 
    /**
     * 注冊(cè) 檢驗(yàn)用戶名是否存在
     *
     * @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);
    }
 
    /**
     * 注冊(cè),發(fā)激活郵箱
     *
     * @param user
     * @return
     */
    @RequestMapping("/register")
    public String register(User user, Model model) {
 
        BSResult isExist = checkUserExist(user.getUsername());
 
        //盡管前臺(tái)頁(yè)面已經(jīng)用ajax判斷用戶名是否存在,
        // 為了防止用戶不是點(diǎn)擊前臺(tái)按鈕提交表單造成的錯(cuò)誤,后臺(tái)也需要判斷
        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() +
                                ",請(qǐng)您點(diǎn)擊此鏈接前往激活</a></body></html>");
            } catch (Exception e) {
                e.printStackTrace();
                model.addAttribute("registerError", "發(fā)送郵件異常!請(qǐng)檢查您輸入的郵箱地址是否正確。");
                return "fail";
            }*/
            model.addAttribute("username", user.getUsername());
            return "register_success";
        } else {
 
            //用戶名已經(jīng)存在,不能注冊(cè)
            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);
    }
 
}

到此這篇關(guān)于Java實(shí)戰(zhàn)網(wǎng)上電子書城的實(shí)現(xiàn)流程的文章就介紹到這了,更多相關(guān)Java 網(wǎng)上電子書城內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • JSON for java快速入門總結(jié)學(xué)習(xí)

    JSON for java快速入門總結(jié)學(xué)習(xí)

    這篇文章主要介紹了JSON for java入門總結(jié)學(xué)習(xí),有需要的可以了解一下。
    2016-11-11
  • 詳解SpringBoot封裝使用JDBC

    詳解SpringBoot封裝使用JDBC

    這篇文章主要介紹了SpringBoot封裝JDBC使用教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12
  • Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送

    Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送

    這篇文章主要介紹了Java實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送的相關(guān)資料,實(shí)例講解了java郵件發(fā)送實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Spring Cloud Config對(duì)特殊字符加密處理的方法詳解

    Spring Cloud Config對(duì)特殊字符加密處理的方法詳解

    這篇文章主要給大家介紹了關(guān)于Spring Cloud Config對(duì)特殊字符加密處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2018-05-05
  • 淺談Java中的this作為返回值時(shí)返回的是什么

    淺談Java中的this作為返回值時(shí)返回的是什么

    Java中的this作為返回值時(shí)返回的是什么?下面小編就為大家介紹一下Java中的this作為返回值時(shí)返回。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • 一文徹底掌握RocketMQ 的存儲(chǔ)模型

    一文徹底掌握RocketMQ 的存儲(chǔ)模型

    這篇文章主要介紹了RocketMQ 的存儲(chǔ)模型,本文的重點(diǎn)在于分析 BrokerServer 的消息存儲(chǔ)模型,筆者按照自己的理解 , 嘗試分析 RocketMQ 的存儲(chǔ)模型,需要的朋友可以參考下
    2022-12-12
  • 詳談java命令的本質(zhì)邏輯揭秘

    詳談java命令的本質(zhì)邏輯揭秘

    一個(gè)簡(jiǎn)單的java命令背后究竟做了些什么事情,很多朋友提出幾個(gè)問題,下面帶領(lǐng)大家一起學(xué)習(xí)Java命令的本質(zhì)邏輯問題,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • Springboot actuator應(yīng)用后臺(tái)監(jiān)控實(shí)現(xiàn)

    Springboot actuator應(yīng)用后臺(tái)監(jiān)控實(shí)現(xiàn)

    這篇文章主要介紹了Springboot actuator應(yīng)用后臺(tái)監(jiān)控實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • SpringBoot如何實(shí)現(xiàn)starter原理詳解

    SpringBoot如何實(shí)現(xiàn)starter原理詳解

    這篇文章主要介紹了SpringBoot如何實(shí)現(xiàn)starter原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Spring多數(shù)據(jù)源切換失敗,發(fā)現(xiàn)與事務(wù)相關(guān)問題

    Spring多數(shù)據(jù)源切換失敗,發(fā)現(xiàn)與事務(wù)相關(guān)問題

    這篇文章主要介紹了Spring多數(shù)據(jù)源切換失敗,發(fā)現(xiàn)與事務(wù)相關(guān)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評(píng)論