AWS

AWS EC2 환경에서 thymeleaf 오류 수정 (Error resolving template)

심나라 2023. 9. 5. 11:03
728x90


SpringBoot, thymeleaf 로 페이지를 만들어서 AWS EC2 (linux) 에 배포한 후에 페이지를 호출하면 아래와 같이 thymleaf를 읽을 수 없다는 에러가 발생합니다.

에러메세지

Exception processing template "admin/index": Error resolving template [/admin/layout/header], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "admin/layout/layout" - line 206, col 6)

 

해결방법
1. Template 경로 바꾸기
application.yml 파일에 template 경로를 추가 합니다.

spring:
  thymeleaf:
    prefix: classpath:/templates/


2. Controller 반환값 수정
Controller의 반환값 맨 앞에 "/"가 들어가 있으면 안됩니다. (잘못된 예 => return "/admin/index";)

@Slf4j
@Data
@Controller
public class AdminMainController {

    @GetMapping("/admin")
    public String adminRoot(Model model) {
        model.addAttribute("menuActive", "main");
        return "admin/index";
    }

}


3. thymeleaf 경로변경

thymeleaf에서 fragment, replace를 사용시에 th:replace 경로 맨 앞 "/"를 넣으면 안됩니다.
<기존 소스>
layout.html
<!-- 내비게시션바(상단메뉴) Start -->
<nav th:replace="/admin/layout/header :: headerFragment"></nav>
<!-- 내비게시션바(상단메뉴) End -->

header.html
<header th:fragment="headerFragment"></header>

<변경 소스>
layout.html
<!-- 내비게시션바(상단메뉴) Start -->
<nav th:replace="admin/layout/header :: headerFragment"></nav>
<!-- 내비게시션바(상단메뉴) End -->

header.html
<header th:fragment="headerFragment"></header>

728x90