본문 바로가기
Java/Spring Boot

Thymeleaf 경로 설정하기

by amungstudy 2023. 11. 30.

thymeleaf에서 static에 있는 정적 리소스를 접근하려면 

 

<script src="assets/js/contact-form.js"></script>

 

이런식으로 바로 접근이 가능하다(현재 프로젝트에는 static/assets/js/js파일 구조 이다.)

 

하지만 templates의 도메인별 폴더를 만들어서 thymeleaf-layout-dialect를 적용했더니 경로가 안맞아서 파일이 다 깨졌다... thymeleaf 공부안하고 무작정 하니 이런 문제가 발생하는구나.

 


thymleaf에도 jsp처럼 Context Path 사용이 가능하다.

@{/} 표현식으로 상대경로 문제를 처리해줄 수 있다. 

 

<script th:src="@{/assets/js/wow.in.js}"></script>
<img th:src="@{/images/example.jpg}" alt="Example Image"/>
<link rel="stylesheet" th:href="@{/assets/css/style.css}" />
<li><a href="userprofile.html" th:href="@{/userprofile}">See User Profile</a></li>

 

변수 표현식도 중첩 사용가능하다. 여러개의 매개변수도 쉼표로 구분해서 사용 가능하다.

@{/order/process(execId=${execId},execType='FAST')}
@{/order/{orderId}/details(orderId=${orderId})}

 

공식문서참고

https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html

'Java > Spring Boot' 카테고리의 다른 글

Thymeleaf 기본  (0) 2023.12.04
Junit & AssertJ  (0) 2023.12.02
spring security에서 @PreAuthorize가 안먹힐때 (SpringBoot 3.0 이상)  (0) 2023.11.27
쿼리 파라미터찍기  (0) 2023.11.08
실무에서 모니터링 사용하기  (0) 2023.09.27