๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

[์Šคํ”„๋ง ๋ถ€ํŠธ/MVC] ์ •์  ๋ฆฌ์†Œ์Šค(Static Resources) ๊ธฐ๋ณธ ์„ค์ •๊ณผ ์ปค์Šคํ…€ ๋ฐฉ๋ฒ•

by Leica 2020. 8. 19.
๋ฐ˜์‘ํ˜•

[์Šคํ”„๋ง ๋ถ€ํŠธ/MVC] ์ •์  ๋ฆฌ์†Œ์Šค(Static Resources) ๊ธฐ๋ณธ ์„ค์ •๊ณผ ์ปค์Šคํ…€ ๋ฐฉ๋ฒ•

์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ์ •์  ๋ฆฌ์†Œ์Šค(static resources)์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ๋งคํ•‘ ์„ค์ •๊ณผ ์ปค์Šคํ…€์„ ์ง€์›ํ•œ๋‹ค.

 

์‹ค์ œ๋กœ ํ™•์ธํ•ด๋ณด์ž.

์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ์˜ src/main/resources/static/์— hello.html์„ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
Hello Static Resource
</body>
</html>

 

์ •์  ๋ฆฌ์†Œ์Šค๋Š” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์žฌ๊ธฐ๋™ํ•˜์ง€ ์•Š๊ณ  ๋นŒ๋“œํ•˜๋Š” ๊ฒƒ ๋งŒ์œผ๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ํ›„ http://localhost:8080/hello.html๋กœ ์š”์ฒญํ•ด๋ณด์ž.

 

 

์ •์  ๋ฆฌ์†Œ์Šค ๋งคํ•‘ URL ํŒจํ„ด

