基于Spring Boot的教学论坛系统中回复功能的实现

基本简介:

对一个论坛网站来说,回复功能是一个很重要的功能,它可以实现用户与用户之间关于所发帖子内容的沟通与交流。

首先,我们先看一下回复功能的实现页面:


主要步骤:

(一)搭建Comment数据库

一个回复需要包含主键id;所回复问题或者回复评论的parent_id;一个判断是回复问题还是回复别人评论的type,当type=1时,回复问题,当type=2时,回复别人的评论;回复人commentor;回复时间createtime;点赞数like_count;回复内容content;回复数:commentcount

使用mybatis- generator逆向工程自动生成实体类,get和set方法。

(二)Comment的数据传输结构
当用户输入回复,点击提交之后,我希望从前端传过来的数据包含parent_id(所回复对象的id),content(回复的内容)和type(判断回复的是问题还是评论),因为user的信息和创建时间等都可以直接在后端获取,不需要从前端传过来。于是建立commentcreateDTO,用来封装传递过来的数据:

public class CommentCreateDto {
    private int parent_id;
    private int type;
    private String content;
    //省略getter and setter 方法
}

 前端需要展示回复的信息,因此需要从后端传递回复数据给前端,传给前端的数据不仅要包含comment的所有属性,还要包含user的所有信息,因为我们需要展示出是回复人的姓名和头像,于是创建CommentDTO用于封装传给前端的数据:

public class CommentDto {
    private int id;
    private int parent_id;
    private int type;
    private int commentor;
    private int like_count;
    private long createtime;
    private int commentcount;
    private String content;
   private User user;
   //省略getter and setter 方法
}

 同时,如果comment成功插入到数据库了,因为希望前端能知道成功了,因此再创建一个ResultDto,里面包含是否成功的信息以及CommentDTO的数据: 

public class ResultDto<T> {
    private int code;
    private String message;
    private T data;
 
    public ResultDto success(){
        ResultDto resultDto=new ResultDto();
        resultDto.setCode(200);
        resultDto.setMessage("成功");
        return resultDto;
    }
    public <T> ResultDto success(T data){
        ResultDto resultDto=new ResultDto();
        resultDto.setCode(200);
        resultDto.setMessage("成功");
        resultDto.setData(data);
        return resultDto;
    }
 //省略getter and setter 方法
}

(三)完成回复问题功能
问题回复功能有两个模块,一是展示回复内容,二是回复框的处理。展示回复内容很简单,在questionController中增加两行代码:

List<CommentDto> comments=commentService.getByid(id);
model.addAttribute("comments",comments);


通过问题的id查找到回复信息,放进一个List中,并通过model传递给前端,前端只需要通过each的方式逐条获取数据展示就行。前端的代码请看源码的question.html

前端回复框包含一个textarea和一个button,给button设置onclick属性,通过jquery来处理点击提交后的回复的逻辑,写一个隐藏起来的input,这个input不会展示在前端页面上,但是可以用来传递问题的id,让后台知道是在哪个问题下评论 

<input type="hidden" id="question_id" th:value="${questionDto.id}">
<textarea class="form-control" rows="6" style="margin-top:10px; margin-bottom: 10px;"
          id="content"></textarea>
<button type="button" class="btn btn-success" style="float: right;margin: 10px;"
        onclick="post()">提交

新建community.js,把数据封装成一个json格式给到后端

function post() {
    //获取问题id
    var questionid = $("#question_id").val();
    //获取回复内容
    var content = $("#content").val();
    if(content==''){
        alert("回复内容不能为空")
    }else {
        $.ajax({
            type:"POST",
            url:"/comment",
            contentType:'application/json',
            data: JSON.stringify({
                "parent_id":questionid,
                "type":1,
                "content":content
            }),
            //获取后端返回过来的数据,也就是ResultDTO
            success: function (data) {
                if (data.code==200){
                    window.location.reload();
                } else{
                    alert("出现了错误");
                }
            },
            dataType:"json"
        });
    }
}


 新建CommentController,将数据写进数据库,具体操作和之前基本一致

@Controller
public class CommentController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private QuestionMapper questionMapper;
    @ResponseBody
    @RequestMapping(value = "/comment",method = RequestMethod.POST)
    public Object post(@RequestBody CommentCreateDto commentCreateDto,
                       HttpServletRequest request){
        //获取user
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return "login";
        }
        User user = null;
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("token")) {
                String token = cookie.getValue();
                user = userMapper.findBytoken(token);
                if (user != null) {
                    request.getSession().setAttribute("user", user);
                }
                break;
            }
        }
        //将前端传递过来的信息上传数据库
        Comment comment=new Comment();
        comment.setParent_id(commentCreateDto.getParent_id());
        comment.setContent(commentCreateDto.getContent());
        comment.setType(commentCreateDto.getType());
        comment.setCreatetime(System.currentTimeMillis());
        comment.setCommentor(user.getId());
        commentMapper.insert(comment);
        if (commentCreateDto.getType()==2){
            commentMapper.updatecommentcount(commentCreateDto.getParent_id());
        }else {
            questionMapper.updatecomment(commentCreateDto.getParent_id());
        }
        ResultDto resultDto=new ResultDto();
        //返回结果
        return resultDto.success();
    }
}


 这样一个回复功能就完成了。
 

 

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值