您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何使用Java實現個人博客系統,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
運行環境: 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實現個人博客系統”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。