🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

코프링이 static 리소스를 못찾는것 같아요.

24.05.22 23:55 작성 24.05.22 23:58 수정 조회수 21

0

package com.devques.phoca_chips

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

@SpringBootApplication
public class PhotoCardApplication {
}

fun main(args: Array<String>) {
    runApplication<PhotoCardApplication>(*args)
}

// Application.kt 메인 파일
package com.devques.phoca_chips.test

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping

@Controller
class IndexController {

    @GetMapping("/")
    fun index(model: Model): String {
        return "/index.html"
    }
}

// test.kt 테스트 파일

스크린샷 2024-05-22 234802.png

2024-05-22T23:48:47.176+09:00 ERROR 10856 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "/index.html": Error resolving template [/index.html], template might not exist or might not be accessible by any of the configured Template Resolvers

 

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/index.html], template might not exist or might not be accessible by any of the configured Template Resolvers

 

2024-05-22T23:48:47.184+09:00 ERROR 10856 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/index.html], 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 [/index.html], template might not exist or might not be accessible by any of the configured Template Resolvers

 

파일 구조 코드 에러입니다.

템플릿 폴더에 html, css 파일을 넣었을때는 정상적으로 페이지가 작동했는데 static 폴더로 옮긴 이후에

위와 같이 오류가 나오면서 파일을 찾지 못하는것 같습니다..

비슷한 오류가 템플릿 폴더에서 나왔을땐 spring-boot-starter-thymeleaf 를 사용해서 해결했었는데

왜 static 폴더 안에 있는 파일들은 인식하지 못하는건지 궁금합니다.

스태틱과 템플릿 폴더가 서로 다른 정적,동적 콘텐츠를 다루는건 알고 있지만 왜 결과가 다르게 나오는지 모르겟습니다.

추가로 "/index.html" <-- 이 주소를 index, index.html, static/index.html, /static/index.html

등등 여러가지로 변경해봐도 달라지는건 없었습니다.


검색해봐도 비슷한 사례가 없는것같아 질문합니다.

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.05.24

안녕하세요, 인프런 AI 인턴입니다.

님의 질문에 대한 답변은 다음과 같습니다.

@GetMapping("/") 에서 return "/index.html" 구문을 사용하시면서 발생하는 문제 같습니다. Spring Boot에서 정적 컨텐츠(예: HTML, CSS, JavaScript 파일 등)를 제공하는 기본 위치는 /static, /public, /resources, /META-INF/resources 디렉터리입니다. 여기서 중요한 점은 Thymeleaf 같은 템플릿 엔진을 사용할 때는 기본적으로 /templates 디렉터리 내의 파일들을 반환하기 위해 구성되어 있습니다. 즉, 님이 @Controller를 이용해 직접 경로를 지정할 때는 Thymeleaf가 처리하는 /templates 경로 내의 파일들을 반환하려 시도하기 때문에, /static 디렉터리 내의 파일들을 찾지 못하는 것입니다.

정적 컨텐츠를 제공하고자 할 때는 @Controller를 사용하여 경로와 매핑된 메서드에서 파일명을 반환하는 것이 아니라, Spring Boot의 정적 리소스 핸들링 기능을 활용해야 합니다. 이렇게 하려면 해당 파일들을 /static, /public, /resources, /META-INF/resources 중 하나의 디렉터리에 위치시키고, 별도의 경로 지정 없이 파일명으로 접근할 수 있습니다.

질문 내용에서는 /static 디렉터리 내에 index.html 파일을 올바로 위치시킨 것 같으니, 특별한 경로 매핑 없이 브라우저에서 http://localhost:8080/index.html 혹은 http://localhost:8080/ 으로 접근했을 때 자동으로 해당 파일이 제공되어야 합니다.

@Controller@GetMapping("/")을 사용하여 동적 컨텐츠를 처리할 필요가 있을 경우에는 /templates 디렉터리 내에 있는 뷰 템플릿(예: Thymeleaf 템플릿)을 반환해야 하며, 정적 파일 접근 방식을 변경하고자 한다면 별도의 설정이 필요할 수 있습니다.

이러한 상황에서 여러분이 /static에 위치한 index.html 파일을 찾지 못하는 문제에 직면했다면, 먼저 해당 파일이 올바른 위치(/static, /public, /resources, /META-INF/resources 중 하나)에 있는지 확인하시기 바랍니다. 또한, application.properties 또는 application.yml 파일 설정을 통해 정적 리소스 경로를 변경한 경우, 해당 설정이 올바른지도 검토해 보시기 바랍니다.

참고하시면 도움이 될 정보입니다:
- Spring Boot에서는 정적 컨텐츠를 자동으로 제공하는 기능이 내장되어 있으므로, 정적 리소스에 접근하기 위해 별도의 컨트롤러 메서드에서 경로를 반환할 필요가 없습니다. 단순히 정적 리소스들을 적절한 디렉터리에 배치하기만 하면 됩니다.

더 궁금한 사항이 있으시면 추가 질문해 주세요.

채널톡 아이콘