Hello

게시글 목록

by 볼빵빵오춘기

index.html

글 목록 링크 추가한다.

<h3>Board2 연습</h3>
<button onclick="saveReq()">글작성</button>
<a href="/board2/save">글작성</a><br>

<a href="/board2/">목록</a>

 

Board2Controller

@GetMapping("/")
public String findAll(Model model){
    List<Board2DTO> board2DTOList =board2Service.findAll();
    model.addAttribute("boardList",board2DTOList);
    return "board2list";
}

 

board2Service

board2Service의 findAll()은 List<Board2DTO>로 반환해야하기때문에 for문을 이용하여 Entity List를 DTO List로 변환하여 넣어줘야한다.

public List<Board2DTO> findAll() {
    List<Board2Entity> board2EntityList = board2Repository.findAll();
    List<Board2DTO> board2DTOList = new ArrayList<>();

    for(Board2Entity board2Entity:board2EntityList){
        board2DTOList.add(Board2DTO.toBoard2DTO(board2Entity));
    }

    return board2DTOList;
}

 

Board2DTO

public static Board2DTO toBoard2DTO(Board2Entity board2Entity){
    Board2DTO board2DTO = new Board2DTO();

    board2DTO.setId(board2Entity.getId());
    board2DTO.setBoardWriter(board2Entity.getBoardWriter());
    board2DTO.setBoardPass(board2Entity.getBoardPass());
    board2DTO.setBoardTitle(board2Entity.getBoardTitle());
    board2DTO.setBoardContents(board2Entity.getBoardContents());
    board2DTO.setBoardHits(board2Entity.getBoardHits());
    board2DTO.setBoardCreatedTime(board2Entity.getCreatedTime());
    board2DTO.setBoardUpdatedTime(board2Entity.getUpdatedTime());

    return board2DTO;
}

 

board2list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>list</title>
</head>
<body>
<table>
    <tr>
        <th>id</th>
        <th>title</th>
        <th>title(||쓰지 않은 경우)</th>
        <th>writer</th>
        <th>date</th>
        <th>hits</th>
    </tr>
    <tr th:each="board: ${boardList}">
        <td th:text="${board.id}"></td>
        <td><a th:href="@{|/board2/${board.id}|}" th:text="${board.boardTitle}"></a></td>
        <td><a th:href="@{/board2/${board.id}}" th:text="${board.boardTitle}"></a></td>
        <td th:text="${board.boardWriter}"></td>
        <td th:text="*{#temporals.format(board.boardCreatedTime, 'yyyy-MM-dd HH:mm:ss')}"></td>
        <td th:text="${board.boardHits}"></td>
    </tr>
</table>
</body>
</html>

'강의 따라하기 > member2' 카테고리의 다른 글

게시글 수정  (1) 2023.12.29
게시글 조회(상세조회,view page)  (0) 2023.12.29
게시글 작성 완료  (0) 2023.12.29
게시판 프로젝트  (1) 2023.12.29
12_ajax 이메일 중복체크_중복체크 구현 완료  (1) 2023.12.28

블로그의 정보

Hello 춘기's world

볼빵빵오춘기

활동하기