jspblog : reply list
This commit is contained in:
@@ -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<Reply> findAll(int boardId) {
|
||||
String sql = "select * from reply where boardId= order by desc";
|
||||
Connection conn = DB.getConnection();
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
List<Reply> 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Reply> 댓글목록보기(int boardId) {
|
||||
return replyDao.findAll(boardId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Reply> 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;
|
||||
|
||||
@@ -40,6 +40,17 @@
|
||||
<!-- 댓글 리스트 시작-->
|
||||
<ul id="reply__list" class="media-list">
|
||||
<!-- 댓글 아이템 -->
|
||||
<c:forEach var="reply" items="${replies}">
|
||||
<li id="reply-${reply.id}" class="media">
|
||||
<img src="/jspblog/images/profile.jpg" class="img-circle" style="width: 50px; height: 50px;" alt="profile-image"/>
|
||||
<div class="media-body">
|
||||
<strong class="text-primary">${reply.userId}</strong>
|
||||
<p>${reply.content}</p></div>
|
||||
<div class="m-2">
|
||||
<i onClick="deleteReply(${reply.id})" class="material-icons">delete</i>
|
||||
</div>
|
||||
</li>
|
||||
</c:forEach>
|
||||
</ul>
|
||||
<!-- 댓글 리스트 끝-->
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user