๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Spring Boot] ์Šคํ”„๋ง๋ถ€ํŠธ View ํ™˜๊ฒฝ์„ค์ •(Welcom Page ๋งŒ๋“ค๊ธฐ) ๋ณธ๋ฌธ

๐Ÿ–Œ๏ธWeb/Spring Boot

[Spring Boot] ์Šคํ”„๋ง๋ถ€ํŠธ View ํ™˜๊ฒฝ์„ค์ •(Welcom Page ๋งŒ๋“ค๊ธฐ)

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 3. 7. 14:51
๋ฐ˜์‘ํ˜•

์ง€๋‚œ๋ฒˆ์—” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊นŒ์ง€ ๋ชจ๋‘ ํ™•์ธํ–ˆ์œผ๋‹ˆ
HTML ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด Welcom Page๋ฅผ ๋งŒ๋“ค์–ด๋ณด์ž.

1. Welcom Page ๋งŒ๋“ค๊ธฐ

recources - static์— index.html ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ๊ธฐ๋ณธํ˜•์‹์„ ์จ์ฃผ๊ณ 
์•„๋ž˜ body์— aํƒœ๊ทธ์˜ ๋งํฌ๋Š” url๋ฅผ ์จ์ค€๋‹ค.

 

์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด์ฃผ๊ณ  ์‹คํ–‰ํ•˜๋ฉด
body์˜ ํ…์ŠคํŠธ์™€ aํƒœ๊ทธ ๋งํฌ๊ฐ€ ๋‹ฌ๋ฆฐ ํ…์ŠคํŠธ๊ฐ€ ๋‚˜์˜จ๋‹ค.

์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ์ž๋ฃŒ

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-welcome-page

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„

thymeleaf ๊ณต์‹ ์‚ฌ์ดํŠธ: https://www.thymeleaf.org/

์Šคํ”„๋ง ๊ณต์‹ ํŠœํ† ๋ฆฌ์–ผ: https://spring.io/guides/gs/serving-web-content/

์Šคํ”„๋ง๋ถ€ํŠธ ๋ฉ”๋‰ด์–ผ: https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-template-engines

 

2. Controller ์ƒ์„ฑ

์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ hello.html์„ ์ฐพ์•„ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

controller ํŒจํ‚ค์ง€ ๋‚ด์— HelloController ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

@Controller ์• ๋„ˆํ…Œ์ด์…˜์€ ์ด ํด๋ž˜์Šค๊ฐ€ ์ปจํŠธ๋กค๋Ÿฌ์ž„์„ ์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ์— ์•Œ๋ฆฌ๊ณ 
์ปจํŠธ๋กค๋Ÿฌ๋Š” ์›น ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ด๊ณ  ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

@GetMapping("hello") ์• ๋„ˆํ…Œ์ด์…˜์€ ์ด ๋ฉ”์†Œ๋“œ๊ฐ€ /hello ๊ฒฝ๋กœ๋กœ ๋“ค์–ด์˜ค๋Š” GET ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ์ง€์ •๋‹ค.

Thymeleaf์˜ ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ์„ ์–ธ(xmlns:th="http://www.thymeleaf.org")ํ•œ html ํŒŒ์ผ์„ templetes์— ๋งŒ๋“ ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ์‹คํ–‰ํ•ด๋ณด๋ฉด ์ปจํŠธ๋กค๋Ÿฌ์— ์žˆ๋˜ data ํ‚ค์˜ ๊ฐ’์„ ์•ˆ๋…•ํ•˜์„ธ์š” ๋’ค์— ๋ถˆ๋Ÿฌ์˜จ๋‹ค.

 

ํ‚ค์˜ ๊ฐ’์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊พธ๊ณ  ๋‹ค์‹œ ์žฌ์‹คํ–‰์„ ํ•˜๋ฉด

 

์•ˆ๋…•ํ•˜์„ธ์š” ๋’ค์— ๋ฐ”๋€ data์˜ ๊ฐ’์œผ๋กœ ๋‚˜์˜จ๋‹ค.

๋ฐ˜์‘ํ˜•