본문 바로가기

개발환경

spring boot: UI template 경로 지정 오류

728x90
반응형

에러내용 : TemplateInputException: Error resolving template [/files/files], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause

지금 윈도우에서 spring boot 를 개발하고 있어. 그런데 실제로 배포하는 OS 는 리눅스(우분투)야. OS 의 차이로 인해서 발생되는 시행착오 중에 UI template 파일(thymeleaf)의 경로 지정과 관련된 내용이야.

에러 상황

일단 윈도우 OS 에서 개발한 내용을 살펴볼께.

페이지에서 업로드된 파일의 목록을 보여주기 위해서 아래와 같이 컨트롤러 클래스를 작성했어. 클래스 이름이 FileUploadController 인 이유는 크게 중요하지 않지만, 해당 페이지에서 파일을 업로드하는 기능도 함께 수행하고 있기 때문에 그렇게 작명한거야.

@Controller
@Slf4j
@RequestMapping("/files")
public class FileUploadController {
    private final FileUploadService service;

    @Autowired
    public FileUploadController(FileUploadService service) {
        this.service = service;
    }

    @GetMapping("")
    public String getFiles(Model model) {
        List<FileUploadResponse> files = service.getFiles();
        model.addAttribute("files", files);
        return "/files/files";
    }
}

UI template 인 files.html 파일의 경로는 아래와 같아.

마찬가지로 files.html 의 내용 따윈 여기에서 중요하지 않아.

/files 로 페이지를 호출하면 데이터를 얻어서 UI template 으로 /files/files.html 파일을 호출하도록 설정했다는게 중요한거지.

윈도우 OS 에서 테스트를 한 결과는 아래처럼 정상적으로 표시가 되지.

그런데 이 개발 소스를 컴파일해서 리눅스 웹 서버로 올린 다음에 실행하면 아래와 같이 UI template 파일인 /files/files.html 을 못 찾는다고 에러가 나.

로그에는 어떤 에러가 찍혀있는지를 한번 볼께.

DEBUG 24-02-18 03:32:40[http-nio-8080-exec-3] [DispatcherServlet:1109] - Failed to complete request: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/files/files], template might not exist or might not be accessible by any of the configured Template Resolvers
ERROR 24-02-18 03:32:40[http-nio-8080-exec-3] [[dispatcherServlet]:175] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/files/files], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/files/files], 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)
...

UI template 파일의 최상위 경로는 templates 이고 이 하위로 files 라는 디렉토리 아래에 files.html 이라는 파일을 만들었는데, /files/files 가 윈도우에서는 제대로 매핑이 되고, 리눅스에서는 매핑이 제대로 안되는거야.

해결방법

그래서 아래와 같이 수정해서 해결했어.

@Controller
@Slf4j
@RequestMapping("/files")
public class FileUploadController {
    private final FileUploadService service;

    @Autowired
    public FileUploadController(FileUploadService service) {
        this.service = service;
    }

    @GetMapping("")
    public String getFiles(Model model) {
        List<FileUploadResponse> files = service.getFiles();
        model.addAttribute("files", files);
        return "files/files";
    }
}

return 값에서 최상위 루트 경로 문자 / 를 제거했지.

(before: ERROR)
return "/files/files";
(after: SUCCESS)
return "files/files";

수정한 결과 아래처럼 정상적으로 UI 템플릿 파일이 매핑된 것을 확인할 수 있어.

윈도우 OS 에서도 물론 정상적으로 매핑되지.

 

반응형