
.png)
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf’ 해당 dependency를 주석 처리해야 테스트가 가능thymeleaf 는 동적 페이지 처리를 위한 템플릿 엔진으로 추가하면 자동으로 Controller에서 html 파일 찾는 경로를/resources/templates 로 설정"redirect:/hello.html" redirect 요청을 문자열로 반환하면 http://localhost:8080/hello.html ****요청이 재 수행되면서 static 폴더의 파일을 반환 
Template engine 에 View 전달
🌐 http://localhost:8080**/html/templates**타임리프 default 설정
/resources/templates/hello.html

.html은 생략가능!)View 정보
"hello-visit" → resources/templates/hello-visit.html
<div>
(방문자 수: <span th:text="${**visits**}"></span>)
</div>
Model 정보
- visits: 방문 횟수 (visitCount)
- 예) 방문 횟수: 1,000,000 번

// controller
package com.sparta.springmvc.html;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HtmlController {
private static long visitCount = 0;
@GetMapping("/static/hello")
public String hello(){
return "hello.html"; // templates파일 안에 있어야만 호출할수있다
}
@GetMapping("/html/redirect")
public String htmlStatic(){
return "redirect:/hello.html"; // 직접접근하는 경로를 redirect다음에 작성
// state code가 302가 나온다
// location쪽은 hello.html을 직접 url에 작성했을때랑 같은게 나온다
// redirect를 통해 한번더 직접 방문
}
@GetMapping("/html/templates")
public String htmlTemplates(){
return "hello"; // 확장자 생략이 가능
// thymeleaf가 자동으로 확장자를 생략가능해도 찾을 수 있게한다
}
@GetMapping("html/dynamic")
public String htmlDynamic(Model model){// springframe work에있는 Model
visitCount ++;
model.addAttribute("visits",visitCount);
return "hello-visit";
}
}
<!DOCTYPE html>
<html>
<head><meta charset="UTF-8"><title>Hello Spring</title></head>
<body>
<div>
Hello, Spring 동적 웹 페이지!!
</div>
<div>
(방문자 수: <span th:text="${visits}"></span>)
<!-- model의 이름이 visits인 값을 가져옴 -->
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Spring</title>
</head>
<body>
Hello, Spring 정적 웹 페이지!! (templates)
</body>
</html>