detail.jsp

<!-- 댓글 리스트 시작-->
	<ul id="reply__list" class="media-list">

		<c:forEach var="reply" items="${replys}">
			<!-- 댓글 아이템 -->
			<li id="reply-${reply.id}" class="media">
				<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>
<!-- 댓글 리스트 끝-->

forEach 문으로 댓글들을 불러옵니다.

 

 

 

boardDetail.js

function replySave(userId, boardId) {
	var data = {
		userId : userId,
		boardId : boardId,
		content : $("#content").val()
	}
	$.ajax({
		type : "post",
		url : "/blog/reply?cmd=save",
		data : JSON.stringify(data),
		contentType : "application/json; charset=utf-8",
		dataType : "json"
	}).done(function(result) {
		if (result.statusCode == 1) {
			console.log(result);

			addReply(result.data);
			location.reload();
		} else {
			alert("댓글쓰기 실패");
		}
	});
}

location.reload(); 한 줄 추가

 

 

 

ReplyDao.java

public List<Reply> findAll(int boardId){
		String sql = "SELECT * FROM reply WHERE boardId = ? ORDER BY id DESC";
		Connection conn = DB.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs  = null;

		List<Reply> replys = new ArrayList<>();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, boardId);
			rs =  pstmt.executeQuery();

			// Persistence API
			while(rs.next()) { // 커서를 이동하는 함수
				Reply reply = new Reply();
				reply.setId(rs.getInt("id"));
				reply.setUserId(rs.getInt("userId"));
				reply.setBoardId(rs.getInt("boardId"));
				reply.setContent(rs.getString("content"));
				replys.add(reply);
			}
			return replys;
		} catch (Exception e) {
			e.printStackTrace();
		} finally { // 무조건 실행
			DB.close(conn, pstmt, rs);
		}
		return null;
	}

 

 

 

ReplyService.java

public List<Reply> 글목록보기(int boardId){
		return replyDao.findAll(boardId);
	}

 

 

 

BoardController.java

protected void doProcess(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cmd = request.getParameter("cmd");
		BoardrService boardService = new BoardrService();
        
		ReplyService replyService = new ReplyService();
        
		// http://localhost:8080/blog/board?cmd=saveForm
		HttpSession session = request.getSession();

doProcess 상단에 ReplyService 선언

 

else if(cmd.equals("detail")) {
			int id = Integer.parseInt(request.getParameter("id"));

			DetailRespDto dto = boardService.글상세보기(id); // board테이블+user테이블 = 조인된 데이터!!
			List<Reply> replys = replyService.글목록보기(id);

			if(dto == null) {
				Script.back(response, "상세보기에 실패하였습니다");
			}else {
				request.setAttribute("dto", dto);
				request.setAttribute("replys", replys);
				//System.out.println("DetailRespDto : "+dto);
				RequestDispatcher dis = request.getRequestDispatcher("board/detail.jsp");
				dis.forward(request, response);
			}
		}

detail (게시글 상세보기) 호출할 때 댓글 목록도 함께 가져오게하기

setAttribute replys 하여서 detail.jsp에서  jstl 사용할 수 있게하기

+ Recent posts