Java实战个人博客系统的实现流程
程序员文章站
2022-03-09 22:53:09
springboot+mybatis+前端vue,使用前后端分离架构实现的个人博客系统,共7个模块,首页,写博客,博客详情页,评论管理,文章分类,标签管理和文章归档。运行环境: jdk8+tomcat...
springboot+mybatis+前端vue,使用前后端分离架构实现的个人博客系统,共7个模块,首页,写博客,博客详情页,评论管理,文章分类,标签管理和文章归档。
运行环境: jdk8+tomcat8.5+mysql5.7+intellij idea+maven
项目技术: springboot+mybatis+redis+vue+element ui
博客管理控制层:
/** * @author yy */ @controller public class myblogcontroller { // public static string theme = "default"; public static string theme = "amaze"; @resource private blogservice blogservice; @resource private tagservice tagservice; @resource private commentservice commentservice; @resource private configservice configservice; @resource private categoryservice categoryservice; /** * 首页 * * @param request http请求 * @return java.lang.string */ @getmapping({"/", "/index", "index.html"}) public string index(httpservletrequest request) { return this.page(request, 1); } /** * 首页(带页码) * * @param request http请求 * @param pagenum 页码 * @return java.lang.string */ @getmapping({"/page/{pagenum}"}) public string page(httpservletrequest request, @pathvariable("pagenum") int pagenum) { pageresult blogpageresult = blogservice.getblogsforindexpage(pagenum); if (blogpageresult == null) { return "error/error_404"; } request.setattribute("blogpageresult", blogpageresult); request.setattribute("newblogs", blogservice.getbloglistforindexpage(1)); request.setattribute("hotblogs", blogservice.getbloglistforindexpage(0)); request.setattribute("hottags", tagservice.getblogtagcountforindex()); request.setattribute("pagename", "首页"); request.setattribute("configurations", configservice.getallconfigs()); return "blog/" + theme + "/index"; } /** * categories页面(包括分类数据和标签数据) * * @param request http请求 * @return java.lang.string */ @getmapping({"/categories"}) public string categories(httpservletrequest request) { request.setattribute("hottags", tagservice.getblogtagcountforindex()); request.setattribute("categories", categoryservice.getallcategories()); request.setattribute("pagename", "分类页面"); request.setattribute("configurations", configservice.getallconfigs()); return "blog/" + theme + "/category"; } /** * 详情页 * * @param request http请求 * @param blogid 博客id * @param commentpage 评论页 * @return java.lang.string */ @getmapping({"/blog/{blogid}", "/article/{blogid}"}) public string detail(httpservletrequest request, @pathvariable("blogid") long blogid, @requestparam(value = "commentpage", required = false, defaultvalue = "1") integer commentpage) { blogdetailvo blogdetailvo = blogservice.getblogdetail(blogid); if (blogdetailvo != null) { request.setattribute("blogdetailvo", blogdetailvo); request.setattribute("commentpageresult", commentservice.getcommentpagebyblogidandpagenum(blogid, commentpage)); } request.setattribute("pagename", "详情"); request.setattribute("configurations", configservice.getallconfigs()); return "blog/" + theme + "/detail"; } /** * 标签列表页 * * @param request http请求 * @param tagname 标签名称 * @return java.lang.string */ @getmapping({"/tag/{tagname}"}) public string tag(httpservletrequest request, @pathvariable("tagname") string tagname) { return tag(request, tagname, 1); } /** * 标签列表页(带页码) * * @param request http请求 * @param tagname 标签名称 * @param page 页码 * @return java.lang.string */ @getmapping({"/tag/{tagname}/{page}"}) public string tag(httpservletrequest request, @pathvariable("tagname") string tagname, @pathvariable("page") integer page) { pageresult blogpageresult = blogservice.getblogspagebytag(tagname, page); request.setattribute("blogpageresult", blogpageresult); request.setattribute("pagename", "标签"); request.setattribute("pageurl", "tag"); request.setattribute("keyword", tagname); request.setattribute("newblogs", blogservice.getbloglistforindexpage(1)); request.setattribute("hotblogs", blogservice.getbloglistforindexpage(0)); request.setattribute("hottags", tagservice.getblogtagcountforindex()); request.setattribute("configurations", configservice.getallconfigs()); return "blog/" + theme + "/list"; } /** * 分类列表页 * * @param request http请求 * @param categoryname 类别名称 * @return java.lang.string */ @getmapping({"/category/{categoryname}"}) public string category(httpservletrequest request, @pathvariable("categoryname") string categoryname) { return category(request, categoryname, 1); } /** * 分类列表页(带页码) * * @param request http请求 * @param categoryname 类别名称 * @param page 页码 * @return java.lang.string */ @getmapping({"/category/{categoryname}/{page}"}) public string category(httpservletrequest request, @pathvariable("categoryname") string categoryname, @pathvariable("page") integer page) { pageresult blogpageresult = blogservice.getblogspagebycategory(categoryname, page); request.setattribute("blogpageresult", blogpageresult); request.setattribute("pagename", "分类"); request.setattribute("pageurl", "category"); request.setattribute("keyword", categoryname); request.setattribute("newblogs", blogservice.getbloglistforindexpage(1)); request.setattribute("hotblogs", blogservice.getbloglistforindexpage(0)); request.setattribute("hottags", tagservice.getblogtagcountforindex()); request.setattribute("configurations", configservice.getallconfigs()); return "blog/" + theme + "/list"; } /** * 搜索列表页 * * @param request http请求 * @param keyword 关键词 * @return java.lang.string */ @getmapping({"/search/{keyword}"}) public string search(httpservletrequest request, @pathvariable("keyword") string keyword) { return search(request, keyword, 1); } /** * 搜索列表页(带页码) * * @param request http请求 * @param keyword 关键词 * @param page 页码 * @return java.lang.string */ @getmapping({"/search/{keyword}/{page}"}) public string search(httpservletrequest request, @pathvariable("keyword") string keyword, @pathvariable("page") integer page) { pageresult blogpageresult = blogservice.getblogspagebysearch(keyword, page); request.setattribute("blogpageresult", blogpageresult); request.setattribute("pagename", "搜索"); request.setattribute("pageurl", "search"); request.setattribute("keyword", keyword); request.setattribute("newblogs", blogservice.getbloglistforindexpage(1)); request.setattribute("hotblogs", blogservice.getbloglistforindexpage(0)); request.setattribute("hottags", tagservice.getblogtagcountforindex()); request.setattribute("configurations", configservice.getallconfigs()); return "blog/" + theme + "/list"; } /** * 评论留言 * * @param request http请求 * @param session session * @param blogid 博客id * @param verifycode 验证码 * @param commentator 评论者昵称 * @param email 邮箱 * @param websiteurl 留言者的网站 * @param commentbody 评论内容 * @return com.hbu.myblog.util.result */ @postmapping(value = "/blog/comment") @responsebody public result comment(httpservletrequest request, httpsession session, @requestparam long blogid, @requestparam string verifycode, @requestparam string commentator, @requestparam string email, @requestparam string websiteurl, @requestparam string commentbody) { if (stringutils.isempty(verifycode)) { return resultgenerator.genfailresult("验证码不能为空"); } string kaptchacode = session.getattribute("verifycode") + ""; if (stringutils.isempty(kaptchacode)) { return resultgenerator.genfailresult("非法请求"); } if (!verifycode.equals(kaptchacode)) { return resultgenerator.genfailresult("验证码错误"); } string ref = request.getheader("referer"); if (stringutils.isempty(ref)) { return resultgenerator.genfailresult("非法请求"); } if (null == blogid || blogid < 0) { return resultgenerator.genfailresult("非法请求"); } if (stringutils.isempty(commentator)) { return resultgenerator.genfailresult("请输入称呼"); } if (stringutils.isempty(email)) { return resultgenerator.genfailresult("请输入邮箱地址"); } if (!patternutil.isemail(email)) { return resultgenerator.genfailresult("请输入正确的邮箱地址"); } if (stringutils.isempty(commentbody)) { return resultgenerator.genfailresult("请输入评论内容"); } if (commentbody.trim().length() > 200) { return resultgenerator.genfailresult("评论内容过长"); } blogcomment comment = new blogcomment(); comment.setblogid(blogid); comment.setcommentator(myblogutils.cleanstring(commentator)); comment.setemail(email); if (patternutil.isurl(websiteurl)) { comment.setwebsiteurl(websiteurl); } comment.setcommentbody(myblogutils.cleanstring(commentbody)); return resultgenerator.gensuccessresult(commentservice.addcomment(comment)); } }
管理员控制层:
/** * @author yy */ @controller @requestmapping("/admin") public class categorycontroller { @resource private categoryservice categoryservice; /** * @param request * @return java.lang.string */ @getmapping("/categories") public string categorypage(httpservletrequest request) { request.setattribute("path", "categories"); return "admin/category"; } /** * @param params * @return com.hbu.myblog.util.result */ @requestmapping(value = "/categories/list", method = requestmethod.get) @responsebody public result list(@requestparam map<string, object> params) { if (stringutils.isempty(params.get("page")) || stringutils.isempty(params.get("limit"))) { return resultgenerator.genfailresult("参数异常!"); } pagequeryutil pageutil = new pagequeryutil(params); return resultgenerator.gensuccessresult(categoryservice.getblogcategorypage(pageutil)); } /** * @param categoryname * @param categoryicon * @return com.hbu.myblog.util.result */ @requestmapping(value = "/categories/save", method = requestmethod.post) @responsebody public result save(@requestparam("categoryname") string categoryname, @requestparam("categoryicon") string categoryicon) { if (stringutils.isempty(categoryname)) { return resultgenerator.genfailresult("请输入分类名称!"); } if (stringutils.isempty(categoryicon)) { return resultgenerator.genfailresult("请选择分类图标!"); } if (categoryservice.savecategory(categoryname, categoryicon)) { return resultgenerator.gensuccessresult(); } else { return resultgenerator.genfailresult("分类名称重复"); } } /** * @param categoryid * @param categoryname * @param categoryicon * @return com.hbu.myblog.util.result */ @requestmapping(value = "/categories/update", method = requestmethod.post) @responsebody public result update(@requestparam("categoryid") integer categoryid, @requestparam("categoryname") string categoryname, @requestparam("categoryicon") string categoryicon) { if (stringutils.isempty(categoryname)) { return resultgenerator.genfailresult("请输入分类名称!"); } if (stringutils.isempty(categoryicon)) { return resultgenerator.genfailresult("请选择分类图标!"); } if (categoryservice.updatecategory(categoryid, categoryname, categoryicon)) { return resultgenerator.gensuccessresult(); } else { return resultgenerator.genfailresult("分类名称重复"); } } /** * @param ids * @return com.hbu.myblog.util.result */ @requestmapping(value = "/categories/delete", method = requestmethod.post) @responsebody public result delete(@requestbody integer[] ids) { if (ids.length < 1) { return resultgenerator.genfailresult("参数异常!"); } if (categoryservice.deletebatch(ids)) { return resultgenerator.gensuccessresult(); } else { return resultgenerator.genfailresult("删除失败"); } } }
处理管理员界面请求:
/** * 处理管理员界面请求 * * @author yy */ @controller @requestmapping("/admin") public class admincontroller { @resource private adminuserservice adminuserservice; @resource private blogservice blogservice; @resource private categoryservice categoryservice; @resource private tagservice tagservice; @resource private commentservice commentservice; /** * 处理登录请求 * * @return java.lang.string */ @getmapping({"/login"}) public string login() { return "admin/login"; } /** * 主页 * * @param request http请求 * @return java.lang.string */ @getmapping({"", "/", "/index", "/index.html"}) public string index(httpservletrequest request) { request.setattribute("path", "index"); request.setattribute("categorycount", categoryservice.gettotalcategories()); request.setattribute("blogcount", blogservice.gettotalblogs()); request.setattribute("tagcount", tagservice.gettotaltags()); request.setattribute("commentcount", commentservice.gettotalcomments()); return "admin/index"; } /** * 登录界面 * * @param username 用户名 * @param password 密码 * @param verifycode 验证码 * @param session session * @return java.lang.string */ @postmapping(value = "/login") public string login(@requestparam("username") string username, @requestparam("password") string password, @requestparam("verifycode") string verifycode, httpsession session) { if (stringutils.isempty(verifycode)) { session.setattribute("errormsg", "验证码不能为空"); return "admin/login"; } if (stringutils.isempty(username) || stringutils.isempty(password)) { session.setattribute("errormsg", "用户名或密码不能为空"); return "admin/login"; } string kaptchacode = session.getattribute("verifycode") + ""; if (stringutils.isempty(kaptchacode) || !verifycode.equals(kaptchacode)) { session.setattribute("errormsg", "验证码错误"); return "admin/login"; } adminuser adminuser = adminuserservice.login(username, password); if (adminuser != null) { session.setattribute("loginuser", adminuser.getnickname()); session.setattribute("loginuserid", adminuser.getadminuserid()); //session过期时间设置为7200秒 即两小时 //session.setmaxinactiveinterval(60 * 60 * 2); return "redirect:/admin/index"; } else { session.setattribute("errormsg", "登陆失败"); return "admin/login"; } } /** * 修改个人信息 * * @param request http请求 * @return java.lang.string */ @getmapping("/profile") public string profile(httpservletrequest request) { integer loginuserid = (int) request.getsession().getattribute("loginuserid"); adminuser adminuser = adminuserservice.getuserdetailbyid(loginuserid); if (adminuser == null) { return "admin/login"; } request.setattribute("path", "profile"); request.setattribute("loginusername", adminuser.getloginusername()); request.setattribute("nickname", adminuser.getnickname()); return "admin/profile"; } /** * 修改密码 * * @param request http请求 * @param originalpassword 原始密码 * @param newpassword 新密码 * @return java.lang.string */ @postmapping("/profile/password") @responsebody public string passwordupdate(httpservletrequest request, @requestparam("originalpassword") string originalpassword, @requestparam("newpassword") string newpassword) { if (stringutils.isempty(originalpassword) || stringutils.isempty(newpassword)) { return "参数不能为空"; } integer loginuserid = (int) request.getsession().getattribute("loginuserid"); if (adminuserservice.updatepassword(loginuserid, originalpassword, newpassword)) { //修改成功后清空session中的数据,前端控制跳转至登录页 request.getsession().removeattribute("loginuserid"); request.getsession().removeattribute("loginuser"); request.getsession().removeattribute("errormsg"); return "success"; } else { return "修改失败"; } } /** * 修改登录名,昵称 * * @param request http请求 * @param loginusername 登录名 * @param nickname 昵称 * @return java.lang.string */ @postmapping("/profile/name") @responsebody public string nameupdate(httpservletrequest request, @requestparam("loginusername") string loginusername, @requestparam("nickname") string nickname) { if (stringutils.isempty(loginusername) || stringutils.isempty(nickname)) { return "参数不能为空"; } integer loginuserid = (int) request.getsession().getattribute("loginuserid"); if (adminuserservice.updatename(loginuserid, loginusername, nickname)) { return "success"; } else { return "修改失败"; } } /** * 管理员退出 * * @param request http请求 * @return java.lang.string */ @getmapping("/logout") public string logout(httpservletrequest request) { request.getsession().removeattribute("loginuserid"); request.getsession().removeattribute("loginuser"); request.getsession().removeattribute("errormsg"); return "admin/login"; } }
到此这篇关于java实战个人博客系统的实现流程的文章就介绍到这了,更多相关java 个人博客系统内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!