From ddc99aeaad637de3887d0d3a0efe0d12c705cd26 Mon Sep 17 00:00:00 2001 From: Qing Date: Fri, 9 Jun 2023 15:48:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=84=E8=AE=BA=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/landaiqing/dao/UserDao.java | 30 +++++++++++ src/com/landaiqing/service/UserService.java | 7 +++ src/com/landaiqing/servlet/ReplyServlet.java | 54 +++++++++++++++++++ web/index.jsp | 55 +++++++++++++------- 4 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 src/com/landaiqing/servlet/ReplyServlet.java diff --git a/src/com/landaiqing/dao/UserDao.java b/src/com/landaiqing/dao/UserDao.java index 1fb96ef..e978bf2 100644 --- a/src/com/landaiqing/dao/UserDao.java +++ b/src/com/landaiqing/dao/UserDao.java @@ -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); + } + } } diff --git a/src/com/landaiqing/service/UserService.java b/src/com/landaiqing/service/UserService.java index d015888..e158239 100644 --- a/src/com/landaiqing/service/UserService.java +++ b/src/com/landaiqing/service/UserService.java @@ -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); + } } diff --git a/src/com/landaiqing/servlet/ReplyServlet.java b/src/com/landaiqing/servlet/ReplyServlet.java new file mode 100644 index 0000000..c812f0b --- /dev/null +++ b/src/com/landaiqing/servlet/ReplyServlet.java @@ -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); + } + } +} diff --git a/web/index.jsp b/web/index.jsp index 285d7c1..c982e7c 100644 --- a/web/index.jsp +++ b/web/index.jsp @@ -251,21 +251,21 @@ -
-
-
-
-
-
-
- - -
-
-
+<%--
--%> +<%--
--%> +<%--
--%> +<%--
--%> +<%--
--%> +<%--
--%> +<%--
--%> +<%-- --%> +<%-- --%> +<%--
--%> +<%--
--%> +<%--
--%>
@@ -275,9 +275,9 @@
- -
@@ -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 () { // 还能输入的字得个数