[22/02/15]SpringBoot templates 경로 설정 (classpath)
가물가물한 경로 복습
- '/' -> 가장 최상의 디렉토리로 이동한다.(Web root)
- './' -> 파일이 현재 디렉토리를 의미한다.
- '../' -> 상위 디렉토리로 이동한다.
출처: https://88240.tistory.com/122 [shaking blog]
templates 경로설정
스프링 부트에 web 의존성을 추가하고 localhost:80XX 로 접근하면
기본적으로 resources 폴더에 있는 static 에 위치한 index.html 파일을 읽게된다.
예를 들어, resources > static > assets > img > slide_3.jpg 를 가져올 때 src는 이렇게 작성하면 불러올 수 있다.
<img src="/assets/img/slide_3.jpg" alt="" width="40" height="40" class="rounded-circle"></a>
편하기는 하지만 나는 resources > templates 에 있는 html을 가져오고 싶었다. 왜냐면 모든 페이지에 들어갈 nav를 분리하고 싶었기 때문이다. (너무 코드가 길어지기 때문에 필수라고 생각한다!)
구글로 검색을 해보다가 괜찮은 해결방법이 있어 시도 했더니 되었지만, 이상하게도 타임리프가 먹지 않았다.
- config 폴더를 생성하여 MvcConfiguration 클래스를 생성한다.
package com.phl.cocolo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry){
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/templates/", "classpath:/static/");
}
}
- WebMvcConfigurer를 상속 받은 후 위와 같은 코드로 재정의 한다.
<div class="nav_html"></div>
<script type="text/javascript">
$(document).ready(function(){
$(".nav_html").load("/base/nav.html")
});
</script>
-
나는 resources > templates > base > nav.html 을 가져왔다.
-
정말 잘 가져와 진다. 하지만 타임리프로 조건문을 쓴 구문은 왜인지 적용이 되지 않는다. 아무래도 잘 모르겠어서 선생님께 물어봐야겠다.
참고 : https://bottom-to-top.tistory.com/38
Author And Source
이 문제에 관하여([22/02/15]SpringBoot templates 경로 설정 (classpath)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hellocdpa/경로저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)