Spring/SpringBoot CRUD 게시판 (Delete)

SpringBoot 게시판 Delete

I want to Sleep 2023. 8. 17. 22:06
728x90

● header 맨아래쪽에 addFlashAttribue 메소드를 위해 코드 추가

{{#msg}}
<div class="alert alert-primay alert-dismissible">
{{msg}}
<button type="button" class="btn-close" data-bs-dismiss="alert"
aria-label="Close"></button>

</div>
{{/msg}}

● Controller

//삭제
@GetMapping("/list/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes rttr){
//삭제할 대상 가져오기
Member target = memberRepository.findById(id).orElse(null);
if(target != null){
memberRepository.delete(target);
rttr.addFlashAttribute("msg","삭제가 완료되었습니다");
}
return "redirect:/index" ;
}

● list.mustache

{{>layout/header}}

<table class="table">
<thead>

<tr>
<th>id</th>
<td>title</td>
<td>content</td>
</tr>

</thead>
<tbody>
{{#member}}
<tr>
<th>{{id}}</th>
<th>{{title}}</th>
<th>{{content}}</th>
</tr>
{{/member}}
</tbody>
</table>
<a href="/index">전체 글 보기</a>
<a href="/list/{{member.id}}/update">수정 폼</a>
<a href="/list/{{member.id}}/delete">글 삭제</a>
{{>layout/footer}}

※ 이것으로 모든 curd 게시판 작업은 끝났습니다 JPA가 제공해주는 직관적인 메소드로 인해서 복잡한 구조 필요없이 손쉽게 간단한 게시판을 구현해보았습니다.