-
#Spring boot-3 View 환경설정SPRING-BOOT 2021. 2. 26. 17:29
[출처] 인프런 김영한 강사님 -실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
View 환경 설정
thymeleaf 템플릿 엔진
thymeleaf 공식 사이트
Thymeleaf
Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati
www.thymeleaf.org
스프링 공식 튜토리얼
: https://spring.io/guides/gs/serving-web-content/
Serving Web Content with Spring MVC
this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team
spring.io
스프링부트 메뉴얼
29. Developing Web Applications
The Spring Web MVC framework (often referred to as simply “Spring MVC”) is a rich “model view controller” web framework. Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP requests. Methods in your contr
docs.spring.io
· 스프링 부트 thymeleaf viewName
· 매핑 resources:templates/ +{ViewName}+ .html
HelloController class
@Controller public class HelloController { @GetMapping("hello") public String hello(Model model) { model.addAttribute("data", "hello!!"); return "hello"; } }
: resources/templates/hello.html - thymeleaf 템플릿엔진 동작 확인(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>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p> 하게되면 서버사이즈 랜더링을 하여 화면에 표출한다.
localhost:8080/hello 페이지 소스 보기 Controller에 hello이름만 했을 뿐인데 어떻게 resource에 templates 밑에 hello.html을 찾을까?
이게 스프링부트의 thymeleaf가 기본적으로 설정을 걸면서 thymeleaf에 대한것은 설정으 걸어버린다.
resources:templates/ +{ViewName}+ .html
만약 완전 정적인 페이지 랜더링을 안하고 순수한 html을 뿌리고 싶을 경우
(static 하위에 index.html 생성)
· index.html 하나 만들기
· 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>
참고: spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다.
인텔리J 컴파일 방법: 메뉴 build -> Recompile
· devtools
참고 -> resource 하위에있는 파일을 수정했을 경우 기본적으로 서버를 다시 띄워야 한다.
이걸 해결하기 위한 방법 ->spring boot devtools
캐시 이런걸 다 없애서 리로딩하는 것을 도와준다.
기존 devtools 설치가 되면 restartedMain으로 바뀐다. 수정한 파일에서 build하위에 Recompile만 눌러주면된다.
'SPRING-BOOT' 카테고리의 다른 글
#Spring boot-6 도메인 분석 설계 (0) 2021.03.01 #Spring boot-5 JPA와 DB 설정, 동작확인 (0) 2021.03.01 #Spring boot-4 H2 데이터베이스 설치 (0) 2021.02.26 #Spring boot-2 라이브러리 살펴보기 (0) 2021.02.26 우아한테크세미나 우아한 스프링 부트 -백기선 선생님 (0) 2021.02.22