diff --git a/jspblog/src/main/java/com/example/jspblog/domain/reply/ReplyDao.java b/jspblog/src/main/java/com/example/jspblog/domain/reply/ReplyDao.java index 7729a11c..ea48f732 100644 --- a/jspblog/src/main/java/com/example/jspblog/domain/reply/ReplyDao.java +++ b/jspblog/src/main/java/com/example/jspblog/domain/reply/ReplyDao.java @@ -1,13 +1,14 @@ package com.example.jspblog.domain.reply; import com.example.jspblog.config.DB; -import com.example.jspblog.domain.board.dto.DetailResDto; import com.example.jspblog.domain.reply.dto.SaveReqDto; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; public class ReplyDao { public int save(SaveReqDto dto) { @@ -66,4 +67,34 @@ public class ReplyDao { } return null; } + + public List findAll(int boardId) { + String sql = "select * from reply where boardId= order by desc"; + Connection conn = DB.getConnection(); + PreparedStatement pstmt = null; + ResultSet rs = null; + List replies = new ArrayList<>(); + if (conn != null) { + try { + pstmt = conn.prepareStatement(sql); + pstmt.setInt(1, boardId); + rs = pstmt.executeQuery(); + while (rs.next()) { + Reply reply = Reply.builder() + .id(rs.getInt("id")) + .userId(rs.getInt("userId")) + .boardId(rs.getInt("boardId")) + .content(rs.getString("content")) + .build(); + replies.add(reply); + } + return replies; + } catch (Exception e) { + e.printStackTrace(); + } finally { + DB.close(conn, pstmt, rs); + } + } + return null; + } } diff --git a/jspblog/src/main/java/com/example/jspblog/service/ReplyService.java b/jspblog/src/main/java/com/example/jspblog/service/ReplyService.java index dc871d30..1f76ce46 100644 --- a/jspblog/src/main/java/com/example/jspblog/service/ReplyService.java +++ b/jspblog/src/main/java/com/example/jspblog/service/ReplyService.java @@ -4,6 +4,8 @@ import com.example.jspblog.domain.reply.Reply; import com.example.jspblog.domain.reply.ReplyDao; import com.example.jspblog.domain.reply.dto.SaveReqDto; +import java.util.List; + public class ReplyService { private final ReplyDao replyDao; @@ -19,4 +21,8 @@ public class ReplyService { public Reply 댓글찾기(int id) { return replyDao.findById(id); } + + public List 댓글목록보기(int boardId) { + return replyDao.findAll(boardId); + } } diff --git a/jspblog/src/main/java/com/example/jspblog/web/BoardController.java b/jspblog/src/main/java/com/example/jspblog/web/BoardController.java index c8e5f73f..b6d11d84 100644 --- a/jspblog/src/main/java/com/example/jspblog/web/BoardController.java +++ b/jspblog/src/main/java/com/example/jspblog/web/BoardController.java @@ -2,8 +2,10 @@ package com.example.jspblog.web; import com.example.jspblog.domain.board.Board; import com.example.jspblog.domain.board.dto.*; +import com.example.jspblog.domain.reply.Reply; import com.example.jspblog.domain.user.User; import com.example.jspblog.service.BoardService; +import com.example.jspblog.service.ReplyService; import com.example.jspblog.service.UserService; import com.example.jspblog.util.Script; import com.google.gson.Gson; @@ -32,6 +34,7 @@ public class BoardController extends HttpServlet { protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String cmd = request.getParameter("cmd"); BoardService boardService = new BoardService(); + ReplyService replyService = new ReplyService(); HttpSession session = request.getSession(); User principal = (User) session.getAttribute("principal"); switch (cmd) { @@ -76,10 +79,13 @@ public class BoardController extends HttpServlet { case "detail" : { int id = Integer.parseInt(request.getParameter("id")); DetailResDto dto = boardService.글상세보기(id); + List replies = replyService.댓글목록보기(id); + if (dto == null) { Script.back(response, "상세보기에 실패하였습니다."); } else { request.setAttribute("detail", dto); + request.setAttribute("replies", replies); request.getRequestDispatcher("board/detail.jsp").forward(request, response); } break; diff --git a/jspblog/src/main/webapp/board/detail.jsp b/jspblog/src/main/webapp/board/detail.jsp index 96bf802e..f8c7c91f 100644 --- a/jspblog/src/main/webapp/board/detail.jsp +++ b/jspblog/src/main/webapp/board/detail.jsp @@ -40,6 +40,17 @@
    + +
  • + profile-image +
    + ${reply.userId} +

    ${reply.content}

    +
    + delete +
    +
  • +