댓글 insert/delete

지환·2024년 1월 5일
0

그룹웨어

목록 보기
10/17

이번 시간에 댓글 insert/delete에 대한 기록을 남기겠다.

핵심 코드는 이와같다

insert

controller

    @GetMapping("commentInsert")
    public String commentInsert(@RequestParam(required = true) Map<String, Object> pmap) {
        logger.info("commentInsert");
        int result = boardLogic.boardInsert(pmap);
        String b_no = (String) pmap.get("b_no");
        /*
         * 현재 페이지는 detial 페이지. redriect / forward 둘 중에 뭘 해야하는가?
         * forward로 뿌려줘야 하는가?
         * 리다이렉트를 해야지 서버를 경유하고 그 값을 꺼내준다. 그렇게 되면
         * 요청객체와 응답객체는 끊어지는가? yes
         * 그렇다면 전체 값이 필요한것인가? no
         * b_no(pk)값으로 값만 넘기면 된다. (쿼리스트링으로)
         * 
         */
        String url = "redirect:./boardDetail?b_no=" + b_no;

        return url;
    }

Logic

    public int boardInsert(Map<String, Object> pmap) {
        int result = 0;
        result = boardDao.commentInsert(pmap);
        logger.info("boardInsert :" + result);
        return result;
    }

Dao

    public int commentInsert(Map<String, Object> pmap) {// gubun:n_title, keyword:휴관
        logger.info("commentList");
        int result = 0;
        // JAVA -> MyBatis -> Oracle
        result = sqlSessionTemplate.insert("commentInsert", pmap);
        return result;
    }

jsp

<%
    //상세보기 한건 size값 담기
	int size = 0;
    //b_no가 2인 글번에 해당하는 댓글 3 건 size값 담기
	int size2 = 0;
    //insert here - boardDetail(b_no=2) -> hitCount(b_no=2) -> commentList:List
    //bList안에 키값이 comments인 bList.get(1) -> commentList
    //        model.addAttribute("bList", bList);
    List<Map<String,Object>> bList = (List)request.getAttribute("bList");
    List<Map<String,Object>> commentList = null;
    Map<String,Object> rmap = new HashMap<>();
    if(bList !=null){
        size = bList.size();
        rmap = bList.get(0);//상세보기 내용들 담김
        //{comments:[{},{},{}]}
		if(size==2){
			Map<String,Object> comments = (Map)bList.get(1);
			
			if(comments.containsKey("comments")){
				List<Map<String,Object>> comList = (List)comments.get("comments");//3건
				commentList = (List)comments.get("comments");
				size2 = comList.size();//3
			}
		}//end of 댓글이 존재할 때만
    }//end of 게시글 상세 내용 가져오기
%>   


<script type="text/javascript">

const commentInsert = () => {
    document.querySelector("#f_comment").submit();

}
</script>

<!-- 댓글목록 시작  -->		
		<div class="card">
			<div class="card-header">댓글 리스트</div>
			<ul id="comment--items" class="list-group">
<% if(size>0){
    for(int i =0; i<size2; i++)
    {
        Map<String,Object> cmap = (Map)commentList.get(i);
            
%>
<li class="list-group-item d-flex justify-content-between">
    <div><%=cmap.get("BC_COMMENT")%></div>
    <div class="d-flex">
        <div><%=cmap.get("BC_WRITER")%></div>
        <button class="badge btn btn-danger" onclick="commentDelete('<%=cmap.get("BC_NO")%>')">삭제</button>
    </div>
</li>

<% 
        }
    }

%>
			</ul>
		</div>		
		<!-- 댓글목록  끝  -->		

댓글 삭제

Controller

    @GetMapping("commentDelete")
    public String commentDelete(int bc_no, int b_no) {
        logger.info("commentDelete");
        /* 여기서 중요한 부분은 bc_no을 넘겨준다는 점. -> b_no가 아니라, */
        boardLogic.commentDelete(bc_no);
        String url = "redirect:./boardDetail?b_no=" + b_no;

        return url;
    }

Logic

   public int commentDelete(int bc_no) {
        int result = 0;
        result = boardDao.commentDelete(bc_no);
        logger.info("boardInsert :" + result);
        return result;
    }

Dao

 public int commentDelete(int bc_no) {// gubun:n_title, keyword:휴관
        logger.info("commentDelete");
        int result = 0;
        // JAVA -> MyBatis -> Oracle
        result = sqlSessionTemplate.delete("commentDelete", bc_no);
        return result;
    }

JSP


const commentDelete = (bc_no) =>{
    location.href="./commentDelete?bc_no="+bc_no+"&b_no="+<%=rmap.get("B_NO")%>;

}

<button class="badge btn btn-danger" onclick="commentDelete('<%=cmap.get("BC_NO")%>')">삭제</button>
  • 대소문자 차이도 알고 있어야한다.


profile
아는만큼보인다.

0개의 댓글