Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자바
- jpa
- 탐사수
- t삭제(delete)
- springboot
- 자바#게임
- 조건문#중첩반복문#기본
- sprinboot
- 문자와숫자#특수기호
- 절차지향
- 쿠팡
- Update
- 오버라이딩
- /자바 #/간단한프로그램
- CRUD
- 물
- 자바#제네릭스기본#List개념
Archives
- Today
- Total
간단
SpringBoot 게시판 Delete 본문
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}}
<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" ;
}
@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}}
<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가 제공해주는 직관적인 메소드로 인해서 복잡한 구조 필요없이 손쉽게 간단한 게시판을 구현해보았습니다.