spring 스프링 게시판 구현 답글

박예린·2023년 2월 26일

답글

-> bbsdetail.jsp에서 답글 클릭(클릭 시 answer.jsp로 이동)

Utility

Utility.java

package mul.cam.a.util;

public class Utility {

	public static String arrow(int depth){
		String img = "<img src='./images/arrow.png' width='20px' height='20px' />&nbsp;";	
		String nbsp = "&nbsp;&nbsp;&nbsp;&nbsp;";
		
		String ts = "";
		for(int i = 0;i < depth; i++){
			ts += nbsp;
		}
		
		return depth==0?"":ts + img;
	}
}

view

answer.jsp

위에 보이는 답글 view

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

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>

<%
	BbsDto dto = (BbsDto)request.getAttribute("dto");
%>

<div align="center">

<h2>부모글</h2>

<table border="2">
<col width="200"><col width="500">
<tr>
	<th>작성자</th>
	<td><%=dto.getId() %></td>
</tr>
<tr>
	<th>제목</th>
	<td><%=dto.getTitle() %></td>
</tr>
<tr>
	<th>작성일</th>
	<td><%=dto.getWdate() %></td>
</tr>
<tr>
	<th>조회수</th>
	<td><%=dto.getReadcount() %></td>
</tr>
<tr>
	<th>내용</th>
	<td>
		<textarea rows="10" cols="50" readonly><%=dto.getContent() %></textarea>
	</td>
</tr>
</table>

<%
MemberDto login = (MemberDto)session.getAttribute("login");
%>

<h2>답글</h2>

<form action="answerAf.do" method="post">
<input type="hidden" name="seq" value="<%=dto.getSeq() %>">

<table border="1">
<col width="200"><col width="500">
<tr>
	<th>아이디</th>
	<td>
		<input type="text" name="id" size="50" readonly="readonly" value="<%=login.getId() %>">
	</td>
</tr>
<tr>
	<th>제목</th>
	<td>
		<input type="text" name="title" size="50">
	</td>	
</tr>
<tr>
	<th>내용</th>
	<td>
		<textarea rows="10" cols="50" name="content"></textarea>
	</td>
</tr>
<tr>
	<td colspan="2" align="center">
		<input type="submit" value="작성완료">
	</td>
</tr>
</table>
</form>
</div>
</body>
</html>

bbslist.jsp

게시글에 답글을 달았을 때 view

<tbody>

<%
if(list == null || list.size() == 0){
	%>
	<tr>
		<td colspan="4">작성된 글이 없습니다</td>
	</tr>
	<%
}else{
	
	for(int i = 0;i < list.size(); i++)
	{
		BbsDto dto = list.get(i);
		%>
		<tr>
			<th><%=i + 1 + (pageNumber * 10) %></th>
			
			<td style="text-align: left;">
			<%
			if(dto.getDel() == 0){
				%>
					<%=Utility.arrow(dto.getDepth()) %>
					<a href="bbsdetail.do?seq=<%=dto.getSeq() %>">
						<%=dto.getTitle() %>
					</a>						
				<%
			}else if(dto.getDel() == 1){
				%>				
					<%=Utility.arrow(dto.getDepth()) %>
					<font color="#ff0000">*** 이 글은 작성자에 의해서 삭제되었습니다 ***</font>					
				<%
			}	
			%>
			</td>
			
			<td><%=dto.getReadcount() %></td>
			<td><%=dto.getId() %></td>
		</tr>
		<%
	}
}
%>
</tbody>

mapper

Bbs.xml

답글은 두개의 sql문이 필요함
1.step을 한칸 씩 미뤄야 하는 -> 수정
2.등록할 답글을 추가해야 하는 -> 추가

수정

<update id="answerBbsUpdate" parameterType="mul.cam.a.dto.BbsDto">
	update bbs
	set step=step+1
	where ref=(select ref from (select ref from bbs a where seq=#{seq}) A)
		and step>(select step from (select step from bbs b where seq=#{seq}) B)
</update>

추가

<insert id="answerBbsInsert" parameterType="mul.cam.a.dto.BbsDto">
	insert into bbs(id, ref, step, depth, title, content, wdate, del, readcount)
	values(#{id}, (select ref from bbs a where seq=#{seq}),
				  (select step from bbs b where seq=#{seq})+1,
				  (select depth from bbs c where seq=#{seq})+1,
			#{title}, #{content}, now(), 0, 0)
</insert>

Controller

BbsController.java

//답글
@GetMapping(value = "answer.do")
public String answer(Model model, int seq) {
	BbsDto dto = service.getBbs(seq);
	model.addAttribute("dto", dto);
	
	return "answer";
}
	
@PostMapping(value = "answerAf.do")
//추가용
public String answerAf(Model model, int seq, BbsDto dto) { 
	//부모글 시퀀스 & 유저가 보내준 타이틀과 컨텐츠, 아이디 하나로 통합
	dto.setSeq(seq);		
	boolean isS = service.answerBbs(dto);
	String answer = "BBS_ANSWER_OK";
	if(isS == false) {
		answer = "BBS_ANSWER_NG";
	}
	model.addAttribute("answer", answer);
	
	return "message";
}

DAO/Service

BbsDao.java

//답글 - 수정, 추가(Bbs.xml에서)
	int answerBbsUpdate(BbsDto dto); //수정
	int answerBbsInsert(BbsDto dto); //추가

BbsDaoImpl.java

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

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

BbsService.java

boolean answerBbs(BbsDto dto);

BbsServiceImpl.java

//답글
@Override
public boolean answerBbs(BbsDto dto) {
	dao.answerBbsUpdate(dto); //수정part
	int n = dao.answerBbsInsert(dto); //추가part
	return n>0?true:false;
}

Message

message.jsp

답글 메세지 호출

String answer = (String)request.getAttribute("answer");
if(answer != null && !answer.equals("")){
	if(answer.equals("BBS_ANSWER_OK")){
		%>
		<script type="text/javascript">
		alert("답글이 성공적으로 작성되었습니다");
		location.href = "bbslist.do";
		</script>
		<%
	}
	else{		
		%>
		<script type="text/javascript">
		alert("답글을 다시 작성해 주십시오");			
		location.href = "bbslist.do";
		</script>
		<%
	}	
}
profile
개발자를 꿈꾸는 귀여운 나

0개의 댓글