Home > Backend > Spring > ๐Ÿƒ[Spring] Welcome Page ๊ตฌํ˜„ ๋ฐ ๋™์ž‘ ๋ฐฉ๋ฒ•.

๐Ÿƒ[Spring] Welcome Page ๊ตฌํ˜„ ๋ฐ ๋™์ž‘ ๋ฐฉ๋ฒ•.
Spring Framework

๐Ÿƒ[Spring] Welcome Page ๊ตฌํ˜„ ๋ฐ ๋™์ž‘ ๋ฐฉ๋ฒ•.

1๏ธโƒฃ Welcom Page ๋งŒ๋“ค๊ธฐ.

  • Welcom Page๋Š” 'resource/static' ๋‚ด๋ถ€์— 'index.html' ์ด๋ผ๋Š” ํŒŒ์ผ๋กœ ๋งŒ๋“ค๋ฉด ๋ฉ๋‹ˆ๋‹ค.
    ```html
    <!DOCTYPE html>
Hello Hello hello

- ์Šคํ”„๋ง ๋ถ€ํŠธ๊ฐ€ ์ œ๊ณตํ•˜๋Š” Welcom Page ๊ธฐ๋Šฅ.
    - **`'static/index.html'`** ์„ ์˜ฌ๋ ค๋‘๋ฉด Welcom page ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
    - [Spring.io ๊ณต์‹ ๋„ํ๋จผํŠธ](https://docs.spring.io/spring-boot/3.3-SNAPSHOT/reference/web/reactive.html#web.reactive.webflux.welcome-page)

## 2๏ธโƒฃ thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„.
- [thymeleaf ๊ณต์‹ ์‚ฌ์ดํŠธ](https://www.thymeleaf.org/)
- [์Šคํ”„๋ง ๊ณต์‹ ํŠœํ† ๋ฆฌ์–ผ](https://spring.io/guides/gs/serving-web-content)
- [์Šคํ”„๋ง๋ถ€ํŠธ ๋ฉ”๋‰ด์–ผ](https://docs.spring.io/spring-boot/3.3-SNAPSHOT/reference/web/servlet.html#web.servlet.spring-mvc.template-engines)

```java
// com/devkobe/hello_spring/controller
@Controller
public class HelloController {
    
    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}
<!-- `resource/static/index.html` -->

<!DOCTYPE html>
<html>
	<head>
		<title>Hello</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	</head>
	<body>
		Hello
		<a href="/hello">hello</a>
	</body>
</html>
<!-- `resource/templates/hello.html` -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<title>Hello</title>
</head>
<meta charset="UTF-8">
<body>
<p th:text="'์•ˆ๋…•ํ•˜์„ธ์š”. ' + ${data}" >์•ˆ๋…•ํ•˜์„ธ์š”. ์†๋‹˜</p>
</body>
</html>
  • thymeleaf ํ…œํ”Œ๋ฆฟ์—”์ง„ ๋™์ž‘ ํ™•์ธ
    • ์‹คํ–‰ : http://localhost:8080/hello
  • ๋™์ž‘ ํ™˜๊ฒฝ ๊ทธ๋ฆผ.

  • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ„('viewResolver') ๊ฐ€ ํ™”๋ฉด์„ ์ฐพ์•„์„œ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    • ์Šคํ”„๋ง ๋ถ€ํŠธ ํ…œํ”Œ๋ฆฟ์—”์ง„ ๊ธฐ๋ณธ 'viewName' ๋งคํ•‘.
      • 'resources:template/' + '{ViewName}' + '.html'

๐Ÿ™‹โ€โ™‚๏ธ ์ฐธ๊ณ : 'spring-boot-devtools' ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด 'html' ํŒŒ์ผ์„ ์ปดํŒŒ์ผ๋งŒ ํ•ด์ฃผ๋ฉด ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ์—†์ด View ํŒŒ์ผ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
IntelliJ ์ปดํŒŒ์ผ ๋ฐฉ๋ฒ• : ๋ฉ”๋‰ด build โž” Recompile