이제 댓글 수정과 삭제를 하는 기능을 구현하도록 하겠습니다.
1. 댓글 수정과 삭제 쿼리
<!-- replyMapper.xml -->
<!-- 댓글 수정 -->
<update id="updateReply" parameterType="ReplyVO">
update reply
set replytext = #{replytext}
where reply_number = #{reply_number}
</update>
<!-- 댓글 삭제 -->
<delete id="deleteReply" parameterType="ReplyVO">
delete
from reply
where reply_number = #{reply_number}
</delete>
<!-- 선택된 댓글 조회 -->
<select id="selectReply" resultType="ReplyVO">
select board_number, reply_number, replytext, replyer, create_date
from reply
where reply_number = #{reply_number}
</select>
2. DAO 작성
package kr.co.web.persistence;
import java.util.List;
import kr.co.web.domain.Criteria;
import kr.co.web.domain.ReplyVO;
public interface ReplyDAO {
public void create(ReplyVO vo) throws Exception;
public void update(ReplyVO vo) throws Exception; // 추가
public void delete(ReplyVO vo) throws Exception; // 추가
public List<ReplyVO> readReply(int board_number) throws Exception;
public ReplyVO selectReply(int reply_number) throws Exception; // 추가
}
package kr.co.web.persistence;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import kr.co.web.domain.Criteria;
import kr.co.web.domain.ReplyVO;
@Repository
public class ReplyDAOImpl implements ReplyDAO{
@Inject
SqlSession session;
private static final String NS = "replyMapper";
private static final String CREATE = NS + ".writeReply";
private static final String UPDATE = NS + ".updateReply"; // 추가
private static final String DELETE = NS + ".deleteReply"; // 추가
private static final String READ = NS + ".readReply";
private static final String SELECTREPLY = NS + ".selectReply"; // 추가
@Override
public void create(ReplyVO vo) throws Exception {
session.insert(CREATE, vo);
}
@Override
public void update(ReplyVO vo) throws Exception { // 추가
session.update(UPDATE, vo);
}
@Override
public void delete(ReplyVO vo) throws Exception { // 추가
session.delete(DELETE, vo);
}
@Override
public List<ReplyVO> readReply(int board_number) throws Exception {
return session.selectList(READ, board_number);
}
@Override
public ReplyVO selectReply(int reply_number) throws Exception { // 추가
return session.selectOne(SELECTREPLY, reply_number);
}
}
3. SERVICE 작성
package kr.co.web.service;
import java.util.List;
import kr.co.web.domain.Criteria;
import kr.co.web.domain.ReplyVO;
public interface ReplyService {
public void register(ReplyVO vo) throws Exception;
public void modify(ReplyVO vo) throws Exception; // 추가
public void remove(ReplyVO vo) throws Exception; // 추가
public List<ReplyVO> readReply(int board_number) throws Exception;
public ReplyVO selectReply(int reply_number) throws Exception; // 추가
}
package kr.co.web.service;
import java.util.List;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import kr.co.web.domain.Criteria;
import kr.co.web.domain.ReplyVO;
import kr.co.web.persistence.ReplyDAO;
@Service
public class ReplyServiceImpl implements ReplyService{
@Inject
ReplyDAO replyDAO;
@Override
public void register(ReplyVO vo) throws Exception {
replyDAO.create(vo);
}
@Override
public void modify(ReplyVO vo) throws Exception { // 추가
replyDAO.update(vo);
}
@Override
public void remove(ReplyVO vo) throws Exception { // 추가
replyDAO.delete(vo);
}
@Override
public List<ReplyVO> readReply(int board_number) throws Exception {
return replyDAO.readReply(board_number);
}
@Override
public ReplyVO selectReply(int reply_number) throws Exception { // 추가
return replyDAO.selectReply(reply_number);
}
}
4. Controller 댓글 수정 메서드 작성
<!-- BoardController -->
@RequestMapping(value="/replyUpdateView", method = RequestMethod.GET)
public String replyUpdateView(ReplyVO vo, Criteria cri, Model model) throws Exception {
logger.info("reply write");
ReplyVO reply = replyService.selectReply(vo.getReply_number());
logger.info("댓글번호 : " + reply.getReply_number());
model.addAttribute("replyUpdate", replyService.selectReply(vo.getReply_number()));
model.addAttribute("cri", cri);
return "board/replyUpdateView";
}
@RequestMapping(value="/replyUpdate", method = RequestMethod.POST)
public String replyUpdate(ReplyVO vo, Criteria cri, RedirectAttributes ra) throws Exception {
logger.info("reply Write");
replyService.modify(vo);
ra.addAttribute("board_number", vo.getBoard_number());
ra.addAttribute("page", cri.getPage());
ra.addAttribute("perPageNum", cri.getPerPageNum());
return "redirect:/board/readView";
}
5. Controller 댓글 삭제 메서드 작성
<!-- BoardController -->
@RequestMapping(value="/replyDeleteView", method = RequestMethod.GET)
public String replyDeleteView(ReplyVO vo, Criteria cri, Model model) throws Exception {
logger.info("reply Delete");
model.addAttribute("replyDelete", replyService.selectReply(vo.getReply_number()));
System.out.println("댓글 번호 : " + replyService.selectReply(vo.getReply_number()) );
model.addAttribute("cri", cri);
return "board/replyDeleteView";
}
@RequestMapping(value="/replyDelete", method = RequestMethod.POST)
public String replyDelete(ReplyVO vo, Criteria cri, RedirectAttributes ra) throws Exception {
logger.info("reply delete");
replyService.remove(vo);
ra.addAttribute("board_number", vo.getBoard_number());
ra.addAttribute("page", cri.getPage());
ra.addAttribute("perPageNum", cri.getPerPageNum());
return "redirect:/board/readView";
}
6. readView.jsp 댓글 수정, 삭제 버튼 추가
<!-- 댓글 수정, 삭제 버튼 추가 -->
<div id="reply">
<ol class="replyList">
<c:forEach items="${replyList}" var="replyList">
<li>
<p>
작성자 : ${replyList.replyer}<br />
작성 날짜 : <fmt:formatDate value="${replyList.create_date}" pattern="yyyy-MM-dd" />
</p>
<p>${replyList.replytext}</p>
<div>
<button type="button" class="replyUpdateBtn" data-reply_number="${replyList.reply_number}">수정</button> // 추가
<button type="button" class="replyDeleteBtn" data-reply_number="${replyList.reply_number}">삭제</button> // 추가
</div>
</li>
</c:forEach>
</ol>
</div>
7. readView.jsp 수정, 삭제 script 작성
$(function () {
updateReply();
deleteReply();
})
function updateReply() {
$(".replyUpdateBtn").on("click", function(){
location.href = "/board/replyUpdateView?board_number=${BoardVO.board_number}"
+ "&page=${cri.page}"
+ "&perPageNum=${cri.perPageNum}"
+ "&reply_number="+$(this).attr("data-reply_number");
});
}
function deleteReply() {
$(".replyDeleteBtn").on("click", function(){
location.href = "/board/replyDeleteView?board_number=${BoardVO.board_number}"
+ "&page=${cri.page}"
+ "&perPageNum=${cri.perPageNum}"
+ "&reply_number="+$(this).attr("data-reply_number");
});
}
8. replyUpdateView.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var formObj = $("form[name='updateForm']");
$(".cancel_btn").on("click", function() {
location.href = "/board/readView?board_number=${replyUpdate.board_number}"
+ "&page=${cri.page}"
+ "&perPageNum=${cri.perPageNum}";
})
})
</script>
</head>
<body>
<div>글번호 : ${BoardVO.board_number}</div>
<section id="container">
<form name="updateForm" role="form" method="post" action="/board/replyUpdate">
<input type="hidden" name="board_number" value="${replyUpdate.board_number}" readonly="readonly"/>
<input type="hidden" id="reply_number" name="reply_number" value="${replyUpdate.reply_number}" />
<input type="hidden" id="page" name="page" value="${cri.page}">
<input type="hidden" id="perPageNum" name="perPageNum" value="${cri.perPageNum}">
<label for="replytext">댓글 내용</label><input type="text" id="replytext" name="replytext" value="${replyUpdate.replytext}"/>
<div>
<button type="submit" class="update_btn">저장</button>
<button type="button" class="cancel_btn">취소</button>
</div>
</form>
</section>
</body>
</html>
9. replyDeleteView.jsp 생성
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var formObj = $("form[name='deleteForm']");
$(".cancel_btn").on("click", function() {
location.href = "/board/readView?board_number=${replyDelete.board_number}"
+ "&page=${cri.page}"
+ "&perPageNum=${cri.perPageNum}";
})
})
</script>
</head>
<body>
<div>글번호 : ${replyDelete.board_number}</div>
<section id="container">
<form name="deleteForm" role="form" method="post" action="/board/replyDelete">
<input type="hidden" name="board_number" value="${replyDelete.board_number}" readonly="readonly"/>
<input type="hidden" id="reply_number" name="reply_number" value="${replyDelete.reply_number}" />
<input type="hidden" id="replytext" name="replytext" value="${replyDelete.replytext}" />
<input type="hidden" id="page" name="page" value="${cri.page}">
<input type="hidden" id="perPageNum" name="perPageNum" value="${cri.perPageNum}">
<div>
<p>삭제 하시겠습니까?</p>
<button type="submit" class="delete_btn">삭제</button>
<button type="button" class="cancel_btn">취소</button>
</div>
</form>
</section>
</body>
</html>
'JAVA > blog' 카테고리의 다른 글
회원 (2) - 로그인, 로그아웃 기능 (0) | 2020.07.31 |
---|---|
회원 (1) - 가입 기능 (0) | 2020.07.31 |
댓글 구현(2) - 게시판 댓글 작성 (0) | 2020.07.27 |
댓글 구현(1) - 게시판 댓글 목록 구현 (0) | 2020.07.27 |
검색(3) (0) | 2020.07.24 |