基于javaweb+jsp的网上书城在线书店管理系统(java+jsp+servlert+mysql+ajax)

一、项目简述(附带IW文档)
功能: 前台: * 用户模块 * 分类模块 * 图书模块 * 购物车模块 * 订单模块

后台: * 管理员模块 * 分类管理模块 * 图书管理模块 * 订单模块

二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)

项目技术: JSP + C3P0+ Servlert + html+ css + JavaScript + JQuery + Ajax + Fileupload等等。

javas写网络书店 javaweb网上书店源代码_java


javas写网络书店 javaweb网上书店源代码_mysql_02


javas写网络书店 javaweb网上书店源代码_用户名_03


javas写网络书店 javaweb网上书店源代码_javas写网络书店_04


javas写网络书店 javaweb网上书店源代码_mysql_05

适用

课程设计,大作业,毕业设计,项目练习,学习演示等

* @return
     */
    @PostMapping("/buy/num/update")
    @ResponseBody
    public BSResult updateBuyNum(int bookId, int newNum, HttpServletRequest request){
        return cartService.updateBuyNum(bookId, newNum, request);
    }
    @PostMapping("/checkOne")
    @ResponseBody
    public BSResult checkACartItem(int bookId,HttpServletRequest request){
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        return cartService.checkedOrNot(cart, bookId);
    }
}
用户信息控制层:
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IMailService mailService;
    @Autowired
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")
* @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
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}")
}
    @GetMapping("/clear")
    public String clearCart(HttpServletRequest request) {
        cartService.clearCart(request,"cart");
        return "cart";
    }
    @GetMapping("/deletion/{bookId}")
    public String deleteCartItem(@PathVariable("bookId") int bookId,HttpServletRequest request){
        cartService.deleteCartItem(bookId, request);
        return "redirect:/cart/items";
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 "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 {
* 加入购物车
     *
     * @param bookId
     * @param request
     * @return
     */
    @RequestMapping("/addition")
    public String addToCart(@RequestParam(value = "bookId",defaultValue = "0") int bookId,
                            @RequestParam(required = false,defaultValue = "0") int buyNum,
                            HttpServletRequest request) {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //根据要加入购物车的bookId查询bookInfo
        BookInfo bookInfo = bookInfoService.queryBookAvailable(bookId);
        if (bookInfo != null) {
            //这本书在数据库里
            BSResult bsResult = cartService.addToCart(bookInfo, cart, buyNum);
            request.getSession().setAttribute("cart", bsResult.getData());
            request.setAttribute("bookInfo", bookInfo);
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";
        }
    }
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";
@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}")
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";
        }
        //未认证的用户
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();
Cart cart = (Cart)request.getSession().getAttribute("cart");
        return cartService.checkedOrNot(cart, bookId);
    }
}
用户信息控制层:
@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 = "用户名或密码错误";
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";
    }
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);
* @param bookId
     * @param newNum
     * @param request
     * @return
     */
    @PostMapping("/buy/num/update")
    @ResponseBody
    public BSResult updateBuyNum(int bookId, int newNum, HttpServletRequest request){
        return cartService.updateBuyNum(bookId, newNum, request);
    }
    @PostMapping("/checkOne")
    @ResponseBody
    public BSResult checkACartItem(int bookId,HttpServletRequest request){
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        return cartService.checkedOrNot(cart, bookId);
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) {
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(),"");
                    }
* @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")
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);
    }
}
订单信息控制层:
@Controller
@RequestMapping("/order")
public class OrderController {
}
    }
    @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";
        }
    }
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";
//获得未激活的用户
            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";
public String deleteCartItem(@PathVariable("bookId") int bookId,HttpServletRequest request){
        cartService.deleteCartItem(bookId, request);
        return "redirect:/cart/items";
    }
    /**
     * 更新某个购物车项的购买数量
     * @param bookId
     * @param newNum
     * @param request
     * @return
     */
    @PostMapping("/buy/num/update")
    @ResponseBody
    public BSResult updateBuyNum(int bookId, int newNum, HttpServletRequest request){
        return cartService.updateBuyNum(bookId, newNum, request);
    }
    @PostMapping("/checkOne")
    @ResponseBody
    public BSResult checkACartItem(int bookId,HttpServletRequest request){
        Cart cart = (Cart)request.getSession().getAttribute("cart");
        return cartService.checkedOrNot(cart, bookId);
    }
用户信息控制层:
@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";
        }
        //未认证的用户
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());

javas写网络书店 javaweb网上书店源代码_javas写网络书店_06