基本简介:
对一个论坛网站来说,回复功能是一个很重要的功能,它可以实现用户与用户之间关于所发帖子内容的沟通与交流。
首先,我们先看一下回复功能的实现页面:
主要步骤:
(一)搭建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();
}
}
这样一个回复功能就完成了。