Checkpoint | 1장을 시작하기 전에 준비할 것:
실습환경 구축 글 👉 https://velog.io/@ttt-1-2/실습-환경-설치
스프링 부트(Spring Boot)는 스프링을 개선하여 자바 애플리케이션 개발을 단순화한 프레임워크다

localhost:8080 접속한다src > main > resources > static 디렉터리에서 hello.html 파일 생성
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>헬로 월드!</h1>
</body>
</html>
Step 4: Rerun firstproject
localhost:8080/hello.html로 접속하면 ‘헬로 월드!’가 화면에 표시된 것을 확인할 수 있다.

클라이언트 ↔ 서버:
만약 서버가 중지되면 웹사이트에 접속할 수 없고 다시 실행(Rerun)하면 정상적으로 접근할 수 있다. 실행 로그의 메시지를 살펴보면:
Tomcat started on port(s): 8080 (http) with context path ''
//--> 톰캣 서버가 8080번 포트에서 구동 중임을 의미한다
/static, /public, /resources, /META-INF/resources 경로에서 해당 파일을 순서대로 탐색한다. 파일이 존재하면 반환하고 없으면 404 에러를 응답한다. //MVC 패턴
[ Client ]
|
v
[ Controller ] -----> [ Model ] -----> [ Database / Data ]
| |
v |
[ View ] <-----------------
|
v
[ Client Screen ]
[ View: greetings.mustache ]
↓
( {{username}} )
[ Controller: FirstController.java ]
↓
@GetMapping("/hi")
public String niceToMeetYou(Model model) {
model.addAttribute("username", "홍팍");
return "greetings";
}
[ Model: 데이터 ]
↓
model.addAttribute("username", "홍팍");
[ Client 화면 출력 ]
↓
<h1>Trang님, 반갑습니다!</h1>
/hi페이지의 실행 흐름
@Controller // 1. 컨트롤러 선언
public class FirstController {
@GetMapping("/hi") // 2. URL 요청
public String niceToMeetYou(Model model) { // 3. 메소드 수행 + 4. 모델 객체 가져오기
model.addAttribute("username", "hongpark"); // 5. 모델 변수 등록
return "greetings"; // 6. 뷰 템플렛 페이지 반환
}
}
💡 실습 /hi 페이지에 헤더-푸터 레이아웃 적용하기
Bootstrap은 HTML, CSS, JavaScript가 미리 준비되어 있는 프레임워크다. Bootstrap 공식 사이트에 접속해서 필요한 코드와 컴포넌트를 가져올 것이다.

이번 실습에서는 템플릿을 헤더와 푸터로 나누어 재사용할 수 있도록 구성해 코드의 가독성과 유지보수성을 높인다
폴더 구조:
main
├── java
│ └── com.example.firstproject
│ ├── FirstprojectApplication.java
│ └── controller
│ └── FirstController.java
│
└── resources
├── static
├── templates
│ ├── greetings.mustache
│ ├── goodbye.mustache
│ └── layouts
│ ├── header.mustache
│ └── footer.mustache
│
└── application.properties
{{username}}을 출력하는 템플릿:{{>layouts/header}}
<!-- content -->
<div class="bg-dark text-white p-5">
<h1>{{username}}님, 반갑습니다!</h1>
</div>
{{>layouts/footer}}