09009

[Spring] 정적 컨텐츠 본문

Back-End/Spring
[Spring] 정적 컨텐츠
09009

정적 컨텐츠

서버에서 추가로 수행하는 작업 없이 파일을 그대로 웹브라우저에 반환하는 것

 

C:/Users/springstudy/hello-spring/src/main/resources/static 경로에서 

"hello-static.html" 라는 파일을 생성한다.

 

static 폴더 안에 hello-static.html 파일을 추가하면 서버에 이 파일을 그대로 전달한다.

 

위 소스코드를 실행하고 url에 "localhost:8080/hello-static.html"로 접속한다.

 

정적 파일이 그대로 반환된다.

 

 

원리 설명

1. 웹 브라우저에 localhost:8080/hello-static.html 검색 -> 내장 톰켓 서버가 요청을 받는다.

2. 요청을 받은 내장 톰켓 서버가 스프링에 넘겨준다.

3. 스프링 컨테이너에서 hello-static 관련 컨트롤러가 있는지 찾아본다. (컨트롤러가 우선순위를 가진다.)

4. 스프링 부트가 내부에 있는 resources 안에 있는 hello-static.html을 찾는다. 있으면 웹 브라우저에 반환한다.

 


즉, 웹 브라우저에서 hello-static.html에 접근한 후,

스프링 컨테이너에서 우선 hello-static 관련 컨트롤러가 있는지를 탐색한다.

그 이후 컨트롤러가 존재하지 않을 시, resources의 static/hello-static.html을 찾아 반환해주는 구조로 되어있다.

 

 

 

출처 및 참고자료

 https://pomo0703.tistory.com/32

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8

인프런 김영한님 스프링입문 강의