current position:Home>Java project: simple personal blog system (java + SSM + MySQL + JSP)

Java project: simple personal blog system (java + SSM + MySQL + JSP)

2022-01-27 05:07:20 qq_ one billion three hundred and thirty-four million six hundr

Spring+SpringMVC+Mybatis Implement a simple personal blog system :

The front desk is only responsible for checking blogs and comments , backstage : Article management 、 Comment management 、 Personal center 、 Change Password 、 Clear cache, etc

The operating environment is as follows : jdk1.8 Tomcat8.5/7.0 maven3.5/3.6 Mysql5.7 IDEA Windows/linux

 

 

Category control layer :



/**
 *  Category control layer 
 */
@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(" 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 the classification name !");
        }
        if (StringUtils.isEmpty(categoryIcon)) {
            return ResultGenerator.genFailResult(" Please select the category icon !");
        }
        if (categoryService.saveCategory(categoryName, categoryIcon)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(" Duplicate category 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 the classification name !");
        }
        if (StringUtils.isEmpty(categoryIcon)) {
            return ResultGenerator.genFailResult(" Please select the category icon !");
        }
        if (categoryService.updateCategory(categoryId, categoryName, categoryIcon)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(" Duplicate category 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(" Delete failed ");
        }
    }

}

 

Handle administrator interface requests :

/**
 *  Handle administrator interface requests 
 *
 */
