Spring framework/Spring boot

TemplateInputException An error happened during template parsing(타임리프 오류)

마샤와 곰 2022. 7. 22. 17:13

 

개발도구로 잘 돌아가던 프로젝트가 빌드한 이후에 아래와 같은 오류가 발생하였습니다.

없다..고?

해당 이유로는 타임리프(thymeleaf)가 스프링 부트 내부에서 어디로 갈지 길을 못찾는(?) 원인에서 발생한 문제 입니다.

이를 해결하기 위해서 2가지를 확인해야 합니다.

 

1. application.properties

프로퍼티에서 prefix 값으로 타임리프에게 바라볼 경로를 지정 해 주었는지 확인합니다.

spring.thymeleaf.prefix=classpath:templates/   ### <---- 여기!!!!
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.check-template-location=true

 

2. 레이아웃에 절대경로로 대입을 하였는지 확인합니다.

아래와 같은 파일이 있다고 가정하여 봅니다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8" />
    <title>hello</title>
</head>
<body>
    <!-- header -->
    <th:block th:replace="/layout/header :: headerFragment"></th:block>
    <!-- content -->
    <th:block layout:fragment="content"></th:block>
    <!-- footer -->
    <th:block th:replace="/layout/footer :: footerFragment"></th:block>
</body>
</html>

 

헤더와 푸터 부분에 절대경로로 표기가 되어 있습니다.

해당 값을 상대경로로 바꾸어 주도록 합니다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <meta charset="UTF-8" />
    <title>hello</title>
</head>
<body>
    <!-- <th:block th:replace="/layout/header :: headerFragment"></th:block>  -->
    <th:block th:replace="layout/header :: headerFragment"></th:block>

    <th:block layout:fragment="content"></th:block>

    <!-- <th:block th:replace="/layout/footer :: headerFragment"></th:block>  -->
    <th:block th:replace="layout/footer :: footerFragment"></th:block>
</body>
</html>

 

익숙치 않은 작업이라 오류가 많이나서 아쉬웠던 거 같습니다.

프로젝트를 export(build) 한 경우에 타임리프에서의 경로와 관련된 오류가 발생 한 다면 위 2가지를 살펴보면 됩니다.

궁금한점 또는 틀린부분은 언제든 연락주세요!👻

반응형