본문 바로가기
공부기록

5월 24일 Spring - 홈페이지 삭제 처리

by project100 2023. 5. 24.

1. boardcontents - javascript

function delCheck(bnum){
        let conf = confirm("삭제하시겠습니까?");
        if(conf == true){
            location.href = "delete?b_num=" + bnum;
        }
    }

2. boardcontroller

  @GetMapping("delete")
    public String boardDelete(Integer b_num, HttpSession session, RedirectAttributes rttr) {
        log.info("boardDelete()");
        String view = bServ.boardDelete(b_num, session, rttr);
        return view;
    }

3. boardservice

 public String boardDelete(Integer b_num, HttpSession session, RedirectAttributes rttr) {
        log.info("boardDelete()");
        String view = null;
        String msg = null;

        try {
            bDao.deleteReply(b_num);
            deleteFiles(b_num, session);
            bDao.deleteFiles(b_num);
            bDao.deleteBoard(b_num);

            view = "redirect:list?pageNum=1";
            msg = "삭제 성공";
        }catch (Exception e){
            e.printStackTrace();
            view = "redirect:contentes?b_num=" + b_num;
            msg = "삭제 실패";
        }
        rttr.addFlashAttribute("msg", msg);
        return view;
    }

4. boarddao - boarddao.xml 

// 댓글 목록 삭제 메소드 선언
void deleteReply(Integer b_num);
<delete id="deleteReply" parameterType="Integer">
DELETE FROM reply WHERE r_bnum=#{b_num}
</delete>
// 파일 목록 삭제 메소드 선언
void deleteFiles(Integer b_num);
<delete id="deleteFiles" parameterType="Integer">
DELETE FROM boardfile WHERE bf_bnum=#{b_num}
</delete>
// 게시글 삭제 메소드 선언
void deleteBoard(Integer b_num);
<delete id="deleteBoard" parameterType="Integer">
DELETE FROM board WHERE b_num=#{b_num}
</delete>

5. boardservice

private void deleteFiles(Integer b_num, HttpSession session) throws Exception {
        log.info("deleteFiles()");
        List<String> sList = bDao.selectSysname(b_num);

        String realpath = session.getServletContext().getRealPath("/");
        realpath += "upload/";

        for(String s : sList){
            File file = new File(realpath + s);
            if(file.exists()){
                file.delete();
            }
        }
    }
6. boarddao - boarddao.xml 
// 파일의 sysname 목록 가져오는 메소드 선언
List<String> selectSysname(Integer b_num);
<select id="selectSysname" resultType="String" parameterType="Integer">
SELECT bf_sysname FROM boardfile WHERE bf_bnum=#{b_num}
</select>

 

7. controller 연결

String view = bServ.boardDelete(b_num, session, rttr);