@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;


    /**
     *  Processing 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 screen 
     *
     * @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", " Verification code cannot be empty ");
            return "admin/login";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", " User name or password cannot be empty ");
            return "admin/login";
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg", " Verification code error ");
            return "admin/login";
        }
        AdminUser adminUser = adminUserService.login(userName, password);
        if (adminUser != null) {
            session.setAttribute("loginUser", adminUser.getNickName());
            session.setAttribute("loginUserId", adminUser.getAdminUserId());
            //session The expiration time is set to 7200 second   That's 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  The 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 " The parameter cannot be null ";
        }
        Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
        if (adminUserService.updatePassword(loginUserId, originalPassword, newPassword)) {
            // Empty after modification session Data in , The front control jumps to the login page 
            request.getSession().removeAttribute("loginUserId");
            request.getSession().removeAttribute("loginUser");
            request.getSession().removeAttribute("errorMsg");
            return "success";
        } else {
            return " Modification failed ";
        }
    }

    /**
     *  Change the login name , nickname 
     *
     * @param request       http request 
     * @param loginUserName  Login name 
     * @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 " The parameter cannot be null ";
        }
        Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
        if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
            return "success";
        } else {
            return " Modification failed ";
        }
    }

    /**
     *  Administrator exit 
     *
     * @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";
    }
}

 

Blog control layer :

/**
 *  Blog control layer 
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private BlogService blogService;
    @Resource
    private CategoryService categoryService;

    /**
     *  Blog list 
     *
     * @param params  Parameters 
     * @return com.hbu.myblog.util.Result
     */
    @GetMapping("/blogs/list")
    @ResponseBody
    public Result list(@RequestParam Map<String, Object> 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(blogService.getBlogsPage(pageUtil));
    }


    /**
     * @param request http request 
     * @return java.lang.String
     */
    @GetMapping("/blogs")
    public String list(HttpServletRequest request) {
        request.setAttribute("path", "blogs");
        return "admin/blog";
    }

    /**
     * @param request http request 
     * @return java.lang.String
     */
    @GetMapping("/blogs/edit")
    public String edit(HttpServletRequest request) {
        request.setAttribute("path", "edit");
        request.setAttribute("categories", categoryService.getAllCategories());
        return "admin/edit";
    }

    /**
     * @param request http request 
     * @param blogId   Blog id
     * @return java.lang.String
     */
    @GetMapping("/blogs/edit/{blogId}")
    public String edit(HttpServletRequest request, @PathVariable("blogId") Long blogId) {
        request.setAttribute("path", "edit");
        Blog blog = blogService.getBlogById(blogId);
        if (blog == null) {
            return "error/error_400";
        }
        request.setAttribute("blog", blog);
        request.setAttribute("categories", categoryService.getAllCategories());
        return "admin/edit";
    }

    /**
     *  Add the article 
     *
     * @param blogTitle       Article title 
     * @param blogSummary     Abstract 
     * @param blogCategoryId  Category 
     * @param blogTags        label 
     * @param blogContent     Content 
     * @param blogStatus      draft , Release 
     * @param enableComment   Can you comment 
     * @return com.hbu.myblog.util.Result
     */
    @PostMapping("/blogs/save")
    @ResponseBody
    public Result save(@RequestParam("blogTitle") String blogTitle,
                       @RequestParam(name = "blogSummary", required = false) String blogSummary,
                       @RequestParam("blogCategoryId") Integer blogCategoryId,
                       @RequestParam("blogTags") String blogTags,
                       @RequestParam("blogContent") String blogContent,
                       @RequestParam("blogStatus") Byte blogStatus,
                       @RequestParam("enableComment") Byte enableComment) {
        if (StringUtils.isEmpty(blogTitle)) {
            return ResultGenerator.genFailResult(" Please enter the title of the article ");
        }
        if (blogTitle.trim().length() > 150) {
            return ResultGenerator.genFailResult(" The title is too long ");
        }
        if (StringUtils.isEmpty(blogTags)) {
            return ResultGenerator.genFailResult(" Please enter the article label ");
        }
        if (blogTags.trim().length() > 150) {
            return ResultGenerator.genFailResult(" The label is too long ");
        }
        if (blogSummary.trim().length() > 375) {
            return ResultGenerator.genFailResult(" The summary is too long ");
        }
        if (StringUtils.isEmpty(blogContent)) {
            return ResultGenerator.genFailResult(" Please enter the content of the article ");
        }
        if (blogTags.trim().length() > 100000) {
            return ResultGenerator.genFailResult(" The content of the article is too long ");
        }
        Blog blog = new Blog();
        blog.setBlogTitle(blogTitle);
        blog.setBlogSummary(blogSummary);
        blog.setBlogCategoryId(blogCategoryId);
        blog.setBlogTags(blogTags);
        blog.setBlogContent(blogContent);
        blog.setBlogStatus(blogStatus);
        blog.setEnableComment(enableComment);
        String saveBlogResult = blogService.saveBlog(blog);
        if ("success".equals(saveBlogResult)) {
            return ResultGenerator.genSuccessResult(" Add success ");
        } else {
            return ResultGenerator.genFailResult(saveBlogResult);
        }
    }

    /**
     *  Revise article 
     *
     * @param blogId          article ID
     * @param blogTitle       Article title 
     * @param blogSummary     Abstract 
     * @param blogCategoryId  Category 
     * @param blogTags        label 
     * @param blogContent     Content 
     * @param blogStatus      draft , Release 
     * @param enableComment   Can you comment 
     * @return com.hbu.myblog.util.Result
     */
    @PostMapping("/blogs/update")
    @ResponseBody
    public Result update(@RequestParam("blogId") Long blogId,
                         @RequestParam("blogTitle") String blogTitle,
                         @RequestParam(name = "blogSummary", required = false) String blogSummary,
                         @RequestParam("blogCategoryId") Integer blogCategoryId,
                         @RequestParam("blogTags") String blogTags,
                         @RequestParam("blogContent") String blogContent,
                         @RequestParam("blogStatus") Byte blogStatus,
                         @RequestParam("enableComment") Byte enableComment) {
        if (StringUtils.isEmpty(blogTitle)) {
            return ResultGenerator.genFailResult(" Please enter the title of the article ");
        }
        if (blogTitle.trim().length() > 150) {
            return ResultGenerator.genFailResult(" The title is too long ");
        }
        if (StringUtils.isEmpty(blogTags)) {
            return ResultGenerator.genFailResult(" Please enter the article label ");
        }
        if (blogTags.trim().length() > 150) {
            return ResultGenerator.genFailResult(" The label is too long ");
        }
        if (blogSummary.trim().length() > 375) {
            return ResultGenerator.genFailResult(" The summary is too long ");
        }
        if (StringUtils.isEmpty(blogContent)) {
            return ResultGenerator.genFailResult(" Please enter the content of the article ");
        }
        if (blogTags.trim().length() > 100000) {
            return ResultGenerator.genFailResult(" The content of the article is too long ");
        }
        Blog blog = new Blog();
        blog.setBlogId(blogId);
        blog.setBlogTitle(blogTitle);
        blog.setBlogSummary(blogSummary);
        blog.setBlogCategoryId(blogCategoryId);
        blog.setBlogTags(blogTags);
        blog.setBlogContent(blogContent);
        blog.setBlogStatus(blogStatus);
        blog.setEnableComment(enableComment);
        String updateBlogResult = blogService.updateBlog(blog);
        if ("success".equals(updateBlogResult)) {
            return ResultGenerator.genSuccessResult(" Modification successful ");
        } else {
            return ResultGenerator.genFailResult(updateBlogResult);
        }
    }


    /**
     *  according to id Delete the article 
     *
     * @param ids  To delete an article id list 
     * @return com.hbu.myblog.util.Result
     */
    @PostMapping("/blogs/delete")
    @ResponseBody
    public Result delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultGenerator.genFailResult(" Parameter exception !");
        }
        if (blogService.deleteBatch(ids)) {
            return ResultGenerator.genSuccessResult();
        } else {
            return ResultGenerator.genFailResult(" Delete failed ");
        }
    }

}

copyright notice
author[qq_ one billion three hundred and thirty-four million six hundr],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270507188294.html

Random recommended