[Java 게시판 만들기]-조회 기능

김예진·2022년 10월 17일
0

java

목록 보기
2/21

쓴 글들을 조회할 수 있는 페이지를 만들어 보았다

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

<a href="/articles/new">New Article</a>
{{>Layouts/footer}}

Controller(TestController.java)

//뷰 페이지
    @GetMapping("/articles")
    private String index(Model model) {
        //모든 test을 가져온다
        List<Test> testEntityList = (List<Test>) testRepository.findAll();

        //가져온 test 묶음을 뷰로 전달
        model.addAttribute("articleList",testEntityList);

        //뷰 페이지를 설정
        return "articles/index";
    }

Model을 사용하면 view에 데이터를 넘겨줄 수 있다

  1. Model 타입의 model 변수를 선언

  1. model.addAttribute("key","value")로 데이터를 전달

  2. {{#key}}{{/key}}로 범위 설정, 사용(mustache문법)

설정해둔 범위 밖에서 데이터를 사용하고 싶다면?

요렇게 사용하면 된다!

0개의 댓글