1. 게시물 목록 페이지 이동
1) controller
@GetMapping("/board/list")
    public String boardList(Model model){
        model.addAttribute("list",boardService.boardList());
        return "boardlist";
}
2) service
 public List<Board> boardList(){
        return boardRepository.findAll();
 }
.findAll() : Board라는 class가 담긴 list를 찾아 반환
3) view(html)
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>게시글 리스트 페이지</title>
</head>
<style>
    .layout{
        width : 500px;
        margin : 0 auto;
        margin-top : 40px;
    }
</style>
<body>
<div class="layout">
    <table>
        <thead>
        <tr>
            <th>글 번호</th>
            <th>제목</th>
        </tr>
        </thead>
        <tbody>
        <tr>
        <tr th:each="board : ${list}">
            <td th:text="${board.id}">1</td>
            <td>
                <a th:text="${board.title}" th:href="@{/board/view(id=${board.id})}">제목입니다.</a>
            </td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>
html 태그에 xmlns:th="https://www.thymeleaf.org" 선언 : thymeleaf 탬플릿을 통해 데이터 받아옴
th:each : each는 반복문 list에서 board에 값을 하나씩 받아옴
2. 게시물 상세 페이지
1) controller
 @GetMapping("/board/view")
public String boardview(Model model, Integer id){
    model.addAttribute("board", boardService.boardView(id));
    return "boardview";
}
2) service
public Board boardView(Integer id){
    return boardRepository.findById(id).get();
}findById : integer 형으로 불러올 수 있어 Integer 자료형인 id를 인자로 줌
get()을 통해 값을 받아옴
3) view(html)
(1) boardview.html
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>게시글 상세 페이지</title>
</head>
<body>
<h1 th:text="${board.title}">제목입니다.</h1>
<p th:text="${board.content}">내용이 들어갈 부분입니다.</p>
</body>thymeleaf를 사용하여 title과 content을 불러오게 설정
(2) boardlist.html
<tr th:each="board : ${list}">
    <td th:text="${board.id}">1</td>
    <td>
        <a th:text="${board.title}" th:href="@{/board/view(id=${board.id})}">제목입니다.</a>
    </td>
</tr>thymeleaf를 사용해서 href를 지정
(id=${board.id}) 이런식으로 지정된 board의 id를 변수로 관리
3. 게시물 삭제
1) controller
@GetMapping("/board/delete")
public String boardDelete(Integer id){
    boardService.boardDelete(id);
    return "redirect:/board/list";
}id에 의해 삭제하게끔 처리, boardDelete 메서드 후 return 하는 redirect를 통해 list로 가게끔 처리
2) service
public void boardDelete(Integer id){
    boardRepository.deleteById(id);
}boardRepository에서 제공하는 id에 따른 삭제를 사용
매개변수로 Integer형인 id 인자 지정
3) view (html)
<body>
<h1 th:text="${board.title}">제목입니다.</h1>
<p th:text="${board.content}">내용이 들어갈 부분입니다.</p>
<a th:href="@{/board/delete(id=${board.id})}">글 삭제</a>
</body>id 파라미터를 같이 넘겨주는 delete로 이동하게끔 지정
https://minddokddok.tistory.com/32?category=1040940
스프링 부트(Spring Boot)로 간단한 게시판 만들기 - 3
이어서, 4) 게시물 리스팅 - 게시물 리스트 페이지 생성 - 게시물 리스트 페이지에 저장된 게시글 출력 단계 차례인데, 그 전에 게시물들을 좀 생성한 후에 게시물 페이지를 만들면 더 잘 와닿을
minddokddok.tistory.com
위의 해당글을 참고하였습니다.
'Spring > SpringBoot' 카테고리의 다른 글
| [SpringBoot] 호텔예약사이트 - 로그인 구현 (0) | 2024.03.10 | 
|---|---|
| [SpringBoot] 간단한 게시판 생성_페이징처리 (0) | 2024.01.20 | 
| [SpringBoot] 간단한 게시판 생성_첨부파일 업로드 (0) | 2024.01.15 | 
| [SpringBoot] 간단한 게시판 생성_글수정 (0) | 2024.01.15 | 
| [SpringBoot] 간단한 게시판 생성_글등록 (0) | 2024.01.12 | 
 
                    
                   
                    
                   
                    
                  