• Java项目:SSM个人博客网站管理系统


    作者主页:源码空间站2022

     简介:Java领域优质创作者、Java项目、学习资料、技术互助

    文末获取源码

    项目介绍

    本项目包含管理员与游客两种角色;

    管理员角色包含以下功能:

    发表文章,查看文章,类别管理,添加类别,个人信息管理,评论管理,评论审核等功能。

    游客角色包含以下功能:
    首页,博客详情,文章分类,评论等功能。

    环境需要

    1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
    2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
    3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
    4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 

    5.数据库:MySql 5.7版本;

    6.是否Maven项目:是;

    技术栈

    1. 后端:Spring+SpringMVC+Mybatis

    2. 前端:JSP+CSS+JavaScript+bootstrap+jquery

    使用说明

    1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;

    2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;

    若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;

    3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;

    4. 运行项目,输入localhost:8080/personal_blog 登录

    运行截图

    游客角色

    管理员角色

     

    相关代码

    BlogAdminController

    1. package com.june.web.controller.admin;
    2. import java.io.File;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. import java.io.InputStream;
    6. import java.util.HashMap;
    7. import java.util.List;
    8. import java.util.Map;
    9. import javax.annotation.Resource;
    10. import javax.servlet.http.HttpServletRequest;
    11. import org.springframework.stereotype.Controller;
    12. import org.springframework.ui.Model;
    13. import org.springframework.web.bind.annotation.RequestMapping;
    14. import org.springframework.web.bind.annotation.RequestParam;
    15. import org.springframework.web.multipart.MultipartFile;
    16. import com.june.lucene.BlogIndex;
    17. import com.june.model.Blog;
    18. import com.june.model.BlogType;
    19. import com.june.model.PageBean;
    20. import com.june.service.BlogService;
    21. import com.june.service.BlogTypeService;
    22. import com.june.service.CommentService;
    23. import com.june.util.Constants;
    24. import com.june.util.DateUtils;
    25. import com.june.util.PageUtils;
    26. import com.june.util.StringUtils;
    27. import lombok.extern.slf4j.Slf4j;
    28. /**
    29. * 博客Controller
    30. */
    31. @Controller
    32. @RequestMapping("/blog")
    33. public @Slf4j class BlogAdminController {
    34. @Resource
    35. private BlogService blogService;
    36. @Resource
    37. private BlogTypeService blogTypeService;
    38. @Resource
    39. private CommentService commentService;
    40. @Resource
    41. private BlogIndex blogIndex;
    42. @RequestMapping("/list")
    43. public String list(@RequestParam(defaultValue = "1") Integer page,
    44. @RequestParam(defaultValue = Constants.DEFAULT_PAGE_SIZE - 1 + "") Integer pageSize,
    45. String firstDate, String secondDate, Integer typeId, String title, Model model,
    46. HttpServletRequest request) {
    47. Map map = new HashMap<>(6);
    48. map.put("typeId", typeId);
    49. map.put("title", title);
    50. map.put("firstDate", firstDate);
    51. map.put("secondDate", secondDate);
    52. int totalCount = blogService.getCount(map);
    53. PageBean pageBean = new PageBean(totalCount, page, pageSize);
    54. map.put("start", pageBean.getStart());
    55. map.put("size", pageSize);
    56. model.addAttribute("pagination", pageBean);
    57. List blogTypeList = blogTypeService.getTypeList();
    58. model.addAttribute("blogTypeList",blogTypeList);
    59. StringBuilder param = new StringBuilder(); // 分页查询参数
    60. param.append(StringUtils.isEmpty(title) ? "" : "title=" + title);
    61. param.append(StringUtils.isEmpty(firstDate) ? "" : "&firstDate=" + firstDate);
    62. param.append(StringUtils.isEmpty(secondDate) ? "" : "&secondDate=" + secondDate);
    63. param.append(typeId == null ? "" : "&typeId=" + typeId);
    64. String pageCode = PageUtils.genPagination(request.getContextPath() + "/blog/list.do",
    65. pageBean, param.toString());
    66. model.addAttribute("pageCode", pageCode);
    67. model.addAttribute("entry", map);
    68. model.addAttribute("blogList", blogService.getBlogList(map));
    69. return "blog/list";
    70. }
    71. @RequestMapping("/toAdd")
    72. public String toAdd(Model model) {
    73. model.addAttribute("blogTypeList", blogTypeService.getTypeList());
    74. return "blog/add";
    75. }
    76. @RequestMapping("/toUpdate")
    77. public String toUpdate(Integer id, Model model) {
    78. model.addAttribute("blogTypeList", blogTypeService.getTypeList());
    79. Blog blog = blogService.findById(id);
    80. model.addAttribute("blog", blog);
    81. BlogType blogType = blog.getBlogType();
    82. if(blogType != null){
    83. model.addAttribute("typeId", blogType.getTypeId());
    84. }
    85. return "blog/update";
    86. }
    87. @RequestMapping("/add")
    88. public void add(Blog blog, @RequestParam(value = "img") MultipartFile file,
    89. Model model) throws Exception {
    90. // 获取原始文件名
    91. String fileName = file.getOriginalFilename();
    92. int index = fileName.indexOf(".");
    93. String imageUrl = null;
    94. String imagePath = DateUtils.getTimeStrForImage();
    95. if (index != -1) {
    96. //生成新文件名
    97. imageUrl = imagePath + fileName.substring(index);
    98. log.info("add {}", imagePath);
    99. handleFileUpload(file, imageUrl);
    100. blog.setImage(imageUrl);
    101. }
    102. // 添加博客及索引
    103. int result = blogService.add(blog);
    104. model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
    105. }
    106. @RequestMapping("/update")
    107. public void update(Blog blog, @RequestParam(value = "img", required=false) MultipartFile file,
    108. Model model) throws Exception {
    109. if (file != null) { //上传图片
    110. // 获取原始文件名
    111. String fileName = file.getOriginalFilename();
    112. int index = fileName.indexOf(".");
    113. String imageUrl = null;
    114. String imagePath = DateUtils.getTimeStrForImage();
    115. if(index != -1){
    116. //生成新文件名
    117. imageUrl = imagePath + fileName.substring(index);
    118. log.info("update {}", imagePath);
    119. handleFileUpload(file,imageUrl);
    120. blog.setImage(imageUrl);
    121. }
    122. }
    123. //更新博客及索引
    124. int result = blogService.update(blog);
    125. model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
    126. }
    127. @RequestMapping("/delete")
    128. public String delete(Integer id) throws IOException {
    129. // 删除博客、索引及评论
    130. blogService.delete(id);
    131. return "redirect:/blog/list.do";
    132. }
    133. @RequestMapping("/deletes")
    134. public String deletes(String ids) throws IOException {
    135. String[] idArr = org.springframework.util.StringUtils.commaDelimitedListToStringArray(ids);
    136. int len = idArr.length;
    137. Integer[] blogIds = new Integer[len];
    138. for (int i = 0; i < len; i++) {
    139. blogIds[i] = Integer.parseInt(idArr[i]);
    140. }
    141. blogService.batchDelete(blogIds);
    142. return "redirect:/blog/list.do";
    143. }
    144. private void handleFileUpload(MultipartFile file, String imageUrl) {
    145. try (InputStream is = file.getInputStream()) {
    146. // 获取输入流
    147. String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)+Constants.COVER_DIR + imageUrl;
    148. File dir = new File(filePath.substring(0, filePath.lastIndexOf("/")));
    149. //判断上传目录是否存在
    150. if (!dir.exists()) {
    151. dir.mkdirs();
    152. }
    153. try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
    154. byte[] buffer = new byte[1024];
    155. int len = 0;
    156. // 读取输入流中的内容
    157. while ((len = is.read(buffer)) != -1) {
    158. fos.write(buffer, 0, len);
    159. }
    160. }
    161. } catch (Exception e) {
    162. log.error("图片上传失败", e);
    163. }
    164. }
    165. }

    BloggerAdminController 

    1. package com.june.web.controller.admin;
    2. import java.io.File;
    3. import java.io.FileOutputStream;
    4. import java.io.InputStream;
    5. import javax.annotation.Resource;
    6. import javax.servlet.http.HttpServletRequest;
    7. import javax.servlet.http.HttpServletResponse;
    8. import org.springframework.stereotype.Controller;
    9. import org.springframework.ui.Model;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RequestParam;
    12. import org.springframework.web.multipart.MultipartFile;
    13. import com.june.model.Blogger;
    14. import com.june.service.BloggerService;
    15. import com.june.service.CommentService;
    16. import com.june.util.Constants;
    17. import com.june.util.DateUtils;
    18. import com.june.util.ResponseUtils;
    19. import com.june.util.ShiroUtils;
    20. import lombok.extern.slf4j.Slf4j;
    21. @Controller
    22. @RequestMapping("/blogger")
    23. public @Slf4j class BloggerAdminController {
    24. @Resource
    25. private CommentService commentService;
    26. @Resource
    27. private BloggerService bloggerService;
    28. @RequestMapping("/toModifyInfo")
    29. public String toModifyInfo(Model model) {
    30. model.addAttribute("blogger", bloggerService.find());
    31. return "blogger/modifyInfo";
    32. }
    33. @RequestMapping("/toModifyPassword")
    34. public String toModifyPassword() {
    35. return "blogger/modifyPassword";
    36. }
    37. @RequestMapping("/modifyInfo")
    38. public void modifyInfo(Blogger blogger,
    39. @RequestParam(value = "img", required = false) MultipartFile file,
    40. Model model) throws Exception {
    41. if (file != null) { //上传图片
    42. // 获取原始文件名
    43. String fileName = file.getOriginalFilename();
    44. int index = fileName.indexOf(".");
    45. String imageUrl = null;
    46. if (index != -1) {
    47. // 生成新文件名
    48. imageUrl = DateUtils.getTimeStrForImage() + fileName.substring(index);
    49. handleFileUpload(file, imageUrl);
    50. blogger.setImageUrl(imageUrl);
    51. }
    52. }
    53. int result = bloggerService.update(blogger);
    54. model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
    55. }
    56. @RequestMapping("/modifyPassword")
    57. public void modifyPassword(String newpwd, String oldpwd, String repwd, HttpServletResponse response,
    58. HttpServletRequest request) {
    59. Blogger blogger = bloggerService.find();
    60. if (!blogger.getPassword().equals(ShiroUtils.encryptPassword(oldpwd))) {
    61. ResponseUtils.writeText(response, "原密码输入不正确");
    62. return;
    63. }
    64. if (!newpwd.equals(repwd)) {
    65. ResponseUtils.writeText(response, "两次密码输入不一致");
    66. return;
    67. }
    68. blogger.setPassword(ShiroUtils.encryptPassword(newpwd));
    69. bloggerService.update(blogger);
    70. ResponseUtils.writeText(response, "修改成功");
    71. }
    72. private void handleFileUpload(MultipartFile file,String imageUrl) {
    73. try (InputStream is = file.getInputStream()) {
    74. // 获取输入流
    75. String filePath = Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(0,Thread.currentThread().getContextClassLoader().getResource("").getPath().length()-16)+Constants.AVATAR_DIR + imageUrl;
    76. File dir = new File(filePath.substring(0, filePath.lastIndexOf("/")));
    77. //判断上传目录是否存在
    78. if (!dir.exists()) {
    79. dir.mkdirs();
    80. }
    81. try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
    82. byte[] buffer = new byte[1024];
    83. int len = 0;
    84. // 读取输入流中的内容
    85. while ((len = is.read(buffer)) != -1) {
    86. fos.write(buffer, 0, len);
    87. }
    88. }
    89. } catch (Exception e) {
    90. log.error("图片上传失败", e);
    91. }
    92. }
    93. }

    BlogTypeAdminController

    1. package com.june.web.controller.admin;
    2. import java.io.IOException;
    3. import java.util.HashMap;
    4. import java.util.List;
    5. import java.util.Map;
    6. import javax.annotation.Resource;
    7. import javax.servlet.http.HttpServletRequest;
    8. import javax.servlet.http.HttpServletResponse;
    9. import org.json.JSONObject;
    10. import org.springframework.stereotype.Controller;
    11. import org.springframework.ui.Model;
    12. import org.springframework.web.bind.annotation.RequestMapping;
    13. import org.springframework.web.bind.annotation.RequestParam;
    14. import com.june.model.BlogType;
    15. import com.june.model.PageBean;
    16. import com.june.service.BlogService;
    17. import com.june.service.BlogTypeService;
    18. import com.june.util.Constants;
    19. import com.june.util.PageUtils;
    20. import com.june.util.ResponseUtils;
    21. /**
    22. * 博客类别Controller
    23. */
    24. @Controller
    25. @RequestMapping("/blogType")
    26. public class BlogTypeAdminController {
    27. @Resource
    28. private BlogTypeService blogTypeService;
    29. @Resource
    30. private BlogService blogService;
    31. @RequestMapping("/list")
    32. public String list(@RequestParam(defaultValue = "1") Integer page,
    33. @RequestParam(defaultValue = Constants.BACK_PAGE_SIZE + 3 + "") Integer pageSize,
    34. Model model, HttpServletRequest request) {
    35. int totalCount = blogTypeService.getCount();
    36. PageBean pageBean = new PageBean(totalCount, page, pageSize);
    37. Map params = new HashMap<>(2);
    38. params.put("start", pageBean.getStart());
    39. params.put("size", pageSize);
    40. List blogTypeList = blogTypeService.getTypeList(params);
    41. blogTypeList.forEach(blogType -> {
    42. Map types = new HashMap<>(1);
    43. types.put("typeId", blogType.getTypeId());
    44. Integer blogCount = blogService.getCount(types);
    45. blogType.setBlogCount(blogCount);
    46. });
    47. model.addAttribute("pagination", pageBean);
    48. String targetUrl = request.getContextPath() + "/blogType/list.do";
    49. String pageCode = PageUtils.genPagination(targetUrl, pageBean, "");
    50. model.addAttribute("pageCode", pageCode);
    51. model.addAttribute("entry", params);
    52. model.addAttribute("blogTypeList", blogTypeList);
    53. return "blogType/list";
    54. }
    55. @RequestMapping("/toAdd")
    56. public String toAdd() {
    57. return "blogType/add";
    58. }
    59. @RequestMapping("/toUpdate")
    60. public String toUpdate(Integer id,Model model) {
    61. model.addAttribute("blogType", blogTypeService.findById(id));
    62. return "blogType/update";
    63. }
    64. @RequestMapping("/add")
    65. public void add(BlogType blogType,Model model) {
    66. int result = blogTypeService.add(blogType);
    67. model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
    68. }
    69. @RequestMapping("/update")
    70. public void update(BlogType blogType,Model model) {
    71. int result = blogTypeService.update(blogType);
    72. model.addAttribute("msg", result > 0 ? "保存成功" : "保存失败");
    73. }
    74. @RequestMapping("/search")
    75. public void search(Integer id,HttpServletResponse response){
    76. JSONObject jsonObj = new JSONObject();
    77. Map map = new HashMap<>(1);
    78. map.put("typeId", id);
    79. jsonObj.put("count", blogService.getCount(map));
    80. ResponseUtils.writeJson(response, jsonObj.toString());
    81. }
    82. //只删除类别
    83. @RequestMapping("/delete")
    84. public String delete(Integer id) {
    85. blogTypeService.delete(id);
    86. return "redirect:/blogType/list.do";
    87. }
    88. //删除的同时将相关博客的类别置为默认分类
    89. @RequestMapping("/batch_delete")
    90. public String batchDelete(Integer id) throws IOException {
    91. blogTypeService.batchDelete(id);
    92. return "redirect:/blogType/list.do";
    93. }
    94. }

    CommentAdminController

    1. package com.june.web.controller.admin;
    2. import java.io.IOException;
    3. import java.util.Date;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7. import javax.annotation.Resource;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. import org.json.JSONObject;
    11. import org.springframework.stereotype.Controller;
    12. import org.springframework.ui.Model;
    13. import org.springframework.web.bind.annotation.RequestMapping;
    14. import org.springframework.web.bind.annotation.RequestParam;
    15. import com.june.model.Comment;
    16. import com.june.model.PageBean;
    17. import com.june.service.BlogService;
    18. import com.june.service.CommentService;
    19. import com.june.util.Constants;
    20. import com.june.util.PageUtils;
    21. import com.june.util.ResponseUtils;
    22. import com.june.util.StringUtils;
    23. @Controller
    24. @RequestMapping("/comment")
    25. public class CommentAdminController {
    26. @Resource
    27. private CommentService commentService;
    28. @Resource
    29. private BlogService blogService;
    30. @RequestMapping("/list")
    31. public String list(@RequestParam(defaultValue = "1") Integer page,
    32. @RequestParam(defaultValue = Constants.BACK_PAGE_SIZE + 1 + "") Integer pageSize,
    33. String firstDate, String secondDate, String userName,
    34. Boolean isPass, Model model, HttpServletRequest request) {
    35. Map params = new HashMap(6);
    36. params.put("firstDate", firstDate);
    37. params.put("secondDate", secondDate);
    38. params.put("userName", userName);
    39. params.put("isPass", isPass);
    40. int totalCount = commentService.getCount(params);
    41. PageBean pageBean = new PageBean(totalCount, page, pageSize);
    42. params.put("start", pageBean.getStart());
    43. params.put("size", pageSize);
    44. List commentList = commentService.getCommentList(params);
    45. commentList.stream().forEach(comment -> {
    46. String content = comment.getContent();
    47. if (content.length() > 60) {
    48. comment.setContent(content.substring(0,60) + "...");
    49. }
    50. });
    51. model.addAttribute("pagination", pageBean);
    52. StringBuilder param = new StringBuilder(); // 分页查询参数
    53. param.append(StringUtils.isEmpty(firstDate) ? "" : "&firstDate=" + firstDate);
    54. param.append(StringUtils.isEmpty(secondDate) ? "" : "&secondDate=" + secondDate);
    55. param.append(StringUtils.isEmpty(userName) ? "" : "&userName=" + userName);
    56. param.append(isPass == null ? "" : "&isPass=" + isPass);
    57. String pageCode = PageUtils.genPagination(request.getContextPath() + "/comment/list.do",
    58. pageBean, param.toString());
    59. model.addAttribute("pageCode", pageCode);
    60. model.addAttribute("entry", params);
    61. model.addAttribute("commentList", commentList);
    62. return "comment/list";
    63. }
    64. @RequestMapping("/toAdd")
    65. public String toAdd() {
    66. return "comment/add";
    67. }
    68. @RequestMapping("/detail")
    69. public String detail(Integer id,Model model){
    70. model.addAttribute("comment", commentService.findById(id));
    71. return "comment/detail";
    72. }
    73. //评论审核
    74. @RequestMapping("/audit")
    75. public void audit(Comment comment, HttpServletResponse response) {
    76. comment.setReplyDate(new Date());
    77. int result = commentService.audit(comment);
    78. JSONObject jsonObj = new JSONObject();
    79. jsonObj.put("success", result > 0);
    80. ResponseUtils.writeJson(response, jsonObj.toString());
    81. }
    82. @RequestMapping("/delete")
    83. public String delete(Integer id) throws IOException{
    84. commentService.delete(id);
    85. return "redirect:/comment/list.do";
    86. }
    87. @RequestMapping("/deletes")
    88. public String deletes(String ids) {
    89. String[] idArr = org.springframework.util.StringUtils.commaDelimitedListToStringArray(ids);
    90. int len = idArr.length;
    91. Integer[] commentIds = new Integer[len];
    92. for (int i = 0; i < len; i++) {
    93. commentIds[i] = Integer.parseInt(idArr[i]);
    94. }
    95. commentService.batchDelete(commentIds);
    96. return "redirect:/comment/list.do";
    97. }
    98. }

    如果也想学习本系统,下面领取。关注并回复:103ssm 

     

  • 相关阅读:
    【STM32嵌入式系统设计与开发】——15PassiveBeep(无源蜂鸣器应用_GPIO输出状态实现)
    Linux中防火墙firewalld
    微信测试号推送
    Delft3D水动力与泥沙运动模拟实践技术应用
    【OpenCV-Python】教程:4-2 Harris角点检测
    STM32 IAP应用开发--bootloader升级程序
    QT 事件与信号区别
    安全可靠的文件传输服务助力完成更高效的医疗保健工作(上)
    centos8 jenkins 搭建和使用
    Python中连接池的分析和应用
  • 原文地址:https://blog.csdn.net/m0_74967853/article/details/128193567