-
[스프링부트/웹 애플리케이션 개발]view 환경 설정, thymeleaf 템플릿 엔진Spring&SpringBoot 2023. 1. 2. 16:32
thymeleaf 템플릿 엔진
1. HelloController
package jpabook.jpashop; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HelloController { @GetMapping("hello") public String hello(Model model) { model.addAttribute("data", "Hello!"); return "hello"; // templates > hello.html 이동 } }
- 스프링 부트 thymeleaf viewName 매핑
> resources:templates/ + {ViewName} + .html
2. templates > hello.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p> </body> </html>
- 서버 randering 없으면 '안녕하세요. 손님' 출력
- 서버 randering 하면 '안녕하세요. Hello!' 출력
3. resources/templates/hello.html
정적페이지, 순수한 html
1. static/index.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hello</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> Hello <a href="/hello">hello</a> </body> </html>
2. http://localhost:8080/
728x90'Spring&SpringBoot' 카테고리의 다른 글
[스프링부트/웹 애플리케이션 개발]H2 데이터베이스 설치, JPA와 DB 설정, 동작확인 (1) 2023.01.03 [스프링부트/웹 애플리케이션 개발]devtools setting, spring-boot-devtools 실행 안 될 때 (0) 2023.01.02 [스프링부트/웹 애플리케이션 개발]라이브러리 (1) 2023.01.02 [스프링부트/인텔리제이]window 단축키 (1) 2023.01.02 [스프링부트/웹 애플리케이션 개발]스프링부트스타터 프로젝트 생성, 스프링부트버전확인, lombok (0) 2023.01.02