In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use Java to achieve a personal blog system. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Operating environment: jdk8+tomcat8.5+mysql5.7+IntelliJ IDEA+maven
Project technology: springboot+mybatis+redis+vue+element ui
{return ResultGenerator.genFailResult ("comment content is too long");} 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));}}
Administrator Control layer:
/ * @ 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 params) {if (StringUtils.isEmpty (params.get ("page")) | | StringUtils.isEmpty (params.get ("limit") {return ResultGenerator.genFailResult ("Parameter exception!") ;} 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 ("Please enter a classification name!") ;} if (StringUtils.isEmpty (categoryIcon)) {return ResultGenerator.genFailResult ("Please select a category icon!") ;} if (categoryService.saveCategory (categoryName, categoryIcon)) {return ResultGenerator.genSuccessResult ();} else {return ResultGenerator.genFailResult ("duplicate classification name") }} / * * @ 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 ("Please enter a classification name!") ;} if (StringUtils.isEmpty (categoryIcon)) {return ResultGenerator.genFailResult ("Please select a category icon!") ;} if (categoryService.updateCategory (categoryId, categoryName, categoryIcon)) {return ResultGenerator.genSuccessResult ();} else {return ResultGenerator.genFailResult ("duplicate classification name") }} / * * @ 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 ("Parameter exception!") ;} if (categoryService.deleteBatch (ids)) {return ResultGenerator.genSuccessResult ();} else {return ResultGenerator.genFailResult (deletion failed);}
Handle administrator interface requests:
/ * handle administrator interface requests * * @ 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 / * handle login requests * * @ return java.lang.String * / @ GetMapping ({"/ login"}) public String login () {return "admin/login" } / * * Home page * * @ param request http request * @ 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" } / * login interface * * @ param userName user name * @ param password password * @ param verifyCode verification code * @ 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", "CAPTCHA cannot be empty") Return "admin/login";} if (StringUtils.isEmpty (userName) | | StringUtils.isEmpty (password)) {session.setAttribute ("errorMsg", "username or password cannot be empty"); return "admin/login";} String kaptchaCode = session.getAttribute ("verifyCode") + "" If (StringUtils.isEmpty (kaptchaCode) | |! verifyCode.equals (kaptchaCode)) {session.setAttribute ("errorMsg", "CAPTCHA error"); return "admin/login";} AdminUser adminUser = adminUserService.login (userName, password); if (adminUser! = null) {session.setAttribute ("loginUser", adminUser.getNickName ()) Session.setAttribute ("loginUserId", adminUser.getAdminUserId ()); / / session expiration time is set to 7200 seconds or two hours / / session.setMaxInactiveInterval (60 * 60 * 2); return "redirect:/admin/index";} else {session.setAttribute ("errorMsg", "login failed"); return "admin/login" }} / * modify personal information * * @ param request http request * @ 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" } / * change password * * @ param request http request * @ param originalPassword original password * @ param newPassword New password * @ 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 "parameter cannot be empty" } Integer loginUserId = (int) request.getSession (). GetAttribute ("loginUserId"); if (adminUserService.updatePassword (loginUserId, originalPassword, newPassword)) {/ / clear the data in session after successful modification, and the front-end control jumps to the login page request.getSession (). RemoveAttribute ("loginUserId"); request.getSession (). RemoveAttribute ("loginUser") Request.getSession () .removeAttribute ("errorMsg"); return "success";} else {return "modify failed" }} / * modify login Nickname * * @ param request http request * @ param loginUserName login * @ param nickName 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 "parameter cannot be empty" } Integer loginUserId = (int) request.getSession (). GetAttribute ("loginUserId"); if (adminUserService.updateName (loginUserId, loginUserName, nickName)) {return "success";} else {return "modification failed" }} / * Administrator exits * * @ param request http request * @ 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";}} this is the end of the article on "how to use Java to implement a personal blog system". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 287
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.