본문 바로가기
프로젝트/게시판

[SpringBoot] 게시판 7. 게시글 삭제 기능 구현

by qkzkdo 2023. 8. 4.
728x90

구현 순서

1. board-list.html 

2. BoardController

3. BoardService

4. BoardServiceProcess

5. BoardMapper

6. board-mapper.xml

7. 게시글 삭제 구현 화면

 

 

1. board-list.html

board-list에 삭제 <from>태그를 넣어준다

<td>
    <form th:action="|/board/delete/${dto.no}|" method="post" onsubmit="return delete_event()">
        <input type="hidden" name="_method" value="DELETE"> 
        <button type="submit">삭제</button>
        <script type="text/javascript">
            function delete_event(){
                if(confirm("정말 삭제하시겠습니까?") == true){
                    return true; 
                 }else{
                    return false; 
                 }
            }
        </script>
    </form>
</td>
  • onsubmit="return delete_event()" 폼이 제출되기 직전에 delete_event() 함수를 실행하고, 그 함수의 반환 값을 폼 제출 여부로 사용
  • confirm() : 사용자에게 확인 대화 상자를 표시하고 '확인' 또는 '취소' 버튼을 눌렀을 때 true 또는 false 값을 반환한다
    '확인'을 선택한 경우에만 아래 코드 블록이 실행된다

 

히든메서드(@DeleteMapping, @PutMapping)를 사용하기 위해 히든타입 태그를 넣어준다.

<input type="hidden" name="_method" value="DELETE">

 

주의할 점

@SpringBootApplication 클래스에 아래 코드를 작성해야 @DeleteMapping, @PutMapping을 사용할 수 있다.

@SpringBootApplication
public class BoardProject01Application {

	//hidden method(put, delete)사용하기 위해 작성
	@Bean
	public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
		return new HiddenHttpMethodFilter();
	}
}

 

 

2. BoardController

히든메서드인 DeleteMapping을 사용해서 게시글 삭제 메서드 작성

//게시글 삭제
@DeleteMapping("/board/delete/{no}")
public String boardDelete(@PathVariable long no) {
    service.boardDeleteProcess(no);
    return "redirect:/board-list";
}

 

 

3. BoardService

BoardService 인터페이스에 boardDeleteProcess(long no) 메서드 선언

//게시글 삭제
void boardDeleteProcess(long no);

 

 

4. BoardServiceProcess

BoardService 인터페이스의 boardDeleteProcess(long no) 메서드를 구현

//게시글 삭제
@Override
public void boardDeleteProcess(long no) {
    mapper.deleteByNo(no);
}

 

 

5. BoardMapper

XML 파일에 정의된 쿼리를 호출할 deleteByNo(long no) 메서드 선언

void deleteByNo(long no);

 

 

6. board-mapper.xml

동적 쿼리 작성

<delete id="deleteByNo">
    delete from board where no = #{no}
</delete>

 

 

7. 게시글 삭제 구현 화면

 

728x90