본문 바로가기
Java/Servlet & JSP

JSP) 게시글 리스트에서 댓글 개수 보여주기

by 박채니 2022. 7. 6.

안녕하세요, 코린이의 코딩 학습기 채니 입니다.

 

개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다.


게시글 리스트에서 댓글 개수 보여주기

 

BoardExt

commentCount 필드 추가

public class BoardExt extends Board {
	private int attachCount;
	private List<Attachment> attachments = new ArrayList<>();
	private int commentCount;
	
	public BoardExt() {
		super();
		// TODO Auto-generated constructor stub
	}

	public BoardExt(int no, String title, String writer, String content, int readCount, Timestamp regDate) {
		super(no, title, writer, content, readCount, regDate);
	}

	public int getAttachCount() {
		return attachCount;
	}

	public void setAttachCount(int attachCount) {
		this.attachCount = attachCount;
	}
	
	public List<Attachment> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<Attachment> attachments) {
		this.attachments = attachments;
	}

	public void addAttachment(Attachment attach) {
		this.attachments.add(attach);
	}
	
	public int getCommentCount() {
		return commentCount;
	}

	public void setCommentCount(int commentCount) {
		this.commentCount = commentCount;
	}

	@Override
	public String toString() {
		return "BoardExt [attachCount=" + attachCount + ", attachments=" + attachments + ", commentCount="
				+ commentCount + ", toString()=" + super.toString() + "]";
	}
}

 

Controller, Service

BoardListServlet 기존과 동일

https://chanychu.tistory.com/327?category=980487 

 

JSP) 첨부파일 있는 게시글 표시

안녕하세요, 코린이의 코딩 학습기 채니 입니다. 개인 포스팅용으로 내용에 오류 및 잘못된 정보가 있을 수 있습니다. 첨부파일 있는 게시글 표시 ※ DB 구조 확인하기 https://chanychu.tistory.com/326?ca

chanychu.tistory.com

 

Dao

BoardDao

// findAll = select * from (select row_number () over (order by no desc) rnum, b.*, (select count(*) from attachment where board_no = b.no) attach_count, (select count(*) from board_comment c where c.board_no = b.no) comment_count from board b) b where rnum between ? and ?
public List<Board> findAll(Connection conn, Map<String, Object> param) {
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    List<Board> boardList = new ArrayList<>();
    String sql = prop.getProperty("findAll");

    try {
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, (int)param.get("start"));
        pstmt.setInt(2, (int)param.get("end"));
        rset = pstmt.executeQuery();
        while(rset.next()) {
            BoardExt board = handleBoardResultSet(rset);
            board.setAttachCount(rset.getInt("attach_count"));
            board.setCommentCount(rset.getInt("comment_count"));
            boardList.add(board);
        }
    } catch (SQLException e) {
        throw new BoardException("게시글 전체 조회 오류!", e);
    } finally {
        close(rset);
        close(pstmt);
    }
    return boardList;
}

 

boardList.jsp

<%
	List<Board> boardList = (List<Board>)request.getAttribute("boardList");
%>
<link rel="stylesheet" href="<%=request.getContextPath()%>/css/board.css" />
<section id="board-container">
	<h2>게시판 </h2>
	<% if(loginMember != null) { %>
		<input type="button" value="글쓰기" id="btn-add" onclick="location.href='<%=request.getContextPath() %>/board/boardEnroll';" />
	<% } %>
	<table id="tbl-board">
		<tr>
			<th>번호</th>
			<th>제목</th>
			<th>작성자</th>
			<th>작성일</th>
			<th>첨부파일</th><%--첨부파일이 있는 경우 /images/file.png 표시 width:16px --%>
			<th>조회수</th>
		</tr>
		<% if(boardList != null && !boardList.isEmpty()) { 
			for(Board board : boardList) { 
				BoardExt boardExt = (BoardExt)board;	
		%>
		<tr>
			<td><%= board.getNo() %></td>
			<td>
				<a href="<%= request.getContextPath()%>/board/boardView?no=<%=board.getNo() %>"><%= HelloMvcUtils.escapeXml(board.getTitle()) %></a>
				<% if(boardExt.getCommentCount() > 0) { %>
					💬<%= boardExt.getCommentCount() %>
				<% } %> 
			</td>
			<td><%= board.getWriter() %></td>
			<td><%= new SimpleDateFormat("yyyy-MM-dd HH:mm").format(board.getRegDate()) %></td>
			<td>
				<% if(boardExt.getAttachCount() > 0) { %>
					<img src="<%=request.getContextPath() %>/images/file.png" alt="" width="16px"/>
				<% } %>
			</td>
			<td><%= board.getReadCount() %></td>
		</tr>
		<% 	}
		} else { %>
		<tr>
			<td colspan="6">조회된 게시글이 없습니다.</td>
		</tr>
		<% } %>
	</table>
	
	<div id='pagebar'>
		<%= request.getAttribute("pagebar") %>
	</div>
</section>
<%@ include file="/WEB-INF/views/common/footer.jsp" %>