210526_1(답글게시판)
일반게시판 ---> 답글게시판으로 변형한다.
개념 : 언제 답글을 달까요
글목록 글보기
답글 이벤트 발생
글번호 idx를 글쓰기로 정송한다.
---------------- -------------------
글쓰기 처리 글쓰기 폼
encoding 원글 번호를 받는다
받는다
번호 확인 원글 번호 있다 : 답글
글입력 메서드 호출
---------------dao 원글의 내용을 파싱할까? 말까?
0 이면 새글 DAO 생성 boardView(idx)
1 이면 답글 - 제목 정도..
원글 번호 없다,널이다 : 새글
답글과 관련된 컬럼
1) 기준블럭 2번 보다 큰 블럭에 +1씩을 한다. 3-->4, 4-->5
4321 ------> 54 21 이된다.
2) 기준블럭에 +1 해서 새 블럭에 부여한다. [3 새블럭] 이 된다.
5421 ------> 54321 로 된다.
결론.
2번블럭 기존원글이고, 새블럭이 답글이된다.
현재 부여된 번호는 step 번호이다.
step 번호는 그룹 ref 내부 순서를 정하는 번호다.
ref 그룹번호 : 하나의 원글로부터 파생되는 원글과답글들의 집합
depth 답글깊이 : 몇단계 답글이냐, 글목록에서 표시한다.
-----------------------추가적으로 필요한 컬럼과 순서를 나타내는 컬럼
-------------------------테이블에 데이터가 들어가는 모습..
idx 글번호 ref 그룹번호 step 그룹내부순서 depth 답글깊이
새1 1 1 0 0
새1 2 2 0 0
새3 3 3 0 0
새3 4 4 0 0
------------------------------------------------------------------
댓2 5 2 1->2->3->4 1
댓2 6 2 1->2 1
댓2 7 2 1 1
댓6 8 2 3 2
댓4 9 4 1 1
------------------------------------------------------------------출력되는 순서 : 글목록에서
order by ref desc, step asc 그룹번호가 큰것부터, 동순위가 생기면 스텝번호는 작은것부터
----------------------------------------------------------------------출력되는 목록 순서
idx 글번호 ref 그룹번호 step 그룹내부순서 depth 답글깊이
4 4 0 0
9 4 1 1
3 3 0 0
2 2 0 0
7 2 1 1
6 2 2 1
8 2 3 2
5 2 4 1
1 1 0 0
content.jsp 49줄만추가
239 그룹번호를 자동으로 만드는 로직
259 where 동일한 그룹에서 기준보다 큰 스텝값은 set step에 +1을 하라
list.jsp 296~303 수정
boardQuery 326~328
테이블 컬럼 추가
Create Table boardRe(
idx int primary key,
name varchar(10),
email varchar(50),
homepage varchar(50),
title varchar(50),
content varchar(2000),
pwd varchar(10),
wdate date,
step int default null,
ref int default null,
depth int default null,
hit int
);
DTO 추가 //setter getter
package board;
public class BoardBean {
private int idx;
private String name;
private String email;
private String homepage;
private String title;
private String content;
private String pwd;
private String wdate;
private int step;
private int ref;
private int depth;
private int hit;
public int getStep() {
return step;
}
public void setStep(int step) {
this.step = step;
}
public int getRef() {
return ref;
}
public void setRef(int ref) {
this.ref = ref;
}
public int getDepth() {
return depth;
}
public void setDepth(int depth) {
this.depth = depth;
}
public int getIdx() {
return idx;
}
public void setIdx(int idx) {
this.idx = idx;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHomepage() {
return homepage;
}
public void setHomepage(String homepage) {
this.homepage = homepage;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getWdate() {
return wdate;
}
public void setWdate(String wdate) {
this.wdate = wdate;
}
public int getHit() {
return hit;
}
public void setHit(int hit) {
this.hit = hit;
}
}
content.jsp에 버튼 추가
<tr>
<td colspan="2" align="center" bgcolor="#CCCCCC">
<input type="button" value="수정하기" onclick="editsend(<%=boardBean.getIdx()%>)">
<input type="button" value="삭제하기" onclick="delsend(<%=boardBean.getIdx()%>)">
<input type="button" value="답글달기" onclick="location.href('write.jsp?idx=<%=boardBean.getIdx()%>')">
</td>
</tr>
보드쿼리에 추가
public BoardBean boardView(int idx) throws SQLException{
BoardBean boardBean=null;
Connection conn= null;
Statement stat=null;
ResultSet rs=null;
String sql=null;
try {
conn=pool.getConnection();
stat=conn.createStatement();
sql="select * from "+board+" where idx= "+idx;
rs= stat.executeQuery(sql);
boardBean = new BoardBean();
if(rs.next()) {
boardBean.setIdx(rs.getInt("idx"));
boardBean.setName(rs.getString("name"));
boardBean.setEmail(rs.getString("email"));
boardBean.setHomepage(rs.getString("homepage"));
String title = rs.getString("title");
title =title.replaceAll("<","<");
title =title.replaceAll(">",">");
boardBean.setTitle(title);
String content = rs.getString("content");
content = content.replace("\n", "<br>");
boardBean.setContent(content);
//step, ref, depth를 꺼낸다
boardBean.setWdate(rs.getString("wdate"));
boardBean.setStep(rs.getInt("step"));
boardBean.setRef(rs.getInt("ref"));
boardBean.setDepth(rs.getInt("depth"));
boardBean.setHit(rs.getInt("hit"));
//boardList.add(boardBean);
}
}catch(Exception e) {
//TODO: handle exception
}finally {
rs.close();
stat.close();
pool.releaseConnection(conn);
}
return boardBean;
}//boardView()
write.jsp에
폼추가 idx,step,ref,depth 히든으로 전송
<%
int idx;
if (request.getParameter("idx") != null) {
idx = Integer.parseInt(request.getParameter("idx"));
} else {
idx = 0;
}
BoardQuery boardQuery = new BoardQuery();
boardBean = boardQuery.boardView(idx); //원글 가져오기 리턴값을 받을때는 생성하는것이 아니라, 변수만 받아와도된다.
%>
////////////////////////////////////////////////////////////////////////////////
<form method=post action="write_ok.jsp" name="wform">
<input type="hidden" name="idx" value=<%=boardBean.getIdx()%>>
<input type="hidden" name="step" value=<%=boardBean.getStep()%>>
<input type="hidden" name="ref" value=<%=boardBean.getRef()%>>
<input type="hidden" name="depth" value=<%=boardBean.getDepth()%>>
보드쿼리 입력값 수정
package board;
import java.sql.*;
import java.util.*;
import mydb.poolfact.*;
import com.sun.corba.se.spi.orbutil.fsm.State;
import com.sun.istack.internal.Pool;
import mydb.poolfact.*;
public class BoardQuery {
String board ="boardRe";//테이블명
String idxNum = "boardRe_idx_seq.nextval";//자동증가 이름
ConnectionPool pool = null;
//객체가 생성될때 연결합
public BoardQuery() { //생성자
try {
pool = ConnectionPool.getInstance();
}catch(Exception e) {
//TODO : handle exception
System.out.println("연결 실패");
}
}//BoardQuery()
보드인서트 메소드 수정 idx,step,ref,depth 추가
public void boardInsert(BoardBean boardBean) throws SQLException{
///////////////////////////pooling
Connection conn = pool.getConnection();
Statement stat = conn.createStatement();
ResultSet rs = null;
String sql ="";
String name=boardBean.getName();
String email=boardBean.getEmail();
String homepage=boardBean.getHomepage();
String title=boardBean.getTitle();
String content=boardBean.getContent();
String pwq=boardBean.getPwd();
int idx = boardBean.getIdx();
int step = boardBean.getStep();
int n_step = 0;//Integer.parseInt(step)
int ref = boardBean.getRef();
int n_ref = 0;//Integer.parseInt(ref)
int depth = boardBean.getDepth();
int n_depth = 0;//Integer.parseInt(depth)
try {
sql = " select max(idx) from "+board+" ";
stat = conn.createStatement();
rs=stat.executeQuery(sql);
int max_ref=0;
if(rs.next()) {
max_ref =rs.getInt(1)+1;
}else{
max_ref=1; // 그룹번호는 1부터 시작
}
if(idx==0) { //////////////새글인경우
n_step=0;
n_ref= max_ref;
n_depth=0;
}else{ ////////////////응답글인경우
sql=" update "+board+" set step=step+1 where ref="+ref+" and step > "+step;
stat=conn.createStatement();
stat.executeUpdate(sql);
n_step= step+1;
n_ref=ref;
n_depth= depth+1;
}
sql=" insert into " +board+" values( "+idxNum+",'"+name+"','"
+email+"','"+homepage+"','"+title+"','"+content+"','"
+pwq+"',sysdate,"+n_step+","+n_ref+","+n_depth+",0)";
sql=new String(sql.getBytes("8859_1"),"euc-kr");
stat.executeUpdate(sql);
//}
}catch(Exception e) {
System.out.println(e);
}finally {
rs.close();
stat.close();
pool.releaseConnection(conn);
}
} //boardInsert()
list.jsp 수정
<%
Vector listVector = boardQuery.getBoardList(offset,limit); //시작번지부터 limit10개
//JSP 빈에서 가져와서 뿌리기
for (int k = 0; k < listVector.size(); k++) {
BoardBean boardBean = (BoardBean) listVector.elementAt(k);
%>
<tr>
<td> <%=boardBean.getIdx()%></td>
<td> <%=boardBean.getWdate()%></td>
<td> <%
if(boardBean.getDepth() >0 ) {
for(int i=0; i<boardBean.getDepth(); i++){
out.print(" ");
}
out.print("<font size=2 color='#BC8F8F'>re:☞</font>");
}
%><a href="content.jsp?idx=<%=boardBean.getIdx()%>"><%=boardBean.getTitle()%></a></td>
<td> <%=boardBean.getName()%></td>
<td> <%=boardBean.getHit()%></td>
</tr>
보드쿼리 보드리스트메소드 수정
public Vector getBoardList(int offset, int limit) throws SQLException{
Connection conn = null;
Statement stat = null;
ResultSet rs= null;
String sql= null;
Vector boardList = new Vector();
/* order by ref desc, step asc*/
try {
conn=pool.getConnection();
stat=conn.createStatement();
sql=" select a.* "+
" from ( " +
" select ROWNUM as RNUM, b.* "+ //행번호를 붙인다. 그것을rnum이라고한고, b의모든컬럼=내가꺼낸결과에 줄번호를붙였다.
" from ( " +
" select * "+ //서브쿼리
" from "+board+" "+
" order by ref desc, step asc "+
" )b "+ // 글번호의 역순으로 꺼낸다. 이서브쿼리가 b이다. b가 가상의테이블이다. 인라인서브쿼리
" )a "+ // 위를 a
" where a.RNUM > " +offset+" "+ //RNUM이 0보다 크고 최대치보다 작거나 같을때
" and a.RNUM <= " +(offset + limit)+" "; // 1~10번까지 가져온다
rs=stat.executeQuery(sql);
while(rs.next()) {
BoardBean boardBean = new BoardBean();
boardBean.setIdx(rs.getInt("idx"));
boardBean.setHit(rs.getInt("hit"));
boardBean.setName(rs.getString("name"));
boardBean.setEmail(rs.getString("email"));
boardBean.setHomepage(rs.getString("homepage"));
String title = rs.getString("title");
title =title.replaceAll("<","<");
title =title.replaceAll(">",">");
boardBean.setTitle(title);
boardBean.setWdate(rs.getString("wdate").substring(2,10));
//인덱스0부터 10개를 가져온다 = 연,월,일 만꺼내오는것.. (2,10)로 해서 년뒷두자리부터 가져왔다.
boardBean.setDepth(rs.getInt("depth")); //depth 꺼냄
boardList.add(boardBean);
}
}catch(Exception e) {
//TODO: handle exception
}finally {
rs.close();
stat.close();
pool.releaseConnection(conn);
}
return boardList;
} //getBoardList()
오타와의싸움...빡시다
아무리찾아도 보이지않던 오타는 db 테이블 컬럼명이었다..