๊ธฐ๋ณธ์ ์œผ๋กœ /**(๋ฃจํŠธ) ๋ถ€ํ„ฐ ๋งคํ•‘๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์œ„ ์˜ˆ์ฒ˜๋Ÿผ http://127.0.0.1:8080/hello.html์„ ์š”์ฒญํ•˜๋ฉด ์ •์  ๋ฆฌ์†Œ์Šค location(์œ„์น˜)์—์„œ ์ฐพ์•„ ์‘๋‹ตํ•œ๋‹ค.

 

์ •์  ๋ฆฌ์†Œ์Šค ๋งคํ•‘ ํŒจํ„ด์„ ๋ฃจํŠธ๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํŒจํ„ด์œผ๋กœ ๋ฐ”๊ฟ”๋ณด์ž.

 

์ •์  ๋ฆฌ์†Œ์Šค ๋งคํ•‘ ํŒจํ„ด ๋ณ€๊ฒฝ - spring.mvc.static-path-pattern

application.properties

spring.mvc.static-path-pattern=/static/**

 

 

์ฐธ๊ณ ๋กœ ์ด ํ”„๋กœํผํ‹ฐ๋Š” WebMvcProperties์— ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.

 

์ •์  ๋ฆฌ์†Œ์Šค Location(์œ„์น˜)

์ •์  ๋ฆฌ์†Œ์Šค location์ด๋ž€ ์„œ๋ฒ„๊ฐ€ ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ๊ธฐ๋ณธ ์ •์  ๋ฆฌ์†Œ์Šค ์œ„์น˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ด๋Š” ResourceProperties์— ์ •์˜๋˜์–ด ์žˆ์œผ๋ฉฐ, ์Šคํ”„๋ง ๋ถ€ํŠธ 1.5.3.RELEASE ๋ถ€ํ„ฐ ํ˜„์žฌ ๊ธฐ์ค€ ๊ฐ€์žฅ ์ตœ์‹  ๋ฒ„์ „์ธ 2.3.3.RELEASE ๊นŒ์ง€ ๋™์ผํ•˜๋‹ค.

 

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • "classpath:/static/"
  • "classpath:/public/"

 

์ •์  ๋ฆฌ์†Œ์Šค location์„ ๋ฐ”๊ฟ”๋ณด์ž.

 

์ •์  ๋ฆฌ์†Œ์Šค location ๋ณ€๊ฒฝ - spring.resources.static-locations

์ •์  ๋ฆฌ์†Œ์Šค location์„ /static-test/์œผ๋กœ ๋ณ€๊ฒฝํ•ด๋ณผ ๊ฒƒ์ด๋‹ค.

ํ”„๋กœ์ ํŠธ src/main/resources/์— static-test ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  hello.html์„ ์œ„์น˜์‹œํ‚จ๋‹ค.

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
static-test/hello.html
</body>
</html>

 

application.properties์— spring.resources.static-locations์„ ์ด์šฉํ•˜์—ฌ ์œ„์—์„œ ๋งŒ๋“  ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

spring.resources.static-locations=classpath:/static-test/

 

์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์žฌ๊ธฐ๋™ํ•˜๊ณ  http://localhost:8080/hello.html๋กœ ์š”์ฒญํ•œ๋‹ค.

 

 

spring.resources.static-locations ํ”„๋กœํผํ‹ฐ๋Š” ResourceProperties์— ์ •์˜๋˜์–ด ์žˆ๋‹ค.

 

์ด spring.resources.static-locations ํ”„๋กœํผํ‹ฐ๋ฅผ ํ†ตํ•ด location์„ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” locations๋ฅผ overrideํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ค์ • ํ›„์—๋Š” ๊ธฐ๋ณธ location์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ์˜ WebConfigurer๋ฅผ ํ†ตํ•œ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋ฐ˜์‘ํ˜•

 

์ •์  ๋ฆฌ์†Œ์Šค ์„ค์ • ์ปค์Šคํ„ฐ๋งˆ์ด์ง• - WebConfigurer

WebMvcConfigurer๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ํด๋ž˜์Šค์—์„œ addResourceHandlers๋ฅผ overrideํ•˜์—ฌ ์ •์  ๋ฆฌ์†Œ์Šค ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์ •์  ๋ฆฌ์†Œ์Šค ํ•ธ๋“ค๋Ÿฌ๋Š” ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ปค์Šคํ…€ ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.

 

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/m/**")
                .addResourceLocations("classpath:/m/")
                .setCachePeriod(20)
                ;
    }
}

/m/** ํŒจํ„ด ์š”์ฒญ ์‹œ classpath์˜ /m/ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์•„ ์‘๋‹ตํ•˜๋„๋ก ํ•˜๋Š” ์„ค์ •์ด๋‹ค.

setCachePeriod๋ฅผ ํ†ตํ•ด ์บ์‹ฑ ์ „๋žต์„ ์„ค์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. (์ดˆ ๋‹จ์œ„)

 

์„ค์ •ํ•œ ํ•ธ๋“ค๋Ÿฌ์— ๋Œ€ํ•ด ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด src/main/resources์— m ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ๋งŒ๋“ค๊ณ  hello.html์„ ์œ„์น˜์‹œํ‚จ๋‹ค.

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
m/hello.html
</body>
</html>

 

 

ResourceHttpRequestHandler

์ •์  ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์š”์ฒญ์˜ ์‘๋‹ต์€ ResourceHttpRequestHandler๊ฐ€ ์ฒ˜๋ฆฌํ•œ๋‹ค.

๋ธŒ๋ผ์šฐ์ €์—์„œ ์ด์ „๊ณผ ๋™์ผํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค์‹œ ์š”์ฒญํ•ด๋ณด์ž.

 

ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณด๋ฉด ์š”์ฒญ ํ—ค๋”์— If-Modified-Since๊ฐ€ ๋ณด๋‚ด์ง€๊ณ  ์‘๋‹ต ํ—ค๋”์— Last-Modified, ์‘๋‹ต ์ฝ”๋“œ๋กœ 304๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

304 ์‘๋‹ต ์ฝ”๋“œ๋Š” hello.html์„ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„์˜ค๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์บ์‹ฑํ•˜๊ณ  ์žˆ๋˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.

 

๋‹ค์‹œ hello.html์„ ์ˆ˜์ •ํ•˜๊ณ  ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•œ ๋’ค ํ…Œ์ŠคํŠธํ•ด๋ณด์ž.

์ด๋กœ ์ธํ•ด hello.html์˜ modified date๊ฐ€ ํ˜„์žฌ ์‹œ๊ฐ์œผ๋กœ ๊ฐฑ์‹ ๋˜๊ณ  http://127.0.0.1:8080/hello.html ์žฌ์š”์ฒญ ์‹œ HTTP ๋ฉ”์‹œ์ง€๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋€๋‹ค.

 

๋ธŒ๋ผ์šฐ์ €๋Š” ์ €์žฅํ•˜๊ณ  ์žˆ๋˜ hello.html ๋ฆฌ์†Œ์Šค์˜ modified date๋ฅผ If-Modified-Since์— ๋‹ด์•„ ๋ณด๋‚ด๊ณ  ์„œ๋ฒ„๋Š” hello.html์ด ๊ทธ ๋ณด๋‹ค ๋‚˜์ค‘์— ์ˆ˜์ •๋˜์—ˆ์œผ๋ฏ€๋กœ 200 ์‘๋‹ต ์ฝ”๋“œ๋กœ ๋ฆฌ์†Œ์Šค๋ฅผ ๋‹ค์‹œ ๋ณด๋‚ธ๋‹ค.

 

References

์ธํ”„๋Ÿฐ - ๋ฐฑ๊ธฐ์„ ๋‹˜์˜ ์Šคํ”„๋ง ๋ถ€ํŠธ ๊ฐœ๋…๊ณผ ํ™œ์šฉ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€