게시판 삭제 기능을 구현해 보았다
나는 삭제 버튼을
이렇게 리스트 옆에 넣었다.
그래서 먼저 조회 페이지에 삭제 버튼을 만들었다
mustache(index.mustache)
{{>Layouts/header}}
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Content</th>
</tr>
</thead>
<tbody>
{{#articleList}}
<tr>
<th>{{id}}</th>
<td><a href="/articles/{{id}}/update">{{title}}</a></td>
<td>{{content}}</td>
<td><td><a href="/articles/{{id}}/delete" class="btn btn-danger">Delete</a></td></td>
</tr> //삭제 버튼 추가
{{/articleList}}
</tbody>
</table>
<a href="/articles/new">New Article</a>
{{>Layouts/footer}}
Controller(TestController.java)
@GetMapping("/articles/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes rttr){
log.info("삭제 요청");
Test target = testRepository.findById(id).orElse(null);
log.info(target.toString());
if(target != null){
testRepository.delete(target);
rttr.addFlashAttribute("msg","삭제가 완료되었습니다!");
}
return "redirect:/articles/";
}
리다이렉트란 클라이언트에게 재요청을 지시하는 것이다
재요청을 받은 클라이언트는 재요청을 받은 주소에 따라서 다시 재요청을 보낸다
그럼 서버는 받은 주소로 다시 요청을 한다.
-사용방법-
redirect: 뒤에 url주소를 적어주면 끝이다
결과물 모습이다
1번의 삭제 버튼을 누르면..
1번 게시글이 삭제가 된 모습이다!