728x90

브라우저 요청 받기

모델과 데이터 등록 - addAttribute()

@GetMapping("/articles")
public String index(Model model){
    // 1. 모든 Article을 가져온다
    List<Article> articleEntityList = articleRepository.findAll();

    // 2. 가져온 Article 묶음을 뷰로 전달!
    model.addAttribute("articleList", articleEntityList);

    // 3. 뷰페이지를 설정!
    return "articles/index"; //articles/index.mustache
}

라파지터리 오버라이딩 - findAll()

public interface ArticleRepository extends CrudRepository<Article,Long> {
// CrudRepository<관리대상 엔티티: Article, 대표값의 타입(id) : Long>
    @Override
    ArrayList<Article> findAll();
}

뷰 페이지 연결 및 작성

{{>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>{{title}}</td>
            <td>{{content}}</td>
        </tr>
    {{/articleList}}
    </tbody>
</table>

{{>layouts/footer}}
728x90
복사했습니다!