HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🤩
개발
/
Spring
Spring
/
Thymeleaf
Thymeleaf
Thymeleaf

Thymeleaf

[참고] https://www.baeldung.com/thymeleaf-in-spring-mvc
  • 템플릿 엔진. jsp(컴파일)와 달리 템플릿을 파싱
<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");
  • th:text안에서 문자열 합치기 : <div th:text="|Hello, ${name}!!|"></div>

링크 식 : @{링크}

[참고] https://www.thymeleaf.org/doc/articles/standardurlsyntax.html
<img th:src="@{/resources/Bootstrap_logo.svg}" class="img-fluid"> // 링크 식 안에 변수 넣는 방법 <td><a th:href="@{/vouchers/{id}(id=${voucher.voucherId})}" th:text="*{voucherId}"></a></td>
  • 링크 식 안에 변수 넣는 방법
  • th:action을 이용해서 @{링크} 만 해주어도 csrf 자동으로 넣어줌
 

선택 변수 식 : *{OGNL}

<tr th:each="customer : ${customers}" th:object="${customer}"> <td th:text="${customer.customerId}"></td> <td th:text="*{name}"></td> <td th:text="*{email}"></td> <td th:text="*{lastLoginAt}"></td> <td th:text="*{createdAt}"></td> </tr>
  • th:object로 지정해준 변수에 대해 *로 해당 attribute에 바로 접근이 가능함
 

SpringSecurity Extension

  • 아래와 같은 네임스페이스 추가가 필요함
<html xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
//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>