-새글 쓰기: 부모글
-답변 쓰기: 자식글
그룹번호 grpno : 부모글 그룹번호와 동일
들여쓰기 indent : 부모글 들여쓰기 +1
글순서 ansnum : 부모글 글순서+1한 후, 글순서 재조정
※답변이 추가되는 경우 글 순서가 밀리게 된다
번호 제목 그룹번호 들여쓰기 글순서
bbs_seq 제주도 1 0(default) 0(default)
답변추가 →ㅇㅇ 1 1 1
bbs_seq 서울시 2 0(default) 0(default)
→마포구 2 1 1 (답변 추가)
→→신촌 2 2 2 (답변 추가)
답변 →종로구 2 1(default) 3 <-1(default)
답변 →강남구 2 1(default) 4 <-2(default)
답변 →→역삼동 2 2(default) 5 <-3(default)
답변 →→→역삼~ 2 3(default) 6 <-4(default)
--특정값을 공유해야 답변글로 보여줄수 있다.
public int reply(BbsDTO dto) {
int cnt=0;
try {
con=dbopen.getConnection();
sql=new StringBuilder();
//1. 부모글 정보 가져오기 select
//부모글의 그룹번호 들여쓰기 글순서
int grpno=0;
int indent=0;
int ansnum=0;
sql.append(" select grpno,indent,ansnum ");
sql.append(" from tb_bbs ");
sql.append(" where bbsno=? ");
pstmt= con.prepareStatement(sql.toString());
pstmt.setInt(1, dto.getBbsno());
rs=pstmt.executeQuery();
if(rs.next()) {
// 그룹번호, 들여쓰기: 부모글 +1, 글순서: 부모글 +1
grpno=rs.getInt("grpno");
indent=rs.getInt("indent")+1;
ansnum=rs.getInt("ansnum")+1;
}
//2.글 순서 재조정하기 update
// 1단계에서 사용했던 sql값 지우기
// sql문을 새로 선언하고 작성해도 무관함 sql2
sql.delete(0,sql.length());
sql.append(" UPDATE tb_bbs ");
sql.append(" SET ansnum=ansnum+1 ");
sql.append(" WHERE (ansnum>=?) and grpno=? ");
pstmt= con.prepareStatement(sql.toString());
pstmt.setInt(1,ansnum);
pstmt.setInt(2,grpno);
pstmt.executeQuery();
//3. 답변글 추가하기
sql.delete(0,sql.length());
sql.append(" insert into tb_bbs(bbsno,wname,subject,content,passwd,ip,grpno,indent,ansnum) ");
sql.append(" values(bbs_seq.nextval, ?, ?, ?, ?, ?, ?, ?, ?) ");
pstmt=con.prepareStatement(sql.toString());
pstmt.setString(1, dto.getWname());
pstmt.setString(2, dto.getSubject());
pstmt.setString(3, dto.getContent());
pstmt.setString(4, dto.getPasswd());
pstmt.setString(5, dto.getIp());
pstmt.setInt(6, grpno);
pstmt.setInt(7,indent);;
pstmt.setInt(8, ansnum);;
cnt=pstmt.executeUpdate();
} catch (Exception e) {
System.out.println("답변 실패"+e);
}finally {
DBClose.close(con,pstmt,rs);;
}
return cnt;
}
공통코드 부분에 새롭게 추가한다
<%
//검색
String word=request.getParameter("word");
String col=request.getParameter("col");
//문자열 값이 null이면 빈문자열로 치환
word=Utility.checkNull(word);
col=Utility.checkNull(col);
기존에 작성한 bbsRead.jsp를 복사해서 수정한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../header.jsp" %>
<!-- 본문 시작 -->
<h3>답변쓰기</h3>
<p><a href="bbsList.jsp">글목록</a></p>
<!-- 이벤트는 myscript.js에 함수 작성
액션을 실행하기전에 폼을 한번 찍고 간다 -->
<form name="bbsfrm" id="bbsfrm" method="post" action="bbsReplyProc.jsp" onsubmit="return bbsCheck()">
<input type="hidden" name="bbsno" value="<%=request.getParameter("bbsno")%>">
<!--부모 글 번호 -->
<table class="table">
<tr>
<th class="success">작성자</th>
<td><input type="text" name="wname" id="wname" class="form-control" maxlength="20" required></td>
</tr>
<tr>
<th class="success">제목</th>
<td><input type="text" name="subject" id="subject" class="form-control" maxlength="100" required></td>
</tr>
<tr>
<th class="success">내용</th>
<td><textarea rows="5" class="form-control" name="content" id="content"></textarea></td>
</tr>
<tr>
<th class="success">비밀번호</th>
<td><input type="password" name="passwd" id="passwd" class="form-control" maxlength="10" required></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="답변쓰기" class="btn btn-success">
<input type="reset" value="취소" class="btn btn-danger">
</td>
</table>
</form>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp" %>
기존에 작성한 bbsIns.jsp를 복사해서 수정한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="ssi.jsp" %>
<%@ include file="../header.jsp" %>
<!-- 본문 시작 -->
<%
//사용자가 입력한 정보를 가져오기
int bbsno=Integer.parseInt(request.getParameter("bbsno")); //부모글 번호
String wname=request.getParameter("wname").trim();
String subject=request.getParameter("subject").trim();
String content=request.getParameter("content").trim();
String passwd=request.getParameter("passwd").trim();
String ip =request.getRemoteAddr();
//dto객체에 담기
dto.setBbsno(bbsno);
dto.setWname(wname);
dto.setSubject(subject);
dto.setContent(content);
dto.setPasswd(passwd);
dto.setIp(ip);
int cnt=dao.reply(dto);
if(cnt==0){
out.println("<p>답변 추가 실패</p>");
out.println("<p><a href='javascript:history.back()'>재시도</a></p>");
}else{
out.println("<script>");
out.println(" alert('답변 추가 성공');");
out.println(" location.href='bbsList.jsp';"); //목록페이지 이동
out.println("</script>");
}
%>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp" %>
public int count2(String col, String word) {
int cnt=0;
try {
con=dbopen.getConnection();
sql=new StringBuilder();
sql.append(" select count(*) as cnt ");
sql.append(" from tb_bbs ");
if(word.length()>=1) {
String search="";
if(col.equals("subject_content")) {
search+= " where subject like '%" +word+"%' ";
search+= " or content like '%" +word+"%' ";
}else if(col.equals("subject")) {
search+= " where subject like '%" +word+"%' ";
}else if(col.equals("content")) {
search+= " where content like '%" +word+"%' ";
}else if(col.equals("wname")) {
search+= " where wname like '%" +word+"%' ";
}
sql.append(search);
}
pstmt=con.prepareStatement(sql.toString());
rs=pstmt.executeQuery();
if(rs.next()){
cnt=rs.getInt("cnt");
}
}catch (Exception e) {
System.out.println("갯수 세기 실패"+e);
}finally {
DBClose.close(con,pstmt,rs);
}
return cnt;
}
public ArrayList<BbsDTO> list2(String col, String word) {
ArrayList<BbsDTO> list= null;
try {
con=dbopen.getConnection();
sql=new StringBuilder();
sql.append(" select bbsno, wname, subject, readcnt, regdt, indent ");
sql.append(" from tb_bbs ");
if(word.length()>=1) {
String search="";
if(col.equals("subject_content")) {
search+= " where subject like '%" +word+"%' ";
search+= " or content like '%" +word+"%' ";
}else if(col.equals("subject")) {
search+= " where subject like '%" +word+"%' ";
}else if(col.equals("content")) {
search+= " where content like '%" +word+"%' ";
}else if(col.equals("wname")) {
search+= " where wname like '%" +word+"%' ";
}
sql.append(search);
}
sql.append(" order by grpno Desc, ansnum asc ");
pstmt=con.prepareStatement(sql.toString());
rs=pstmt.executeQuery();
if(rs.next()){
list=new ArrayList<>();
do { //커서가 가리키는 한줄씩 sungjukDto에 저장
BbsDTO dto=new BbsDTO();
dto.setBbsno(rs.getInt("bbsno"));;
dto.setWname(rs.getString("wname"));
dto.setSubject(rs.getString("subject"));
dto.setReadcnt(rs.getInt("readcnt"));
dto.setRegdt(rs.getString("regdt"));
dto.setIndent(rs.getInt("indent"));
list.add(dto);//list에 저장
}while(rs.next());
}else{
list=null;
}
}catch (Exception e) {
System.out.println("목록 가져오기 실패"+e);
}finally {
DBClose.close(con,pstmt,rs);
}
return list;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="ssi.jsp" %>
<%@ include file="../header.jsp" %>
<!-- 본문 시작 -->
<h3>글목록</h3>
<p><a href="bbsForm.jsp">글쓰러 가기</a></p>
<table class="table table-hover" border="1">
<thead>
<tr class="info" >
<th>제목</th>
<th>조회수</th>
<th>작성자</th>
<th>등록일</th>
</tr>
</thead>
<tbody>
<%
ArrayList<BbsDTO> list = dao.list2(col,word);
if(list==null){
out.println(" <tr>");
out.println(" <td colspan='5'>" );
out.println(" <strong> 작성 글없음 <strong>" );
out.println(" </td>" );
out.println(" <tr>");
}else{
//오늘 날짜를 문자열로 2023-04-14
String today=Utility.getDate();
for(int i=0;i<list.size();i++){
dto=list.get(i);
%>
<tr>
<td style="text-align:left">
<%
//답변 이미지 출력
for(int n=1;n<=dto.getIndent();n++){
out.println("<img src='../images/reply.gif'>");
}
%>
<a href="bbsRead.jsp?bbsno=<%=dto.getBbsno()%>&col=<%=col%>&word=<%=word%>"><%=dto.getSubject()%></a>
<%
//오늘 작성한 글 제목 뒤에 new 이미지 출력
//작성일(regdt)에서 년월일 자르기
String regdt=dto.getRegdt().substring(0,10);
if(regdt.equals(today)){
out.println("<img src='../images/new.gif'>");
}
//조회수 10이상 hot 이미지 출력
if(dto.getReadcnt()>=10){
out.println("<img src='../images/hot.gif'>");
}
%>
</td>
<td><%=dto.getReadcnt() %></td>
<td><%=dto.getWname() %></td>
<td><%=dto.getRegdt().substring(0,10) %></td>
</tr>
<%
}
int totalRecord=dao.count2(col,word);
out.println("<tr>");
out.println("<td colspan='4' style='text-align:center;'>");
out.println(" 글갯수:<strong>" +totalRecord +"</strong>");
out.println("</td>");
out.println("</tr>");
%>
<tr>
<td colspan="4" style='text-align: center; height: 50px'>
<form action="bbsList.jsp" onsubmit="return searchCheck()">
<select name="col">
<option value="subject_content">제목+내용
<option value="subject">제목
<option value="content">내용
<option value="wname">작성자
</select>
<input type="text" name="word" id="word">
<input type="submit" value="검색">
</form>
</td>
</tr>
<%
}
%>
</tbody>
</table>
<!-- 본문 끝 -->
<%@ include file="../footer.jsp" %>