您的位置:首页 > 编程语言

仿百度贴吧回帖功能分析及代码示例

2017-06-24 12:32 381 查看
本人毕设,一个社区网站,做到论坛这个模块的时候,想做的智能化一些,刚好在用百度贴吧,就想着参考百度贴吧做一个智能论坛回复模块。参考很多网上的开源论坛源码,针对于回复功能,基本上没有动态提示以及分类的,全是死板的一条一条的堆积在界面里,没有分类,没有筛选。不知道谁在对谁评论,谁在对谁回复,这样就是一条条记录的增删改查,毫无功能性而言,用户体验性极差。刚开始的时候,我也是这样想,随便做一个增删改查就行了,不需要讲究太多,但是后来做着做着连我自己都感觉恶心,既然做了,为什么不能做的好一点,为什么不能智能化一点呢,所以,我开始抽出时间认真琢磨百度贴吧的嵌套回复功能。慢慢的发现,好像大多数主流论坛系统都是采用这种方式。这更加坚定了我要搞出这样回复模式的念头。



public String details()throws Exception{
topic=topicService.findTopicById(topicId);

if (StringUtil.isEmpty(page)) {
page="1";
}
PageBean pageBean=new PageBean(Integer.parseInt(page), 10);
replyList=replyService.findReplyListByTopicId(topicId, pageBean);
for(Reply reply : replyList){
long count=replyService.getSonCountByRid(reply.getId());
son.put(reply, count);
sonList=replyService.getsonListByRid(reply.getId());
sonReplyList.put(reply, sonList);

}
total=replyService.getReplyCountByTopicId(topicId);
StringBuffer param=new StringBuffer();
if (topicId>0) {
param.append("topicId="+topicId);
}
pageCode=PageUtil.genPagination(request.getContextPath()+"/Topic_details.action", total, Integer.parseInt(page), 10,param.toString());
return "details";
}empty


因为我的项目回复表只采用一张,没有子回复表,再者建两张回复表提交保存的时候,要准备两提交张表单,或者要在服务器端加各种判断,。所以,我的目标是在一张回复表上加上各种标识符来区分子回复与父回复。如上图所示,用户对发帖者回复的时候,为父回复,即第一回复,其他人可以对该回复进行深一层的回复与评论,这层分为子回复。我的思路是,对第一级回复做一个回复链接,点击回复时,通过js函数得到当前级回复的Id,然后保存子回复的时候,服务器端通过第一级的I回复d得到发表者对象,获取该对象的邮箱,通过邮箱服务器向该用户发出邮件,提示有人对你的评论做出了回复。同时对子回复记录状态设为0,即动态信息提示,发表父级回复的人登录之后会显示所有状态为0的信息总数,用户点击查看即可。这样就做到了双重智能提示功能。



数据库倒数第二列中,即是子回复与父回复的标识符,子回复记录了父回复的id,通过查询语句,只需要查询所有标识id为父级回复id的集合就行了,这里就涉及到了嵌套查询。

项目运行截图:



public String details()throws Exception{
topic=topicService.findTopicById(topicId);

if (StringUtil.isEmpty(page)) {
page="1";
}
PageBean pageBean=new PageBean(Integer.parseInt(page), 10);
replyList=replyService.findReplyListByTopicId(topicId, pageBean);
for(Reply reply : replyList){
long count=replyService.getSonCountByRid(reply.getId());
son.put(reply, count);
sonList=replyService.getsonListByRid(reply.getId());
sonReplyList.put(reply, sonList);

}
total=replyService.getReplyCountByTopicId(topicId);
StringBuffer param=new StringBuffer();
if (topicId>0) {
param.append("topicId="+topicId);
}
pageCode=PageUtil.genPagination(request.getContextPath()+"/Topic_details.action", total, Integer.parseInt(page), 10,param.toString());
return "details";
} 2..发邮件动态提醒功能:
public String save()throws Exception{
reply.setPublishTime(new Date());
reply.setStatus(0);
replyService.saveReply(reply);
Topic topic=topicService.findTopicById(reply.getTopic().getId());
User Tuser=userService.getUserById(topic.getUser().getId()); //2017.04.27 得到发帖人,向其发送邮件**1**
topic.setModifyTime(new Date());
topicService.saveTopic(topic);

// 2017.04.27回复功能拓展,通过邮箱的方式提醒用户。 **2**
User currentUser=userService.getUserById(reply.getUser().getId());
//2017.05.01 回复帖子实现用户积分+1
currentUser.setScore(currentUser.getScore()+1);
userService.saveUser(currentUser);

if(reply.getTitle().equals("")){

/*
* 3. 发邮件 **3**
* 把配置文件内容加载到prop中
*/
Properties prop = new Properties();
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream("email_template2.properties"));
} catch (IOException e1) {
throw new RuntimeException(e1);
}
/*
* 登录邮件服务器,得到session
*/
String host = prop.getProperty("host");//服务器主机名
String name = prop.getProperty("username");//登录名
String pass = prop.getProperty("password");//登录密码
Session session = MailUtils.createSession(host, name, pass);

/*
* 创建Mail对象
*/
String from = prop.getProperty("from");
String to = Tuser.getEmail();
String subject = prop.getProperty("subject");
// MessageForm.format方法会把第一个参数中的{0},使用第二个参数来替换。
// 例如MessageFormat.format("你好{0}, 你{1}!", "张三", "去死吧"); 返回“你好张三,你去死吧!”
String content = MessageFormat.format(prop.getProperty("content"), topic.getId());
Mail mail = new Mail(from, to, subject, content);
/*
* 发送邮件
*/
try {
MailUtils.send(session, mail);
System.out.println("发送成功");
} catch (MessagingException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}else{

Reply father=replyService.findReplyById(Integer.parseInt(reply.getTitle()));
3.jsp代码块:
<td style="width:21%;">
<fmt:formatDate value="${sonReply.publishTime }" pattern="yy-MM-dd HH:mm "/> 
<a href="#1" style="font-size: 9pt;text-align:right;color:gray;"onclick='reply("${sonReply.user.nickName }","","${reply.id }")'>回复</a>
</td>
</tr>
</table>
</dd>
</c:forEach>
<dd>
<c:choose>
<c:when test="${son.get(reply)-6>0 }">
.............................................................................
还有(${son.get(reply)-6 })条,<a href="Reply_details.action?rid=${reply.id }" style="font-size: 9pt;text-align:right;color:blue;" target="_blank">查看更多</a>
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>

综上,完整的嵌套回复以及动态提醒功能算是正式完成了,虽然还有很多不足,但毕竟是自己辛辛苦苦想出来的思路,条条大路通罗马。只要你敢去尝试,前方也许是别样的风景。另外附上项目网站,大家可以亲自去体验一下这个功能的实用性。附上网址  http://sharehoo.cn/Topic_details.action?topicId=38
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