[스프링부트] 글 목록 보기 View

익명 블로그 V1 - 6
이나겸's avatar
Nov 17, 2024
[스프링부트] 글 목록 보기 View

[익명 게시글을 쓰는 블로그 만들기 6]


설정 - 플러그인 - 마켓플레이스 - Mustache 설치

notion image

src - main - resources - templates - list.mustache파일 생성

  • mustache 파일로 view 구성(웹 실행했을 때 보여지는 화면)
    • mustache 파일 = html 파일
  • 파일명 뒤에 파일 확장자를 .mustache로 직접 작성해야함
notion image

list.mustache파일 - 글 목록 보기 화면 view

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>blog</title> </head> <body> <nav> <!-- 헤더가 네비게이션 역할을 할거니까 --> <ul> <!-- 헤더 --> <li> <a href="#">홈</a> </li> <li> <a href="#">글쓰기</a> </li> </ul> </nav> <hr> <!-- 줄긋기 --> <section> <table border="1"> <!-- 표 --> <tr> <!-- 표의 헤더 --> <th>번호</th> <th>제목</th> </tr> <tr> <!-- 표의 바디 --> <td>5</td> <td>제목5</td> <td><a href="#">상세보기</a></td> </tr> <tr> <td>4</td> <td>제목4</td> <td><a href="#">상세보기</a></td> </tr> </table> </section> </body> </html>

BoardController 매핑

  • 매핑 없이는 templates 파일 안에 있는 파일들은 접근이 불가능, 접근하더라도 .mustache파일은 해석이 안됨
  • @GetMapping(”/”) : 글 목록 보기 화면을 get요청하고 /로 매핑
  • return “파일명” : 파일명만 적어놓으면 view resolver가 templates 엔진에서 .mustache 파일을 찾아내서 실행시킴
package com.example.blog.Board; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @RequiredArgsConstructor // final이 붙어있는 필드에 대한 생성자를 만들어줌 @Controller public class BoardController { private final BoardService boardService; @GetMapping("/") // get 요청, /로 매핑 public String list() { // 파일명만 적어놓으면 view resolver가 .mustache파일을 templates에서 찾아냄 return "list"; } }

한글 깨지지 않도록 application.properties 파일에 utf-8 인코딩

notion image

웹에서 실행

notion image
Share article

Nakyeom's Study