基于javaweb+jsp的在线购书商城系统(java+jsp+mysql+servlert+ajax)

一、项目简述
功能:一个基于JavaWeb的网上书店的设计与实现,归纳 出了几个模块,首先是登录注册模块,图书查找模块,购 物车模块,订单模块,个人中心模块,用户管理模块,图 书管理模块等。 该项目是javaJeb技术的实战操作,采用了MVC设计模 式,包括基本的entity, jscript, servlet,以及ajax异步请 求,查询分页,持久化层方法的封装等等,对javaweb技 术的巩固很有帮助,为J2EE的学习打下基础,适用于课程 设计,毕业设计。

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

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

java jsp网上商城 servlet 基于jsp的购物商城_User


java jsp网上商城 servlet 基于jsp的购物商城_mysql_02


java jsp网上商城 servlet 基于jsp的购物商城_mysql_03


java jsp网上商城 servlet 基于jsp的购物商城_User_04

适用

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

}
    /* @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);
}
    /**
     * 确认收货
     *
     * @param orderId
     * @return
     */
    @RequestMapping("/confirm/{orderId}")
    public String confirmReceiving(@PathVariable("orderId") String orderId, Model model) {
        BSResult bsResult = orderService.confirmReceiving(orderId);
} 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";
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() {
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private ICartService cartService;
    @Autowired
    private IBookInfoService bookInfoService;
    /**
     * 填写订单信息页面
     *
     * @param bookId
     * @param buyNum
     * @param request
     * @return
     */
    @GetMapping("/info")
    public String orderInfo(@RequestParam(required = false, defaultValue = "0") int 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) {
//查询书籍详情
        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查询
private IBookInfoService bookInfoService;
    @Autowired
    private BookDescMapper bookDescMapper;
    /**
     * 查询某一本书籍详情
     *
     * @param bookId
     * @param model
     * @return
     */
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";
* @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 {
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);
private IOrderService orderService;
    @Autowired
    private ICartService cartService;
    @Autowired
    private IBookInfoService bookInfoService;
    /**
     * 填写订单信息页面
     *
     * @param bookId
     * @param buyNum
     * @param request
     * @return
     */
    @GetMapping("/info")
    public String orderInfo(@RequestParam(required = false, defaultValue = "0") int bookId,
if (bsResult.getCode() == 200) {
            return "redirect:/order/list";
        }
        return "exception";
    }
    /**
     * 订单列表
     *
     * @return
     */
    @GetMapping("/list")
    public String orderList(HttpServletRequest request) {
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";
* @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);
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 {
*
     * @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 {
    @Autowired
* 确认收货
     *
     * @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";
        }
    }
}
书籍信息控制层:
@Controller
@RequestMapping("/book")
public class BookInfoController {
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) {
}
                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", "登录失败!");
@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";
@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
@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";
    }
    /**
     * 订单列表
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);
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()) {


java jsp网上商城 servlet 基于jsp的购物商城_搜索_05