YSHUSH

게시판4. 답글기능 만들기 본문

Coding/SpringFramework

게시판4. 답글기능 만들기

코딩; 2022. 1. 28. 08:36

답글 기능이 있는 게시판을 만들어보도록 하자

 

1. bbsdetail에 버튼추가

<%
MemberDto mem = (MemberDto)request.getSession().getAttribute("login");
%>

<button type="button" onclick="answerbbs(<%=bbs.getSeq() %>)">답글</button>

<%
if(mem.getId().equals( bbs.getId() )){
%>
	<button type="button" onclick="updatebbs(<%=bbs.getSeq() %>)">수정</button>
	
	<button type="button" onclick="deletebbs(<%=bbs.getSeq() %>)">삭제</button>
<%
}
%>
</div>

<script type="text/javascript">
function answerbbs( seq ) {
	location.href = "answer.do?seq=" + seq;
}

function updatebbs( seq ) {
	location.href = "updatebbs.do?seq=" + seq;
}
function deletebbs( seq ) {
	location.href = "deletebbs.do?seq=" + seq;
}
</script>

로그인 세션도 가져오고 본인의 글만 수정, 삭제가 가능하도록 한다.

</table>밑 부분에 추가

 

2. BbsController 업데이트

@RequestMapping(value = "answer.do", method = RequestMethod.GET)
public String answer(Model model, int seq) {
    logger.info("BbsController answer() " + new Date());

    // DB -> BbsDto
    BbsDto bbs = service.getBbs(seq);
    // 짐싸!
    model.addAttribute("bbs", bbs);

    return "bbsanswer";
}

 

3. views에 bbsanswer.jsp만들고 형식 업데이트

<%@page import="mul.camp.a.dto.MemberDto"%>
<%@page import="mul.camp.a.dto.BbsDto"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%
	BbsDto bbs = (BbsDto)request.getAttribute("bbs");

	MemberDto mem = (MemberDto)request.getSession().getAttribute("login");
%>    
    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bbsanswer</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div align="center">
<h1>부모글</h1>
<table class="table table-bordered" style="width: 1000px">
<tr>
	<th>작성자</th>
	<td><%=bbs.getId() %></td>
</tr>
<tr>
	<th>제목</th>
	<td><%=bbs.getTitle() %></td>
</tr>
<tr>
	<th>작성일</th>
	<td><%=bbs.getWdate() %></td>
</tr>
<tr>
	<th>내용</th>
	<td align="center">
		<textarea rows="15" cols="100" readonly="readonly"><%=bbs.getContent() %></textarea>
	</td>
</tr>
</table>

<h1>답글</h1>

<form action="answerAf.do" method="get">

<!-- 부모글에 대한 sequence -->
<input type="hidden" name="seq" value="<%=bbs.getSeq() %>">

<table class="table table-bordered" style="width: 1000px">
<tr>
	<th>아이디</th>
	<td>
		<input type="text" name="id" size="70px" value="<%=mem.getId() %>" readonly="readonly">
	</td>	
</tr>
<tr>
	<th>제목</th>
	<td>
		<input type="text" name="title" size="70px">
	</td>
</tr>
<tr>
	<th>내용</th>
	<td>
		<textarea rows="20" cols="80" name="content"></textarea>
	</td>
</tr>
<tr>
	<td colspan="2">
		<input type="submit" value="글쓰기">
	</td>
</tr>
</table>
</form>
</div>
</body>
</html>

 

4. BbsController 1차 업데이트

@RequestMapping(value = "answerAf.do", method = RequestMethod.GET)
public String answerAf(BbsDto dto) {
    logger.info("BbsController answerAf() " + new Date());

    // DB	-> update, insert 2가지 작업을 한꺼번에 해줘야함		
//	System.out.println(dto.toString());

    return "redirect:/bbslist.do";
}

 

5. Bbs.xml

<!-- answer -->
<update id="replyBbsUpdate" parameterType="mul.camp.a.dto.BbsDto">
	UPDATE BBS
	SET STEP = STEP + 1
	WHERE REF = (SELECT REF FROM BBS WHERE SEQ=#{seq})
		AND STEP > (SELECT STEP FROM BBS WHERE SEQ=#{seq})
</update>

<insert id="replyBbsInsert" parameterType="mul.camp.a.dto.BbsDto">
	INSERT INTO BBS(SEQ, ID, REF, STEP, DEPTH, 
					TITLE, CONTENT, WDATE, DEL, READCOUNT)
	VALUES(SEQ_BBS.NEXTVAL, #{id},
			(SELECT REF FROM BBS WHERE SEQ=#{seq}),		<!-- ref --> 
			(SELECT STEP FROM BBS WHERE SEQ=#{seq}) + 1, <!-- step -->
			(SELECT DEPTH FROM BBS WHERE SEQ=#{seq}) + 1, <!-- depth -->
			#{title}, #{content}, SYSDATE,
			0, 0)	
</insert>

 

6. BbsDao, BbsDaoImpl

BbsDao

int replyBbsUpdate(BbsDto dto);
int replyBbsInsert(BbsDto dto);

BbsDaoImpl

@Override
public int replyBbsUpdate(BbsDto dto) {
    int n = session.update(ns + "replyBbsUpdate", dto);
    return n;
}

@Override
public int replyBbsInsert(BbsDto dto) {	
    int n = session.insert(ns + "replyBbsInsert", dto);
    return n;
}

 

7. BbsService, BbsServiceImpl

BbsService

void reply(BbsDto dto);

BbsServiceImpl

@Override
public void reply(BbsDto dto) {
    int n = dao.replyBbsUpdate(dto);
    if(n == 0) {
        System.out.println("replyBbsUpdate fail~");
    }

    n = dao.replyBbsInsert(dto);
    if(n == 0) {
        System.out.println("replyBbsInsert fail~");
    }
}

 

8. BbsController 2차 업데이트 → 서비스만 추가

@RequestMapping(value = "answerAf.do", method = RequestMethod.GET)
public String answerAf(BbsDto dto) {
    logger.info("BbsController answerAf() " + new Date());

    // DB	-> update, insert		
//	System.out.println(dto.toString());
    service.reply(dto);

    return "redirect:/bbslist.do";
}