냐냐한 IT/냐냐한 Spring Boot

org.thymeleaf.exceptions.TemplateInputException 오류 해결

소소하냐 2023. 2. 6. 18:58

Spring Boot를 사용하고, Tymeleaf를 사용하여 화면을 구성하였습니다.

 

IDE에서 개발 및 실행할 때는 문제없이 동작하던 것이, 서버에 올리니 오류가 발생합니다. 

(빌드한 jar 파일을 로컬에서 실행해도 동일하게 오류가 발생합니다.) 

 

org.thymeleaf.exceptions.TemplateInputException

 

오류 내용의 일부입니다.

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [home], template might not exist or might not be accessible by any of the configured Template Resolvers
        at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.15.RELEASE.jar!/:3.0.15.RELEASE]

 

해석하면: 

[home] 템플릿 resolve 오류, 템플릿이 존재하지 않거나 Template Resolvers 구성이 액세스하지 못할 수 있습니다. (Error resolving template [home], template might not exist or might not be accessible by any of the configured Template Resolvers)

 

 

 

문제는, 해당 템플릿이 존재하고 IDE에서 실행시에는 정상 동작했다는 것입니다. 

 

org.thymeleaf.exceptions.TemplateInputException 오류 해결

 

확인 1. Controller에서 return "/home" 으로 호출한다면 오류 발생

슬래시(/)로 시작하면 오류가 발생했습니다. 제거해주세요. 

 

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home(){
        // return "/home"; <- org.thymeleaf.exceptions.TemplateInputException 발생!!
        return "home"; // 슬래시(/) 제거!
    }

}

 

확인 2. application.properties 설정 확인

file: 경로로 설정되어 있어서 오류 발생했습니다. classpath: 경로로 변경하니 오류가 해결되었습니다. 

* 추가(2023.02.07): 기본 설정을 사용한다면 spring.thymeleaf.prefix 부분은 제거하셔도 됩니다. 

# 기본 설정으로 사용한다면 제거해도 됨
# spring.thymeleaf.prefix=classpath:/templates/ 

# file: 경로로 설정되어 있어서 오류 발생, classpath: 로 경로 변경
# spring.thymeleaf.prefix=file:src/main/resources/templates/

 

* spring.thymeleaf.prefix=file:src/main/resources/templates/이 들어갔던 이유: 

html 파일 수정시, Live Reload될 수 있도록 설정한 부분입니다. 

이 부분이 빠지면 개발시 Live Reload가 되지 않습니다. 하지만 이 부분이 있으면 실서비스에는 문제가 생깁니다.

개발용, 실서비스 용 설정 분리가 필요한 부분입니다. 

 

 

스타일이 깨지는 오류 해결

위 문제를 해결하고 나니, 스타일이 깨집니다. (css 적용이 안 되는 문제) 

resources/static 내의 파일을 못 읽네요. 

 

application.properties 설정 확인

이것도 file: 경로로 설정되어 있어서 오류 발생했습니다. classpath: 경로로 변경하니 오류가 해결 되었습니다. 

* 추가(2023.02.07): 기본 설정을 사용한다면 spring.web.resources.static-locations 부분은 제거하셔도 됩니다. 

# 기본 설정으로 사용한다면 제거해도 됨
# spring.web.resources.static-locations=classpath:/static/

# file: 경로로 설정되어 있어서 오류 발생, classpath: 로 경로 변경
# spring.web.resources.static-locations=file:src/main/resources/static/

*spring.web.resources.static-locations=file:src/main/resources/static/이 들어갔던 이유: 

css와 같은 resource 파일 수정시, Live Reload될 수 있도록 설정한 부분입니다.

이 부분이 빠지면 개발시 Live Reload가 되지 않습니다. 하지만 이 부분이 있으면 실서비스에는 문제가 생깁니다. 

개발용, 실서비스 용 설정 분리가 필요한 부분입니다. 

 

template parsing 오류 해결

이번엔, template parsing 오류가 발생했습니다. 

오류 내용은 아래와 같습니다. 

2023-02-06 17:22:57.684 ERROR 8864 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "home": An error happened during template parsing (template: "class path resource [templates/home.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/home.html]")
        at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.15.RELEASE.jar!/:3.0.15.RELEASE]

... 생략 ...

... 48 common frames omitted

Caused by: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/fragments/styles], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "layouts/layout" - line 8, col 15)
        at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.15.RELEASE.jar!/:3.0.15.RELEASE]

 

아래에 구체적인 오류 원인(Cause by)에서 template: "layouts/layout" - line 8, col 15 에서 오류가 발생했다고 친절히 알려줍니다. 

 

fragment 호출 경로를 확인하세요.

오류 메세지가 알려준, 해당 파일을 확인하니 오류 난 부분은 아래와 같습니다. 

슬래시(/)가 포함되어 생긴 경로 오류입니다. 제거해 주세요.

<th:block th:replace="fragments/styles::commons"></th:block>

<!-- // 슬래시(/) 가 포함되어 생긴 경로 오류입니다. 제거해주세요.  -->
<!-- <th:block th:replace="/fragments/styles::commons"></th:block> -->

 

 

결론

  • application.properties 설정 확인: 개발 편의를 위한 Live Reload 용으로 설정한 내용이, 실서비스에도 포함되어 생긴 문제. 
아래 설정 추가는 개발 편의를 위한 Live Reload를 위한 설정으로, 실서비스에 포함되면 위와 같은 오류가 발생하니 주의가 필요합니다. 

spring.thymeleaf.prefix=file:src/main/resources/templates/ 
spring.web.resources.static-locations=file:src/main/resources/static/ 
  • thymeleaf 호출 경로에 슬래시(/) 포함 여부 확인

 

Thymeleaf를 적용하며, 오류를 다양하게도 발생시켰네요;;; 

 

끝까지 읽어주셔서 감사합니다.