赞
踩
本系统是一个校园论坛管理系统,分为前后台,前台可以发帖留言,亮点是集成了mardown文档,可以有个很美观的帖子格式。后台管理员可以对用户、帖子、评论、分类等进行管理。
(文末查看完整源码)
登录
帖子查看
帖子详情
markdown发帖
留言
后台首页
用户管理
分类管理
帖子管理
回复评论管理
通知管理
本系统实现了以下功能:
1、登录
2、分类帖子查看
3、留言
4、发帖
5、用户管理
6、帖子管理
7、评论管理
8、留言管理
9、回复管理
10、分类管理
11、通知管理
等
数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:Spring+structs2+hibernate
项目结构
java端
实体类
Comment.java
package com.forum.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.Date; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor public class Comment { private Long id; private String username; private String email; private String content; //是否为用户评论 private boolean userComment; //头像 private String avatar; private Date createTime; private Long forumId; private Long parentCommentId; //父评论id,是為1 // private String parentNickname; //回复评论 private List<Comment> replyComments = new ArrayList<>(); //是否是用户评论 private Comment parentComment; private Forum forum; }
Forum.java
package com.forum.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.util.ArrayList; import java.util.Date; import java.util.List; @Data @AllArgsConstructor @NoArgsConstructor public class Forum { private Long id; private String title; private String content; private String avatars; // 原创转载 private String flag; private Integer views; private boolean commentabled; private Date createTime; private Date updateTime; private Long userId; private String description; private int like; // private Admin admin; private User user; private List<Tag> tags = new ArrayList<>(); private List<Comment> comments = new ArrayList<>(); private String tagIds; public void init(){ this.tagIds = tagsToIds(this.getTags()); } //将tags集合转换为tagIds字符串形式:“1,2,3”,用于编辑博客时显示博客的tag private String tagsToIds(List<Tag> tags){ if(!tags.isEmpty()){ StringBuffer ids = new StringBuffer(); boolean flag = false; for(Tag tag: tags){ if(flag){ ids.append(","); }else { flag = true; } ids.append(tag.getId()); } return ids.toString(); }else { return tagIds; } } }
CommentServiceImpl.java
package com.forum.service.impl; import com.forum.dao.ForumDao; import com.forum.dao.CommentDao; import com.forum.entity.Comment; import com.forum.service.CommentService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; @Service public class CommentServiceImpl implements CommentService { @Autowired private CommentDao commentDao; @Autowired private ForumDao forumDao; @Override public List<Comment> getCommentByForumId(Long forumId) { //查询父评论 //没有父节点的默认为-1 List<Comment> comments = commentDao.findByForumIdAndParentCommentNull(forumId, Long.parseLong("-1")); return comments; } @Override //接收回复的表单 public int saveComment(Comment comment) { //获得父id Long parentCommentId = comment.getParentComment().getId(); System.out.println("是否是父评论"+parentCommentId); comment.setAvatar("https://wallpaperm.cmcm.com/aab1692cf70ed27d9ebcf0ca3e88d61d.jpg"); //没有父级评论默认是-1 if (parentCommentId != -1) { //有父级评论 // comment.setParentComment(commentDao.findByParentCommentId(comment.getParentCommentId())); comment.setParentCommentId(parentCommentId); } else { //没有父级评论 comment.setParentCommentId((long) 0); // comment.setParentComment(null); } comment.setCreateTime(new Date()); return commentDao.saveComment(comment); } @Override public List<Comment> getAllComment() { return commentDao.getAllComment(); } @Override public void delById(Integer id) { commentDao.delById(id); } /** * 循环每个父评论“1” */ // private List<Comment> oneComment(List<Comment> comments){ // List<Comment> commentsView = new ArrayList<>(); // for (Comment comment : comments) { // Comment comment1 = new Comment(); // BeanUtils.copyProperties(comment,comment1); // commentsView.add(comment1); // // } // combineChildren(commentsView); // return commentsView; // } // // /** // * 存储帖子为1的对象集合 // * @param comments // */ // private void combineChildren(List<Comment> comments) { // for (Comment comment : comments) { // List<Comment> replys1 = comment.getReplyComments(); // for (Comment reply1 : replys1) { // recursively(reply1); // } // comment.setReplyComments(tempReplys); // tempReplys = new ArrayList<>(); // } // } //private List<Comment> tempReplys =new ArrayList<>(); // // /** // * 迭代的对象 // * @param // */ // private void recursively(Comment comment) { // tempReplys.add(comment); // if (comment.getReplyComments().size()>0){ // List<Comment> replys = comment.getReplyComments(); // for (Comment reply : replys) { // tempReplys.add(reply); // if (reply.getReplyComments().size()>0){ // recursively(reply); // } // // } // } // } }
ForumServiceImpl.java
package com.forum.service.impl; import com.forum.dao.ForumDao; import com.forum.entity.*; import com.forum.exception.NotFoundException; import com.forum.service.ForumService; import com.forum.service.TagService; import com.forum.utils.MarkdownUtils; //import com.forum.utils.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service public class ForumServiceImpl implements ForumService { @Autowired ForumDao forumDao; @Autowired TagService tagService; // @Autowired // RedisUtil redisUtil; private Boolean hasKey = false; @Override public void updateView(Long id) { forumDao.updateView(id); } @Override public Forum getForum(Long id) { return forumDao.getForum(id); } @Override public Forum getDetailedForum(Long id) { Forum forum = forumDao.getDetailedForum(id); if (forum == null) { throw new NotFoundException("该帖子不存在"); } String content = forum.getContent(); forum.setContent(MarkdownUtils.markdownToHtmlExtensions(content)); //将Markdown格式转换成html return forum; } @Override public List<Forum> getAllForum() { return forumDao.getAllForum(); } @Override public List<Forum> getByTypeId(Long typeId) { return forumDao.getByTypeId(typeId); } @Override public List<Forum> getByTagId(Long tagId) { return forumDao.getByTagId(tagId); } @Override public List<Forum> getIndexForum() { List<Forum> forums = forumDao.getIndexForum(); // int startPage=(pageNum-1)*5; // List<Forum> forums= forumDao.getForumTadIds(); // System.out.println(forums.size()); // List<Tag> tags; // for (Forum forum : forums) { // tags=new ArrayList<>(); // for (TagIds id : forum.getTagsId()) { // tags.add(tagService.getTagById(id.getTag_id())); // } // forum.setTags(tags); // // } return forums; } @Override public List<Forum> getAllRecommendForum() { return forumDao.getAllRecommendForum(); } @Override public List<Forum> getSearchForum(String query) { return forumDao.getSearchForum(query); } @Override public Map<String, List<Forum>> archiveForum() { List<String> years; Map<String, List<Forum>> map = new LinkedHashMap<>(); years = forumDao.findGroupYear(); Set<String> set = new HashSet<>(years); //set去掉重复的年份 // 转回list进行排序 years = new ArrayList<>(set); Collections.sort(years); Collections.reverse(years); // 遍历 for (String year : years) { System.out.println(year); map.put(year, findForumAndTagsByYear(year)); } return map; } /* 根据查找文章的所有 */ public List<Forum> findForumAndTagsByYear(String year) { List<Forum> forumAndTagIds = forumDao.findForumAndTagsByYear(year); // 将标签的id集合遍历查找出标签 List<Tag> tags; for (Forum forumAndTagId : forumAndTagIds) { tags = new ArrayList<>(); // System.out.println(tags.size()); forumAndTagId.setTags(tags); } return forumAndTagIds; } @Override public int countForum() { return forumDao.getAllForum().size(); } @Override public List<Forum> searchAllForum(Forum forum) { return forumDao.searchAllForum(forum); } /** * 排行榜信息 * * @return */ @Override public List<RankForum> findForumByLank() { List<RankForum> rankForums = new ArrayList<>(); rankForums = forumDao.findForumByRank(); return rankForums; // // }else { // System.out.println("走REDIS---"); // return (List<RankForum>) redisUtil.get("rank"); // // } } @Override public List<RankForum> getNewForum() { return forumDao.getNewForum(); } // @Override // public Long getLike(Long id) { // return forumDao.getLike(id); // } // @Override // public void addLike(Long forumId, Long userId) { forumDao.addLike(forumId); forumDao.addForum_Like(forumId, userId); // System.out.println("执行插入Like_User"); // // } // @Override // public int getLikeByUser(Long userId, Long forumId) { // return forumDao.getLikeByUser(userId, forumId); // } // @Override // public void cancelLike(Long forumId, Long userId) { // forumDao.cancelLike(forumId); // forumDao.removeForum_Like(forumId, userId); // } @Override public int countView() { return forumDao.countView(); } @Override public void addLike(Long id) { forumDao.addLike(id); } @Override //新增帖子 public int saveForum(Forum forum) { forum.setCreateTime(new Date()); forum.setViews(0); // forum.setFlag("原创"); //保存帖子 forumDao.saveForum(forum); //保存帖子后才能获取自增的id Long id = forum.getId(); //将标签的数据存到t_forums_tag表中 List<Tag> tags = forum.getTags(); ForumAndTag forumAndTag = null; for (Tag tag : tags) { //新增时无法获取自增的id,在mybatis里修改 forumAndTag = new ForumAndTag(tag.getId(), id); forumDao.saveForumAndTag(forumAndTag); } return 1; } @Override //编辑帖子 public int updateForum(Forum forum) { forum.setUpdateTime(new Date()); //将标签的数据存到t_forums_tag表中 List<Tag> tags = forum.getTags(); ForumAndTag forumAndTag = null; for (Tag tag : tags) { forumAndTag = new ForumAndTag(tag.getId(), forum.getId()); forumDao.saveForumAndTag(forumAndTag); } return forumDao.updateForum(forum); } @Override public int deleteForum(Long id) { return forumDao.deleteForum(id); } }
controller层
CommentController.java
package com.forum.controller; import com.forum.entity.Comment; import com.forum.entity.User; import com.forum.service.ForumService; import com.forum.service.CommentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import javax.servlet.http.HttpSession; @Controller public class CommentController { @Autowired private CommentService commentService; @Autowired private ForumService forumService; // @Value("${comment.avatar}") private String avatar; @GetMapping("/comments/{forumId}") //展示 public String getComments(@PathVariable Long forumId, Model model){ model.addAttribute("comments", commentService.getCommentByForumId(forumId)); model.addAttribute("forum", forumService.getDetailedForum(forumId)); return "forum :: commentList"; } @PostMapping("/comments") //提交 public String postComment(Comment comment, HttpSession session){ Long forumId = comment.getForum().getId(); comment.setForum(forumService.getDetailedForum(forumId)); //绑定评论 comment.setForumId(forumId); User user = (User) session.getAttribute("user"); if (user != null){ //用户存在 comment.setAvatar(user.getAvatar()); comment.setUserComment(true); }else { comment.setAvatar(avatar); } System.out.println(comment); String contents = comment.getContent(); String arr[]={"垃圾","滚","混蛋","王八蛋","有病","美白","祛痘","祛斑","纯天然","换肤","去除皱纹","防脱发","生发","无毒","安全","瘦脸","助眠",}; for (int i = 0; i < arr.length; i++) { contents= contents.replaceAll(arr[i],"*"); System.out.println(contents); comment.setContent(contents); } commentService.saveComment(comment); return "redirect:/comments/" + forumId; } }
ForumController.java
package com.forum.controller.admin; import com.forum.entity.Forum; import com.forum.entity.User; import com.forum.service.ForumService; import com.forum.service.TagService; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import javax.servlet.http.HttpSession; import java.util.List; @Controller @RequestMapping("/admin") public class ForumController { @Autowired private ForumService forumService; @Autowired private TagService tagService; public void setTypeAndTag(Model model) { model.addAttribute("tags", tagService.getAllTag()); } @GetMapping("/forums") //后台显示帖子列表 public String forums(@RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){ PageHelper.startPage(pagenum, 5); List<Forum> allForum = forumService.getAllForum(); //得到分页结果对象 PageInfo pageInfo = new PageInfo(allForum); model.addAttribute("pageInfo", pageInfo); setTypeAndTag(model); //查询类型和标签 return "admin/forums"; } @GetMapping("/forums/input") //去新增帖子页面 public String toAddForum(Model model){ model.addAttribute("forum", new Forum()); //返回一个forum对象给前端th:object setTypeAndTag(model); return "admin/forums-input"; } @PostMapping("/forums") //新增 public String addForum(Forum forum, HttpSession session, RedirectAttributes attributes){ //设置user属性 forum.setUser((User) session.getAttribute("admin")); //设置用户id forum.setUserId(forum.getUser().getId()); // //设置forum的type //给forum中的List<Tag>赋值 forum.setTags(tagService.getTagByString(forum.getTagIds())); forumService.saveForum(forum); attributes.addFlashAttribute("msg", "新增成功"); return "redirect:/admin/forums"; } @GetMapping("/forums/{id}/edit") //去编辑帖子页面 public String toEditForum(@PathVariable Long id, Model model){ Forum forum = forumService.getForum(id); forum.init(); //将tags集合转换为tagIds字符串 model.addAttribute("forum", forum); //返回一个forum对象给前端th:object setTypeAndTag(model); return "admin/forums-edit"; } @PostMapping("/forums/edit") //编辑 public String editForum(Forum forum, HttpSession session, RedirectAttributes attributes){ //设置user属性 forum.setUser((User) session.getAttribute("admin")); //设置用户id forum.setUserId(forum.getUser().getId()); // //设置forum的type //给forum中的List<Tag>赋值 forum.setTags(tagService.getTagByString(forum.getTagIds())); System.out.println(forum); forumService.updateForum(forum); attributes.addFlashAttribute("msg", "编辑成功"); return "redirect:/admin/forums"; } @GetMapping("/forums/{id}/delete") public String deleteForums(@PathVariable Long id, RedirectAttributes attributes){ forumService.deleteForum(id); attributes.addFlashAttribute("msg", "删除成功"); return "redirect:/admin/forums"; } @PostMapping("/forums/search") //按条件查询帖子 public String searchForums(Forum forum, @RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){ PageHelper.startPage(pagenum, 5); List<Forum> allForum = forumService.searchAllForum(forum); //得到分页结果对象 PageInfo pageInfo = new PageInfo(allForum); model.addAttribute("pageInfo", pageInfo); model.addAttribute("message", "查询成功"); setTypeAndTag(model); return "forums"; } }
觉得有用,记得一键三连哦!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。