1. 답글 기능 구조 이해
- 답글 기능을 만들기 위한 구조를 이해하는데 시간이 많이 걸렸다. 답글에 깊이가 왜 필요한지, 그룹내 순서가 왜 필요한지 그리고 그 값을 증가시키는 이유가 무엇인지 그 이유를 풀어쓴 블로그들이 많았지만 글로만 접하는 것보다 직접 쿼리를 실행함으로써 좀 더 이해가 쉬웠다
1.1 컬럼 구성
- 글 번호 : 글의 고유 번호(기본키)
- 그룹 번호(refid): 답글이 생성된 원본 글의 번호(부모글에서 파생된 답글들을 묶어줌)
- 깊이(depthnum) : 답글의 답글
- 그룹 내 순서(ordernum) : 답글의 정렬 순서(최신글은 항상 가장 위로)
1.2 새 글 쓰기
- cid : 글번호
- refid : 그룹 번호 (글번호와 동일)
- depthnum : 깊이 (0)
- ordernum : 그룹 내 순서 (0)
1.3 부모 게시글에 답글 쓰기
- cid : 글번호
- refid : 그룹 번호 (원본글의 그룹번호와 동일)
- depthnum : 깊이 (+1)
- ordernum : 그룹 내 순서 (+1)
1.4 답글에 답글 쓰기
- cid : 글번호
- refid : 그룹 번호 (원본글의 그룹번호와 동일)
- depthnum : 깊이 (+1)
- ordernum : 그룹 내 순서 (+1)
1.5 부모 게시글에 추가로 답글 달기
- cid : 글번호
- refid : 그룹 번호 (원본글의 그룹번호와 동일)
- depthnum : 깊이 (+1)
- ordernum : 그룹 내 순서 (+1) + 기존 게시글 그룹 내 순서(+1) => 최신글을 항상위로 올라오게 하기 위해서
2 답글 기능 추가
2.1 답글 기능을 추가하기 위해 board 테이블에 컬럼 추가
- parentNo, depth, groupOrder 컬럼을 추가
- parentNo : 답글이 생성된 원본글의 번호
- depth : 깊이 (답글의 답글)
- groupOrder : 순서 (답글의 정렬 순서)
2.2 boardRead.jsp
- [답글]을 클릭하면 답글 페이지로 이동
- 답글 페이지로 원본글의 id, parentNo, depth, groupOrder 값을 함께 전송
2.3 boardReply.jsp
- 답글을 내용을 입력 후 submit하면 boardRead.jsp에서 받은 값과 함께 boardReplyInsert.bbs로 전송한다.
- BoardFrontController에서 boardReplyInsert.bbs을 받아 BoardReplyCmd로 담은 값들을 보내준다
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>답글</title>
</head>
<body>
<form action="../boardReplyInsert.bbs" method="post">
<input type="hidden" name="id" value="<%= request.getParameter("id")%>">
<input type="hidden" name="parentNo" value="<%= request.getParameter("parentNo")%>"/>
<input type="hidden" name="depth" value="<%= request.getParameter("depth")%>"/>
<input type="hidden" name="groupOrder" value="<%= request.getParameter("groupOrder")%>"/>
<table>
<tr>
<td colspan="2">글 추가하기</td>
</tr>
<tr>
<td><label for="subject">제목</label></td>
<td><input type="text" name="subject" id="subject"/></td>
</tr>
<tr>
<td><label for="author">작성자</label></td>
<td><input type="text" name="author" id="author"/></td>
</tr>
<tr>
<td><label for="content">내용</label></td>
<td><input type="text" name="content" id="content"/></td>
</tr>
<tr>
<td><label for="password">수정/삭제 비밀번호</label></td>
<td><input type="password" name="password" id="password"/></td>
</tr>
<tr>
<!-- 취소 버튼을 누르면 boardList.bbs로 가도록 처리해 보시오. -->
<td colspan="2">
<input type="submit" value="[글쓰기]"/>
<a href="../boardList.bbs">[취소]</a>
</td>
</tr>
</table>
</form>
</body>
</html>
2.4 boardList.jsp
- 글 리스트에 depth에 따라 제목에 RE : 글자를 붙여주도록 수정
2.5 BoardDao
2.6 BoardReplyCmd
package kr.ac.daegu.jspmvc.biz;
import kr.ac.daegu.jspmvc.model.BoardDAO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
public class BoardReplyCmd implements BoardCmd {
@Override
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// enduser로부터 입력받은 데이터
int newId;
String subject =request.getParameter("subject");
String author = request.getParameter("author");
String content = request.getParameter("content");
String password = request.getParameter("password");
int parentNo = Integer.parseInt(request.getParameter("parentNo"));
int depth = Integer.parseInt(request.getParameter("depth"));
int groupOrder = Integer.parseInt(request.getParameter("groupOrder"));
//enduser로부터 입력받은 데이터 잘 들어왔는지 확인 log
System.out.println("subject"+subject);
System.out.println("author"+author);
System.out.println("content"+content);
System.out.println("password" + password);
// db에 접근해서 데이터 가져오는 인스턴스
BoardDAO dao = new BoardDAO();
try{
// board 테이블에 들어갈 id값을 가져오기 : board.id중에서 가장 높은 id값 + 1
newId = dao.getBoardNewId();
dao.updateGroupOrder(parentNo,groupOrder);
// dao 기능 호출해서 enduser가 입력한 데이터를 insert
dao.insertReplyBoardContent(newId, subject, author, content, password,parentNo,depth,groupOrder);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return true;
}
}
실행화면
- 현재 글 작성 시 제목과 작성자가 바뀌어 나와 수정이 필요
'JSP' 카테고리의 다른 글
2021-09-09(게시판 - 로그인 기능) (0) | 2021.09.09 |
---|---|
2021-09-06(jspmvc 시스템 구성도) (0) | 2021.09.08 |
2021-09-01(게시판-댓글 기능) 문제점 수정 (0) | 2021.09.02 |
2021-08-30(게시판 - 댓글 기능) (0) | 2021.08.30 |
2021-08-26(게시판 프로젝트 구조) (0) | 2021.08.26 |