springmvc实现留⾔回复功能
很多⽹站都提供了留⾔,评论回复功能,⽽我做的⼀个分享圈项⽬同样要实现的功能。
思路:
1.可以只创建⼀个留⾔表,⽤户的留⾔和回复功能都插进这张表,这样建议在⽹页显⽰效果⽤爬楼形式,因为数据都在⼀张表,划分留⾔跟回复情况会有点复杂。
2.创建留⾔表和回复表,⽤户的留⾔和回复插⼊相应的表,这样在⽹页中显⽰可以进⾏清晰的迭代数据库的数据,进⾏分层显⽰。
效果:
实现步骤:
1.创建留⾔表和回复表:
-- ----------------------------
-- Table structure for `comment`
-- ----------------------------
DROP TABLE IF EXISTS `comment`;
CREATE TABLE `comment` (
`c_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '评论表id',
`c_userid` varchar(50) DEFAULT NULL COMMENT '评论者id',
`c_contentid` int(11) DEFAULT NULL COMMENT '内容id(在哪个内容下评论的)',
`c_createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '插⼊时间',
`c_content` varchar(50) DEFAULT NULL COMMENT '评论的内容',
`c_otherid` varchar(50) DEFAULT NULL COMMENT '给谁(该内容id的作者)留⾔',
`c_state` int(11) DEFAULT NULL COMMENT '0-未读,1-已读',
PRIMARY KEY (`c_id`),
KEY `userid` (`c_userid`),
KEY `contentid` (`c_contentid`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for `reply`
-- ----------------------------
DROP TABLE IF EXISTS `reply`;
CREATE TABLE `reply` (
`r_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '回复表id',
`r_userid` varchar(50) DEFAULT NULL COMMENT '回复的⽤户账号',
`r_creatime` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '插⼊时间',  `r_content` varchar(100) DEFAULT NULL COMMENT '回复的内容',
`r_otherid` varchar(50) DEFAULT NULL COMMENT '给谁回复',
`r_words` varchar(100) DEFAULT NULL COMMENT '在哪个留⾔下的回复',
`r_contentid` int(11) DEFAULT NULL COMMENT '那篇分享下的回复',
`r_state` int(11) DEFAULT NULL COMMENT '0-未读,1-已读',
PRIMARY KEY (`r_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=utf8;
2.后台代码:
评论:
package ntroller;
import del.*;
import service.CommentService;
import service.ContentService;
import service.ReplyService;
import service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* 评论操作的控制
* Created by ASUS on 2018/5/27
*
* @Authod Grey Wolf
*/
@Controller
@RequestMapping("/user")
public class CommentController {
@Autowired
private ContentService contentService;
@Autowired
private CommentService commentService;
@Autowired
private UserinfoService userinfoService;
@Autowired
private ReplyService replyService;
/**
* 跳到评论页⾯
* 跳到评论页⾯
* @param contentId
* @param model
* @return
*/
@RequestMapping("/comment")
public String comment(@RequestParam("contentId")int contentId, Model model){        System.out.println("id:"+contentId);
//获取该内容的所有信息
Content content=contentService.selectContent(contentId);
//获取该内容的⽤户信息
Userinfo userinfo=userinfoService.Userid());
//获取在该内容下的所有评论
List<Comment> commentList=commentService.selectById(contentId);
//获取在该内容下的所有回复
List<Reply>replyList=replyService.selectReply(contentId);
model.addAttribute("content",content);
model.addAttribute("user",userinfo);
model.addAttribute("commentList",commentList);
model.addAttribute("replyList",replyList);
return "showContent";
}
/
**
* 增加评论
* @param comment
* @return
*/
@RequestMapping("/addcomment")
public String addComment(Comment comment){
System.out.println("增加评论:"+String());
Content()!=null&& !Content().equals("")) {
int result = commentService.insertComment(comment);
}
return "redirect:/user/comment?contentId="+Contentid();
}
}
回复:
package ntroller;
import del.Reply;
import service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 回复处理控制器
* Created by ASUS on 2018/6/7
*
* @Authod Grey Wolf
*/
@Controller
@RequestMapping("/user")
public class ReplyController {
@Autowired
private ReplyService replyService;
/**
* 增加回复信息
* @param reply
* @return
*/
@RequestMapping("/addreply")
public String addReply(Reply reply){
System.out.println("回复信息:"+String());
if (Content()!=null&& !reply.equals("")) {
int res = replyService.addReply(reply);
}
return "redirect:/user/comment?contentId="+Contentid();    }
}
3.页⾯jsp代码:
<%@ page import="SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%--
Created by IntelliJ IDEA.
User: ASUS
Date: 2018/5/19
Time: 17:30
To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="java.sun/jsp/jstl/core" %>
<%
String path = ContextPath();
String basePath = Scheme() + "://" + ServerName() + ":" + ServerPort() + path;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
layui和bootstrap哪个好String nowDate = sdf.format(date);
%>
<html>
<head>
<title>Title</title>
<link rel="stylesheet" href="<%=basePath%>/public/layui/src/css/layui.css"/>
<link rel="stylesheet" href="<%=basePath%>/public/bootstrap/bootstrap.min.css"/>
<link rel="stylesheet" type="text/css" href="<%=ContextPath()%>/public/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="<%=ContextPath()%>/public/css/nprogress.css">
<link rel="stylesheet" type="text/css" href="<%=ContextPath()%>/public/css/style.css">
<link rel="stylesheet" type="text/css" href="<%=ContextPath()%>/public/css/font-awesome.min.css">
<link rel="apple-touch-icon-precomposed" href="<%=ContextPath()%>/public/images/icon.png">
<link rel="shortcut icon" href="<%=ContextPath()%>/public/images/favicon.ico">
<%--图⽚css--%>
<link rel="stylesheet" type="text/css" href="<%=ContextPath()%>/public/content/publishContent/css/picture.css">
</head>
<body>
<%--显⽰信息--%>
<div class="title">
<h3>内容信息:</h3>
<div class="more">
<li><a data-cont="消息页" title="消息页" href="<%=ContextPath()%>/user/message?userid=${sessionScope.user.userid}">我的消息</a></li>
<a href="<%=ContextPath()%>/user/selectPage" title="全部分享" >全部分享</a>
<a href="<%=ContextPath()%>/user/selectPage?otherid=${sessionScope.user.userid}" title="别⼈的分享" >别⼈的分享</a>
<a href="<%=ContextPath()%>/user/selectPage?userid=${sessionScope.user.userid}" title="我的分享" >我的分享</a>
<a href="<%=ContextPath()%>/user/publishContent" title="发布分享" >发布分享</a>
</div>
</div>
<article class="excerpt excerpt-${content.userid}" >
<a class="focus" href="<%=ContextPath()%>/user/selectPage?userid=content.userid" title="分享圈" target="_blank" >
<div class="imgs"><img class="img" src="<%=ContextPath()%>/public/images/head.jpg"/></div>
</a>
<header>
<h2><a href="#" title="⽤户" target="_blank" >${content.userid}</a>
</h2>
</header>
<p class="meta">
<time class="time"><i class="glyphicon glyphicon-time"></i> ${atetime}</time>
<span class="views"> <a class="comment" id="${content.id}" onclick="niceDetail(this.id)" title="点赞" target="_blank" ><i class="glyphicon glyphicon-commen    </p>
<p class="note">${t}</p>
<%--图⽚--%>
<div  class="spu_img"id="spu_img">
<div id="spu_img_div_0" >
<c:if test="${content.pic1 !=null }">
<img id = "img_1}"  src="localhost:8089/SharedImageServer/contentpic/${content.pic1}" width="80px" height="80px"/>
</c:if>
<c:if test="${content.pic2 !=null }">
<img id = "img_2"  src="localhost:8089/SharedImageServer/contentpic/${content.pic2}" width="80px" height="80px" />
</c:if>
<c:if test="${content.pic3 !=null }">
<img id = "img_3}" src="localhost:8089/SharedImageServer/contentpic/${content.pic3}" width="80px" height="80px" />
</c:if>
</div>
</div>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。