
๋ธ๋ก๊ทธ ์น ํ์ด์ง๋ฅผ ๊ตฌํํ๋ ์ค์ต์ ์งํํ๊ณ ์๋ค.
๋ธ๋ก๊ทธ ํ๋ฉด์ ๊ตฌ์ฑํ๊ธฐ ์ํด html๊ณผ js ํ์ผ์ ์์ฑํ์๋ค.
html์์์ ์ญ์ ๋ฒํผ์ ํด๋ฆญํ์ ๋, jsํ์ผ์ด ๋์ํ์ฌ ํ์ผ์ ์ญ์ ์ํค๋ ๋ฉ์๋ ์์ฒญ์ ๋ณด๋ด์ผ ํ๋๋ฐ, ๋ฒํผ์ ๋๋ ค๋ ์๋ฌด๋ฐ ๋ฐ์์ด ์์๋ค.
๋ ๊ฐ์ง ์์ธ์ ์๊ฐํ๋ค.
1. ์ญ์ ๋ฒํผ์ ์ธ์ํ๊ธฐ ์ํด ์์ฑํ html์์์ id๊ฐ๊ณผ js์์์ id๊ฐ์ ๋ถ์ผ์น ๋ฌธ์
2. js ํ์ผ์ ๊ฒฝ๋ก๋ช
๋ฌธ์
๊ฒฐ๋ก ์ ๊ฒฝ๋ก ๋ฌธ์ ์๋ค.
resources >> static >> article.js ์ ๊ฐ์ ๊ฒฝ๋ก์ js ํ์ผ์ด ์กด์ฌํ๊ธฐ ๋๋ฌธ์, html ํ์ผ์์ js ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์์ฑํ ๋ /static/article.js๋ก ์์ฑํ๋ค.
ํ์ง๋ง SpringBoot๋ static, public, resources, META-INF/resources ํด๋๋ฅผ ์๋์ผ๋ก ์ ์ ๋ฆฌ์์ค ๊ฒฝ๋ก๋ก ๋งคํํ๋ค.
์๋ฅผ ๋ค์ด src/main/resources/static/article.js ๊ฒฝ๋ก๋ช
์ด ์๋ค๋ฉด
์น์์ ์ ๊ทผํ ๋๋ ๐๐ผ http://localhost:8080/article.js ๋ก ์ ๊ทผํ๋ ๊ฒ์ด๋ค.
classpath:/static/
classpath:/public/
classpath:/resources/
classpath:/META-INF/resources/
SpringBoot์ ์ ์ ์์ ๋งคํ ๊ฒฝ๋ก๋ ์์ ๊ฐ๋ค.
๋ฐ๋ผ์ ๋ธ๋ผ์ฐ์ ์์ ์ ๊ทผํ ๋ /static/article.js๊ฐ ์๋๋ผ /article.js๋ผ๊ณ ์์ฒญํ๋ฉด Spring์ด ์์์ static ํด๋ ๋ด๋ถ๋ฅผ ๋ค์ ธ ์ฐพ์์ฃผ๋ ๊ฒ์ด๋ค.
์ฆ, ํด๋๋ช
์ธ static์ ๊ฒฝ๋ก์์ ์๋ตํ๋ ๊ฒ์ด SpringBoot์ ๊ท์น์ โโ
๊ทธ๋ฌ๋ฉด html ํ์ผ์ด ๋ค์ด์๋ templates ํด๋๋ ๋ง์ฐฌ๊ฐ์ง์ผ๊น?
=> ์๋๋ค.
์นํ์ด์ง์ http://localhost:8080/templates/article.html ์ ์์ฒญํ์ ๋ 404 ์๋ฌ๊ฐ ๋ด๋ค.
templates ํด๋๋ static ํด๋์ ๋ค๋ฅด๊ฒ ๋์ํ๋ค.
templates ํด๋๋ ์ ์ ๋ฆฌ์์ค๊ฐ ์๋๋ผ ํ
ํ๋ฆฟ ์์ง์ด ์ฒ๋ฆฌํ๋ ๋์ ๋ฆฌ์์ค์ด๋ค.
๋ณดํต Thymeleaf, Mustache, Freemarker ๊ฐ์ ํ
ํ๋ฆฟ ์์ง์์ ์ฌ์ฉ๋๋ค.
๋ฐ๋ผ์ templates ์์ ์๋ ํ์ผ๋ค์ ์ง์ URL๋ก ์ ๊ทผํ ์ ์๊ณ , Controller์์ ํด๋น ํ์ผ ์ด๋ฆ์ ๋ฆฌํดํด์ผ ๋ทฐ๋ก ๋ ๋๋ง๋๋ค.
@Controller
public class BlogController {
@GetMapping("/blog")
public String getBlogPage(Model model) {
model.addAttribute("title", "๋ธ๋ก๊ทธ ์ ๋ชฉ");
return "blog"; // โ templates/blog.html ๋ ๋๋ง
}
}