欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实战网上电子书城的实现流程

程序员文章站 2022-03-09 22:55:33
项目描述: spring mvc +jsp实现的简单书城项目,可以在支付宝沙箱内实现支付运行环境: jdk8+tomcat9+mysql+intellij idea项目技术: spring+sprin...

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

运行环境: jdk8+tomcat9+mysql+intellij idea

项目技术: spring+spring mvc+mybatis+jsp+maven

Java实战网上电子书城的实现流程

Java实战网上电子书城的实现流程

Java实战网上电子书城的实现流程

Java实战网上电子书城的实现流程

Java实战网上电子书城的实现流程

Java实战网上电子书城的实现流程

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

@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", "您请求的资源不存在");
            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);
        //查询书籍推荐列表
        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 {
 
 
    @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";
         }
         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")
    @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(), "<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")
    @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实战网上电子书城的实现流程的文章就介绍到这了,更多相关java 网上电子书城内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!