Home > 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