Model : data
View : screen
Controller : logic
src > main > resource > templates > mustache 파일 생성
src > main > java > 기본 package > controller package 생성 > controller java class 생성
: view template을 만들어주는 도구. view template engine 중 하나.
mustache를 확장자로 인식하지 못할 경우 plugin 설치
help > find action > mustache 검색 > handlebars/mustache 설치
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class FirstController {
@GetMapping("/hi")
public String hello(Model model){
model.addAttribute("username", "Park");
return "greetings"; // templates/greetings.mustache 를 찾아서 브라우저에 전송
}
}
<!-- doc치고 tab치면 html 기본 템플릿 코드 생성 -->
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>hello, Mr.{{username}} !</h1>
</body>
</html>
controller : client 로부터 요청받음
view : 페이지를 만들어주고
model : 최종 페이지에 쓰일 데이터들을 view에 전달
[뷰 템플릿이 출력되는 과정]
1. controller가 브라우저로부터 request 받음
2. 적절한 어노테이션이 붙은 메소드 실행(ex> @GetMapping)
3. view template에서 사용할 데이터들을 model 오브젝트로 생성
4. model을 view template에 넘겨주고 화면 출력.
build.gradle > id 'org.springframework.boot' version '2.7.0'을 '2.6.8'로 변경 -> 우측에 있는 코끼리 모양 아이콘 클릭