<c:if test="${sessionScope.id eq dto.writer }">
<a href="updateform?num=${dto.num }">수정</a>
<a href="javascript:" onclick="deleteConfirm()">삭제</a>
<script>
function deleteConfirm(){
const isDelete=confirm("이 글을 삭제 하겠습니까?");
if(isDelete){
location.href="delete?num=${dto.num}";
}
}
</script>
</c:if>
<!-- 수정폼 -->
<form id="updateForm${tmp.num }" class="comment-form update-form" action="comment_update" method="post">
<input type="hidden" name="num" value="${tmp.num }" />
<textarea name="content">${tmp.content }</textarea>
<button type="submit">수정</button>
</form>
<c:when test="${tmp.deleted eq 'yes' }">
<li>삭제된 댓글 입니다.</li>
</c:when>
addUpdateFormListener(".update-form");
addUpdateListener(".update-link");
addDeleteListener(".delete-link");
addReplyListener(".reply-link");
addUpdateListener(".page-"+currentPage+" .update-link");
addDeleteListener(".page-"+currentPage+" .delete-link");
addReplyListener(".page-"+currentPage+" .reply-link");
//새로 추가된 댓글 li 요소 안에 있는 댓글 수정폼에 이벤트 리스너 등록하기
addUpdateFormListener(".page-"+currentPage+" .update-form");
//댓글 삭제 요청 처리
@RequestMapping("/cafe/comment_delete")
@ResponseBody
public Map<String, Object> commentDelete(HttpServletRequest request){
service.deleteComment(request);
Map<String, Object> map = new HashMap<String, Object>();
map.put("isSuccess", true);
//{isSuccess",true}형식의 JSON문자열이 응답되도록 한다.
return map;
}
@Override
public void deleteComment(HttpServletRequest request) {
int num=Integer.parseInt(request.getParameter("num"));
//삭제할 댓글 정보를 읽어와서
CafeCommentDto dto = cafeCommentDao.getData(num);
String id=(String)request.getSession().getAttribute("id");
//글 작성자와 로그인된 아이디와 일치하지 않으면
if(!dto.getWriter().equals(id)) {
throw new NotDeleteException("남의 댓글 지우면 혼난당!");
}
cafeCommentDao.delete(num);
}
[DB에서 확인해보면]
응답은 json 요청은 ajax
//댓글 수정 요청 처리 (JSON을 응답하도록 한다.)
@RequestMapping("/cafe/comment_update")
@ResponseBody
public Map<String, Object> commentUpdate(CafeCommentDto dto){
service.updateComment(dto);
Map<String, Object> map = new HashMap<String, Object>();
map.put("isSuccess", true);
//{isSuccess",true}형식의 JSON문자열이 응답되도록 한다.
return map;
}
@Override
public void updateComment(CafeCommentDto dto) {
cafeCommentDao.update(dto);
}