Servlet/JSP 답변형 게시판 만들기 - 답글

코코·2020년 8월 18일
0

답글

답글은 간단하다. 따로 DAO를 만들 필요가 없다. register메서드를 그대로 이용하면 된다.

  1. post.jsp에서 '답글' 버튼을 누른다.
  2. 해당 post의 번호(bno)를 가지고 rePost.jsp로 이동한다.
  3. 작성한 답글의 데이터로 VO를 만들어서 register메서드를 호출한다.

Controller

재밌는 점은 register와 등록 방식이 완전히 동일하기 때문에 새로운 post메서드를 만들지 않아도 된다는 것이다.
register메서드에 단순히 조건을 하나 추가해주기만 하면 된다! 이른바 재사용성!

@WebServlet("/board/*")
public class BoardController extends HttpServlet {
...

	//GET방식 답글. '/rePost'로 접근하면 답변 폼 rePost.jsp로 보낸다.
 	else if (action.equals("/rePost")) {
		int bno = Integer.parseInt(request.getParameter("bno"));
		log.info("bno : " +bno);
		request.setAttribute("bno", bno);
			
		nextPage="/WEB-INF/board/rePost.jsp";
	} else if (action.equals("/register") || action.equals("/rePostRegister")) {
		//등록한 게시물 데이터를 받아온다.
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String imgName = request.getParameter("imgName");
		String id = request.getParameter("id");
		int p_bno = Integer.parseInt(request.getParameter("pbno"));
			
		//받아온 데이터로 VO객체를 생성한다.
		BoardVO vo = new BoardVO();
			
		vo.setTitle(title);
		vo.setContent(content);
		vo.setImgName(imgName);
		vo.setId(id);
		vo.setP_bno(p_bno);
			
		log.info("vo : " + vo);
			
		//데이터베이스에 저장하는 메서드에 vo를 전달한다.
		//결과가 1이라면 성공, 그 외는 실패..
		nextPage = boardService.register(vo) == 1
						? "/board/list" : "error";
			
	}
    
	RequestDispatcher dispatcher = request.getRequestDispatcher(nextPage);
	dispatcher.forward(request, response);
	}
    
    
}

post.jsp

조회 페이지에서 자바스크립트를 이용해서 bno값과 함께 답변 페이지로 보낸다.

...
	
	<input type='hidden' id='bno' name='bno' value='${post.bno }'>
	</form>
	
<script>
	window.onload = () => {
		const remove = document.getElementById('remove')
		const list = document.getElementById('list')
		const rePost = document.getElementById('rePost')
		const modify = document.getElementById('modify')
		const actionForm = document.getElementById('action')
		const id = document.getElementById('id')
		
		//답글
		document.getElementById('rePost').addEventListener("click", () => {
			actionForm.method = 'post'
			actionForm.action = '/board/rePost'
			actionForm.submit()
			
		})
...

rePost.jsp

형식은 write.jsp와 동일하다.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>답변 작성</title>
</head>
<body>
<form name='action' method='post' action='/board/rePostRegister'>
	<table>
		<tr>
			<td align="right"> 제목 </td>
			<td clspan='2'><input type='text' name='title' size='67' maxlength="50"></td>
		</tr>
		<tr>
			<td align="right" valign="top"><br> 내용 </td>
			<td colspan="2"><textarea rows="10" cols="65" name='content'></textarea></td>
		</tr>
		<tr>
			<td align="right"> 작성자 </td>
			<td clspan='2'><input type='text' name='id' maxlength="50"></td>
		</tr>
		<tr>
			<td align="right"> </td>
			<td colspan="2">
				<input type='submit' value='답변 등록'>
				<input type='button' id='back' value='이전'>
			</td>
		</tr>
	</table>
	<input type="hidden" id='pbno' name='pbno' value='${bno }'>
</form>
<script>
	window.onload = () => {
		const back = document.getElementById('back')
		
		//'이전'버튼 누르면 원글로 돌아감
		back.addEventListener("click", () => {
			const pbno = document.getElementById('pbno')
			location.replace('/board/post?no='+pbno.value)					
		})
	}
</script>
</body>
</html>

0개의 댓글