项目描述: 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 ibookinforvice bookinforvice; @autowired private bookdescmapper bookdescmapper; @autowired private istorervice storervice; @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, httprvletrequest request) throws exception { uploadpicture(bookinfo, picturefile, request); bookinforvice.savebook(bookinfo, bookdesc); return "redirect:/admin/book/list"; } /** * 书籍列表 * */ @requestmapping(value = "/list") @requirespermissions("book-query") public string booklist(@requestparam(defaultvalue = "", required = fal) string keywords, @requestparam(value = "page", defaultvalue = "1", required = fal) int page, httpssion ssion, model model) { keywords = keywords.trim(); store store = (store) ssion.getattribute("loginstore"); if (store != null) { pageinfo<bookinfo> books = bookinforvice.findbooklistbycondition(keywords, 0, page, 10, store.getstoreid()); model.addattribute("bookpageinfo", books); model.addattribute("keywords", keywords); } el { model.addattribute("exception", "您请求的资源不存在"); return "exception"; } return "admin/book/list"; } /** * 更新页面回显 * * @param bookid * @param model * @return * @throws exception */ @requestmapping("/echo") @requ爸爸别舔了快点irespermissions("book-edit") public string echo(int bookid, model model) throws bxception { bookinfo bookinfo = bookinforvice.adminfindbyid(bookid); bookdesc bookdesc = bookdescmapper.lectbyprimarykey(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, httprvletrequest request, redirectattributes ra) throws exception { uploadpicture(bookinfo, picturefile, request); bookinfo originbook = bookinforvice.findbyid(bookinfo.getbookid()); bookinforvice.updatebook(bookinfo, bookdesc); //更新图片后,删除原来的图片 string realpath = request.getrvletcontext().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, httprvletrequest request) throws bxception { bookinfo bookinfo = bookinforvice.findbyid(bookid); string realpath = request.getrvletcontext().getrealpath("/"); file uploadpic = new file(realpath + bookinfo.getimageurl()); uploadpic.delete(); bookinforvice.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) { bookinforvice.changeshelfstatus(bookid, isshelf); ra.addattribute("keywords", keywords); return "redirect:/admin/book/list"; } private void uploadpicture(bookinfo bookinfo, multipartfile picturefile, httprvletrequest request) throws ioexception { if (picturefile != null) { if (!stringutils.impty(picturefile.getoriginalfilename())) { string realpath = request.getrvletcontext().getrealpath("/" + urlprefix); //原始文件名称 string picturefilename = picturefile.getoriginalfilename(); //新文件名称 string newfilename = idutils.genshortuuid() + picturefilename.substring(picturefilename.lastindexof(".")); //上传图片 file uploadpic = new file(realpath + file.parator + newfilename); //向磁盘写文件 picturefile.transferto(uploadpic); bookinfo.timageurl(urlprefix + file.parator + newfilename); } } } }
书信息控制层:
@controller@requestmapping("/book")public class bookinfocontroller { @autowired private ibookinforvice bookinforvice; @autowired private bookdescmapper bookdescmapper; /** * 查询某一本书籍详情 * * @param bookid * @param model * @return */ @requestmapping("/info/{bookid}") public string bookinfo(@pathvariable("bookid") integer bookid, model model) throws bxception { //查询书籍 bookinfo bookinfo = bookinforvice.findbyid(bookid); //查询书籍推荐列表 list<bookinfo> recommendbooklist = bookinforvice.findbooklistbycateid(bookinfo.getbookcategoryid(), 1, 5); //查询书籍详情 bookdesc bookdesc = bookdescmapper.lectbyprimarykey(bookid); //增加访问量 bookinforvice.addlookmount(bookinfo); collections.shuffle(recommendbooklist); model.addattribute("bookinfo", bookinfo); model.addattribute("bookdesc", bookdesc); model.addattrib高考报志愿ute("recommendbooklist", recommendbooklist); return "book_info"; } /** * 通过关键字和书籍分类搜索书籍列表 * * @param keywords * @return */ @requestmapping("/list") public string bookarchlist(@requestparam(defaultvalue = "", required = fal) string keywords, @requestparam(defaultvalue = "0", required = fal) int cateid,//分类id,默认为0,即不按照分类id查 @requestparam(defaultvalue = "1", required = fal) int page, @requestparam(defaultvalue = "6", required = fal) int pagesize, model model) { keywords = keywords.trim(); pageinfo<bookinfo> bookpageinfo = bookinforvice.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("/ur")public class urcontroller { @autowired private iurrvice urrvice; @autowired private imailrvice mailrvice; @autowired private istorervice storervice; @value("${mail.frommail.addr}") private string from; @value("${my.ip}") private string ip; private final string urname_password_not_match = "用户名或密码错误"; private final string urname_cannot_null = "用户名不能为空"; @requestmapping("/login") public string login(@requestparam(value = "urname", required = fal) string urname, @requestparam(value = "password", required = fal) string password, httprvletrequest request, model model) { if (stringutils.impty(urname) || stringutils.impty(password)) { return "login"; } //未认证的用户 subject ursubject = curityutils.getsubject(); if (!ursubject.isauthenticated()) { urnamepasswordtoken token = new urnamepasswordtoken(urname, password); token.trememberme(fal);//禁止记住我功能 try { //登录成功 ursubject.login(token); ur loginur = (ur) ursubject.getprincipal(); request.getssion().tattribute("loginur", loginur); store store = storervice.findstorebyurid(loginur.geturid()); request.getssion().tattribute("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.impty(url) || url.equals("/favicon.ico")){ url = "/"; } return "redirect:" + url; } catch (unknownaccountexception | incorrectcredentialxception uae) { model.addattribute("loginmsg", urname_password_not_match); return "login"; } catch (lockedaccountexception lae) { model.addattribute("loginmsg", "账户已被冻结!"); return "login"; } catch (authenticationexception ae) { model.addattribute("loginmsg", "登录失败!"); return "login"; } } el { //用户已经登录 return "redirect:/index"; } } @requestmapping("/info") public string personinfo(){ return "ur_info"; } /* @requestmapping("/login1") public string login1(@requestparam(value = "urname", required = fal) string urname, @requestparam(value = "password", required = fal) string password, model model, httprvletrequest request) { if (stringutils.impty(urname)) { model.addattribute("loginmsg", urname_cannot_null); return "login"; } if (stringutils.impty(password)) { model.addattribute("loginmsg", "密码不能为空"); return "login"; } bsresult<ur> bsresult = urrvice.login(urname, password); //登录校验失败 if (bsresult.getdata() == null) { 如何去痘痘 model.addattribute("loginmsg", bsresult.getmessage()); return "login"; } //登录校验成功,重定向到首页 ur ur = bsresult.getdata(); //置密码为空 ur.tpassword(""); request.getssion().tattribute("ur", ur); return "redirect:/"; } */ //shiro框架帮我们注销 @requestmapping("/logout") @cacheevict(cachenames="authorizationcache",allentries = true) public string logout() { curityutils.getsubject().logout(); return "redirect:/page/login"; } /** * 注册 检验用户名是否存在 * * @param urnam哭笑不得类似的成语e * @return */ @requestmapping("/checkurexist") @responbody public bsresult checkurexist(string urname) { if (stringutils.impty(urname)) { return bsresultutil.build(200, urname_cannot_null, fal); } return urrvice.checkurexistbyurname(urname); } /** * 注册,发激活邮箱 * * @param ur * @return */ @requestmapping("/register") public string register(ur ur, model model) { bsresult ixist = checkurexist(ur.geturname()); //尽管前台页面已经用ajax判断用户名是否存在, // 为了防止用户不是点击前台按钮提交表单造成的错误,后台也需要判断 if ((boolean) ixist.getdata()) { ur.tactive("1"); bsresult bsresult = urrvice.saveur(ur); //获得未激活的用户 ur urnotactive = (ur) bsresult.getdata(); /* try { mailrvice.ndhtmlmail(ur.getemail(), "<dd书城>---用户激活---", "<html><body><a href='http://"+ip+"/ur/active?activecode=" + urnotactive.getcode() + "'>亲爱的" + ur.geturname() + ",请您点击此链接前往激活</a></body></html>"); } catch (exception e) { e.printstacktrace(); model.addattribute("registererror", "发送邮件异常!请检查您输入的邮箱地址是否正确。"); return "fail"; }*/ model.addattribute("urname", ur.geturname()); return "register_success"; } el { //用户名已经存在,不能注册 model.addattribute("registererror", ixist.getmessage()); return "register"; } } @requestmapping("/active") public string activeur(string activecode, model model) { bsresult bsresult = urrvice.activeur(activecode); if (!stringutils.impty(bsresult.getdata())) { model.addattribute("urname", bsresult.getdata()); return "active_success"; } el { model.addattribute("failmessage", bsresult.getmessage()); return "fail"; } } @requestmapping("/update") @responbody public bsresult updateur(ur ur, httpssion ssion){ ur loginur = (ur) ssion.getattribute("loginur"); loginur.tnickname(ur.getnickname()); loginur.tlocation(ur.getlocation()); loginur.tdetailaddress(ur.getdetailaddress()); loginur.tgender(ur.getgender()); loginur.tupdated(new date()); loginur.tphone(ur.getphone()); loginur.tidentity(ur.getidentity()); loginur.tphone(ur.getphone()); bsresult bsresult = urrvice.updateur(loginur); ssion.tattribute("loginur", loginur); return bsresult; } @requestmapping("/password/{urid}") @responbody public bsresult changepassword(@pathvariable("urid") int urid,string oldpassword,string new智者学派password){ if(stringutils.impty(oldpassword) || stringutils.impty(newpassword)){ return bsresultutil.build(400, "密码不能为空"); } return urrvice.compareandchange(urid,oldpassword,newpassword); } }
到此这篇关于java实战网上电子书城的实现流程的文章就介绍到这了,更多相关java 网上电子书城内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!
本文发布于:2023-04-04 09:37:59,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/dd7474025f6f921b0c0a9d18e5c36ada.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:Java实战网上电子书城的实现流程.doc
本文 PDF 下载地址:Java实战网上电子书城的实现流程.pdf
留言与评论(共有 0 条评论) |