springmvc实现留言回复功能

本文介绍如何在项目中实现留言和回复功能。通过两种思路:1.使用单一留言表,2.设立独立的留言和回复表,详细阐述实现步骤。包括后台代码处理和前端页面展示的细节,旨在打造清晰易用的交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

很多网站都提供了留言,评论回复功能,而我做的一个分享圈项目同样要实现的功能。

思路:

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 net.stxy.one.controller;

import net.stxy.one.model.*;
import net.stxy.one.service.CommentService;
import net.stxy.one.service.ContentService;
import net.stxy.one.service.ReplyService;
import net.stxy.one.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.selectUser(content.getUserid());
        //获取在该内容下的所有评论
        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("增加评论:"+comment.toString());
        if(comment.get
评论 56
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灰太狼_cxh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值