detail.jsp
//댓글 목록
function commentList() {
$.ajax({
url:'/comment/list'
,type:'get'
,data:{'pno':pno} //부모글번호
,success:function(data){
//alert(data);
let a = ''; //출력할 결과값
$.each(data, function(key, value){
//alert(key); //순서 0 1 2
//alert(value); //[object object]
//alert(value.cno);
//alert(value.pno);
//alert(value.content);
//alert(value.wname);
//alert(value.regdate);
a +='댓글번호:' + value.cno + ' / 작성자 : ' + value.wname + " " + value.regdate;
a +='<p>내용 : ' + value.content + '</p>';
});//each() end
$(".commentList").html(a);
}//success end
});
}//commentList() end
$(document).ready(function(){ //페이지 로딩시 댓글 목록 출력
commentList();
});//ready() end
</script>
//댓글 목록
function commentList() {
$.ajax({
url:'/comment/list'
,type:'get'
,data:{'pno':pno} //부모글번호
,success:function(data){
//alert(data);
let a = ''; //출력할 결과값
$.each(data, function(key, value){
//alert(key); //순서 0 1 2
//alert(value); //[object object]
//alert(value.cno);
//alert(value.pno);
//alert(value.content);
//alert(value.wname);
//alert(value.regdate);
a += '<div class="commentArea" style="border-bottom:1px solid darkgray; margin-bottm:15px;">';
a += ' 댓글번호:' + value.cno + ' / 작성자 : ' + value.wname + " " + value.regdate;
a += ' <div class="commentContent' + value.cno + '">';
a += ' <p>내용 : ' + value.content + '</p>';
a += ' </div>';
a += '</div>';
});//each() end
$(".commentList").html(a);
}//success end
});
}//commentList() end
$.each(data, function(key, value){
//alert(key); //순서 0 1 2
//alert(value); //[object object]
//alert(value.cno);
//alert(value.pno);
//alert(value.content);
//alert(value.wname);
//alert(value.regdate);
a += '<div class="commentArea" style="border-bottom:1px solid darkgray; margin-bottm:15px;">';
a += ' <div class="commentInfo' + value.cno + '">';
a += ' 댓글번호:' + value.cno + ' / 작성자 : ' + value.wname + " " + value.regdate;
a += ' </div>';
a += ' <div class="commentContent' + value.cno + '">';
a += ' <p>내용 : ' + value.content + '</p>';
a += ' </div>';
a += '</div>';
});//each() end
a += '<div class="commentArea" style="border-bottom:1px solid darkgray; margin-bottm:15px;">';
a += ' <div class="commentInfo' + value.cno + '">';
a += ' 댓글번호:' + value.cno + ' / 작성자 : ' + value.wname + " " + value.regdate;
a += ' <a href="javascript:commentUpdate('+ value.cno + ', \''+ value.content +'\')">수정</a>';
a += ' <a href="javascript:commentDelete(' + value.cno + ')">삭제</a>';
a += ' </div>';
a += ' <div class="commentContent' + value.cno + '">';
a += ' <p>내용 : ' + value.content + '</p>';
a += ' </div>';
a += '</div>';
});//each() end
댓글 삭제
detail.jsp
//댓글 삭제
function commentDelete(cno) {
$.ajax({
url:'/comment/delete/' + cno
,type:'post'
,success:function(data){
if(data==1) {
commentList();//댓글 삭제후 목록 출력
}//if end
}//success end
});//ajax() end
}//commentDelete() end
CommentCont.java
@RequestMapping("/delete/{cno}")
@ResponseBody
private int mCommentServiceDelete(@PathVariable int cno) throws Exception {
return commentDao.commentDelete(cno);
}//mCommentServiceDelete() end
CommentDAO.java
public int commentDelete(int cno) throws Exception {
return sqlSession.delete("comment.delete", cno);
}//commentDelete() end
comment.xml
<delete id="delete" parameterType="int">
DELETE FROM pcomment
WHERE cno=#{cno}
</delete>
댓글 수정
detail.jsp
//댓글 수정
function commentUpdate(cno, content) {
let a = '';
a += '<div class="input-group">';
a += ' <input type="text" id="content_' + cno + '" value="' + content + '">';
a += ' <button type="button" onclick="commentUpdateProc(' + cno + ')">수정</button>';
a += '</div>';
$('.commentContent' + cno).html(a);
}//commentUpdate() end
function commentUpdateProc(cno) {
let updateContent = $('#content_' + cno).val();
$.ajax({
url:'/comment/update'
,type:'post'
,data:{'content':updateContent, 'cno':cno}
,success:function(data) {
if(data==1) commentList(); //댓글 수정 후 목록 출력
}
});//ajax() end
}//commentUpdateProc() end
CommentCont.java
@RequestMapping("/update")
@ResponseBody
private int mCommentServiceUpdateProc(@RequestParam int cno, @RequestParam String content) throws Exception {
CommentDTO comment = new CommentDTO();
comment.setCno(cno);
comment.setContent(content);
return commentDao.commentUpdate(comment);
}//mCommentServiceUpdateProc() end
CommentDAO.java
public int commentUpdate(CommentDTO comment) {
return sqlSession.update("comment.update", comment);
}//update() end
comment.xml
<update id="update" parameterType="kr.co.itwill.comment.CommentDTO">
UPDATE pcomment
SET content=#{content}
WHERE cno=#{cno}
</update>
'웹개발 교육 > Spring' 카테고리의 다른 글
[78일] Spring (26) - MyBatis 프로젝트 호스팅 (0) | 2022.11.16 |
---|---|
[76일] Spring (24) - MyBatis 프로젝트 (댓글 게시판) (0) | 2022.11.14 |
[75일] Spring (23) - MyBatis 프로젝트 (삭제) (0) | 2022.11.11 |
[75일] Spring (22) - MyBatis 프로젝트 (수정) (0) | 2022.11.11 |
[75일] Spring (21) - MyBatis 프로젝트 (상세보기) (0) | 2022.11.11 |