트러블 슈팅: Thymeleaf 템플릿 오류 해결

song yuheon·2023년 9월 25일
0

Trouble Shooting

목록 보기
20/57
post-thumbnail

문제 상황


Thymeleaf 템플릿 엔진에서 return "/"; 사용 시 발생하는 오류.


오류 메시지

Error resolving template [/], template might not exist or might not be accessible by any of the configured Template Resolvers

이 메시지는 "/" 라는 템플릿을 찾을 수 없거나 설정된 템플릿 리졸버(Template Resolvers)로 접근할 수 없다는 것을 의미한다


원인


처음에 return "/";를 사용한 이유는 "/" 경로를 매핑하여 /user/login으로 리다이렉트 해주려는 의도였다.

컨트롤러에서 return "/";를 사용할 경우 Thymeleaf는 src/main/resources/templates/.html 또는 src/main/resources/templates/index.html를 찾게 된다.
그러나 해당 경로가 표준적인 템플릿 경로가 아니므로 오류가 발생한다.


해결 방법


  1. 템플릿 이름 변경

컨트롤러에서 return "/"; 대신 return "index"; 또는 return "home"; 등의 유효한 템플릿 이름을 사용한다.
그리고 해당 이름과 일치하는 템플릿 파일을 src/main/resources/templates/ 경로에 위치시킨다.

  1. 템플릿 위치 확인
    만약 index.html 또는 home.html과 같은 템플릿이 src/main/resources/templates/ 경로에 이미 있다면 해당 템플릿의 이름을 컨트롤러의 리턴 값과 일치시키도록 변경한다.

결과


return "/user/login";으로 수정하여 문제를 해결했다.
이로써 Thymeleaf는 올바른 템플릿 파일을 찾아 정상적으로 처리하게 되었다.


profile
backend_Devloper

0개의 댓글