Spring/SpringBoot/Study

[SpringBoot] Welcome Page 생성, Thymeleaf 적용

이나피스 2023. 3. 27. 15:01
반응형

✶ 본 포스팅은 인프런 김영한 강사님의 스프링입문으로 공부한 내용을 기반으로 작성하였습니다.

 

Welcome Page 만들기

 

resources - static 폴더에 index.html 파일을 생성한다.

스프링부트에서 자동으로 index.html을 찾아서 메인페이지로 보여준다.

간단한 html문을 작성 후 localhost:8080에서 확인 가능하다.

 

Thymeleaf

 

자바 라이브러리 중 하나로 HTML, XML, Javascript, CSS, 텍스트를 생성할 수 있는 템플릿 엔진이다.

View계층에 적합하며, JSP로 만든 기능을 완전히 대체할 수 있다.

 

Thymeleaf 공식 사이트

https://www.thymeleaf.org/
스프링 공식 튜토리얼

https://spring.io/guides/gs/serving-web-content/
스프링부트 메뉴얼

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-template-engines

 

Thymeleaf를 이용한 view

 

resources - templates 폴더에 hello.html 파일을 생성했다.

  • Thymeleaf 문법을 사용하기 위해 선언
<html xmlns:th="http://www.thymeleaf.org">
  • 문법
표현 설명 예제
@{ ... } URL 링크 th:href="@{/css/bootstrap.min.css}"
th:href="@{/{itemId}/edit(itemId=${item.id})}"
| ... | 리터럴 값 대체
*리터럴 값이란? 변하지 않는 값으로
  예를들어 int a = 1 을 선언했을 때 1이 바로 리터럴 값이다
th:text="|Hi ${user.name}!|"
(= th:text="'Hi '+${user.name}+'!'"
${ ... } 변수 th:text=${user.name}
th:each 반복 출력 <tr th:each="item: ${items}">
  <td th:text="${item.price}">100</td>
</tr>
*{ ... } 선택 변수 <tr th:object="${items}">
  <td th:text="*{price}">100</td>
</tr>
#{ ... } 메시지. properties 같은 외부 자원에서 코드에 해당하는 문자열 get. th:text="#{member.register}"

 

 

 

 참고 사이트

https://github.com/ihoneymon/spring-boot-orm-learn/blob/master/THYMELEAF_TEMPLATE_ENGINE.md

https://yeonyeon.tistory.com/153

https://maenco.tistory.com/entry/Thymeleaf-Literal-%EB%A6%AC%ED%84%B0%EB%9F%B4

https://jongminlee0.github.io/2020/03/12/thymeleaf/

반응형