<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
// 만약 아래와 같이 thymeleaf를 추가해서 활용하려면 뷰 리졸버 설정같은 것 다 해주어야함
// [참고] https://offbyone.tistory.com/406
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
//html에 다음과 같이 추가하면 thymeleaf 명령어들 자동완성이용가능
<html lang="en" xmlns:th="http://www.thymeleaf.org">
Expression
변수 식 : ${OGNL} OGNL(Object-Graph Navigation Language)
OGNL : 객체의 속성의 값을 가져올 때 사용함
// Controller 단
@RequestMapping(value = "/customers", method = RequestMethod.GET)
public ModelAndView findCustomers(){
List<Customer> customerList = customerService.getAllCustomers();
return new ModelAndView("views/customers", Map.of("serverTime", LocalDateTime.now(), "customers", customerList));
}
//위의 Map 이 Context가 되어서 넘어가게 됨
// th:text 다음의 문자가 나오게 됨. 그리고 ---- 는 파싱이 실패했을 때 나오는 default string
<p th:text="'The time on the server is' + ${serverTime}"> ---- </p>
<p>Today is: <span th:text="${today}">13 february 2011</span>.</p>
==> ctx.getVariable("today");
<img th:src="@{/resources/Bootstrap_logo.svg}" class="img-fluid">
// 링크 식 안에 변수 넣는 방법
<td><a th:href="@{/vouchers/{id}(id=${voucher.voucherId})}" th:text="*{voucherId}"></a></td>
//import
implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity5")
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<div sec:authorize="isAuthenticated()">
This content is only shown to authenticated users.
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">
This content is only shown to administrators.
</div>
<div sec:authorize="hasRole('ROLE_USER')">
This content is only shown to users.
</div>
<div th:text="${#authentication.name}">
The value of the "name" property of the authentication object should appear here.
</div>
<div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>
<div sec:authentication="name">
The value of the "name" property of the authentication object should appear here.
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')">
This will only be displayed if authenticated user has role ROLE_ADMIN.
</div>