添加评论回复功能

This commit is contained in:
landaiqing 2023-06-09 15:48:57 +08:00
parent 345c2614cf
commit ddc99aeaad
4 changed files with 127 additions and 19 deletions

View File

@ -72,4 +72,34 @@ public class UserDao {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
/**
* 回复评论
* */
public int reply(UserEntity userEntity){
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = JdbcUtils.getConnection();
JdbcUtils.beginTransaction(connection);
preparedStatement = connection.prepareStatement("UPDATE `webguestbook`.`user` SET `replyContent` = ?, `replyDateTime` = ? WHERE `userId` = ?;");
// preparedStatement.setInt(1,userEntity.getUserId());
preparedStatement.setString(1,userEntity.getReplyContent());
preparedStatement.setDate(2,userEntity.getReplyDateTime());
preparedStatement.setInt(3,userEntity.getUserId());
// preparedStatement.setString(4,userEntity.getContent());
// preparedStatement.setDate(5, new Date(userEntity.getDateTime().getTime()));
Integer result = preparedStatement.executeUpdate();
JdbcUtils.commitTransaction(connection);
return result;
} catch (SQLException e) {
JdbcUtils.rollBackTransaction(connection);
throw new RuntimeException(e);
} finally {
JdbcUtils.closeConnection(null, preparedStatement, connection);
}
}
}

View File

@ -19,4 +19,11 @@ public class UserService {
public int insert(UserEntity userEntity){
return userDao.insert(userEntity);
}
/**
* 回复评论
* */
public int reply(UserEntity userEntity){
return userDao.reply(userEntity);
}
}

View File

@ -0,0 +1,54 @@
package com.landaiqing.servlet;
import com.landaiqing.entity.UserEntity;
import com.landaiqing.service.UserService;
import com.mysql.cj.util.StringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/reply")
public class ReplyServlet extends HttpServlet {
private UserService userService=new UserService();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
String userId = req.getParameter("userId");
if (StringUtils.isNullOrEmpty(userId)){
req.setAttribute("errorMsg","userId 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
String replyContent = req.getParameter("replyContent");
if (StringUtils.isNullOrEmpty(replyContent)){
req.setAttribute("errorMsg","content 的值不能为空!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
Date time = new java.sql.Date(new java.util.Date().getTime());
UserEntity userEntity=new UserEntity();
userEntity.setUserId(Integer.valueOf(userId));
userEntity.setReplyContent(replyContent);
userEntity.setReplyDateTime((java.sql.Date) time);
int result = userService.reply(userEntity);
if (result<=0){
req.setAttribute("errorMsg","插入失败!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
return;
}
resp.sendRedirect("index.jsp");
} catch (Exception e) {
req.setAttribute("errorMsg","系统异常!!!");
req.getRequestDispatcher("error.jsp").forward(req,resp);
throw new RuntimeException(e);
}
}
}

View File

@ -251,21 +251,21 @@
</div>
</div>
</div>
<div class="comments">
<div class="comment-wrap">
<div class="photo">
<div class="avatar" style="background-image: url('./static/img/头像修改.png')"></div>
</div>
<div class="comment-block">
<form action="">
<textarea name="" cols="30" rows="3" placeholder="Say somthing..."
style="resize:none"></textarea>
<button type="button" class="layui-btn layui-btn-xs">
<i class="layui-icon layui-icon-release"></i> 回复
</button>
</form>
</div>
</div>
<%-- <div class="comments">--%>
<%-- <div class="comment-wrap">--%>
<%-- <div class="photo">--%>
<%-- <div class="avatar" style="background-image: url('./static/img/头像修改.png')"></div>--%>
<%-- </div>--%>
<%-- <div class="comment-block">--%>
<%-- <form action="">--%>
<%-- <textarea name="content" id="replyContent" cols="30" rows="3" placeholder="Say somthing..."--%>
<%-- style="resize:none"></textarea>--%>
<%-- <button type="button" class="layui-btn layui-btn-xs" onclick="reply(${list.userId})">--%>
<%-- <i class="layui-icon layui-icon-release"></i> 回复--%>
<%-- </button>--%>
<%-- </form>--%>
<%-- </div>--%>
<%-- </div>--%>
</c:when>
<c:otherwise>
<div class="comments">
@ -275,9 +275,9 @@
</div>
<div class="comment-block">
<form action="">
<textarea name="" id="" cols="30" rows="3" placeholder="Say somthing..."
<textarea name="" id="replyContent" cols="30" rows="3" placeholder="Say somthing..."
style="resize:none"></textarea>
<button type="button" class="layui-btn layui-btn-xs">
<button type="button" class="layui-btn layui-btn-xs" onclick="reply(${list.userId})">
<i class="layui-icon layui-icon-release"></i> 回复
</button>
</form>
@ -329,7 +329,7 @@
})
})
// 发布评论
function publish() {
var name = document.getElementById('name').value;
var qq = document.getElementById('qq').value;
@ -353,7 +353,24 @@
console.log(result);
})
}
// 回复
function reply(userId){
var content = document.getElementById('replyContent').value;
axios({
// 请求方式
method: 'post',
// 请求的地址
url: 'http://localhost:8080${pageContext.request.contextPath}/reply',
// URL 中的查询参数
params: {
userId: userId,
replyContent:content
}
}).then(function (result) {
//定位 span id 名称 error 修改
console.log(result);
})
}
$(function () {
// 还能输入的字得个数