๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ
# ์์กด๊ด๊ณ
**Gradle์ ์์กด๊ด๊ณ๊ฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํจ๊ป ๋ค์ด๋ก๋ ํ๋ค.
์์ ์๋ ์น ์๋ฒ๋ฅผ ์ง์ ์๋ฒ์ ์ค์นํ๊ณ ์๋ฐ ์ฝ๋๋ฅผ ๋ฐ์ด๋ฃ๋ ์์ผ๋ก ์น ์๋ฒ์ ๊ฐ๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์ ํ ๋ถ๋ฆฌ๋์ด ์์์
> ์์ฆ์๋ ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์น์๋ฒ๋ฅผ ๋ด์ฅ(์๋ฒ ๋๋)ํ๊ณ ์๊ธฐ ๋๋ฌธ์ ์๋ฐ ๋ฉ์ธ ๋ฉ์๋๋ง ์คํํด๋ ์น ์๋ฒ๊ฐ ๋ธ
๋ผ์ด๋ธ๋ฌ๋ฆฌ ํ๋๋ฅผ ๋น๋ํด์ ์น ์๋ฒ์ ์ฌ๋ฆฌ๋ฉด ๋๋๋ ๊ฐ๋จํ ๋ฐฉ์์ผ๋ก ๊ฐ๋ฐํ๋ ์ถ์ธ์ด๋ค.
# ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ์คํ๋ง ๋ถํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- spring-boot-starter-web
- spring-boot-starter-tomcat: ํฐ์บฃ (์น ์๋ฒ)
- spring-webmvc: ์คํ๋ง ์น MVC
- spring-boot-starter-thymeleaf: ํ์๋ฆฌํ ํ ํ๋ฆฟ ์์ง (View)
- spring-boot-starter (๊ณตํต): ์คํ๋ง ๋ถํธ + ์คํ๋ง ์ฝ์ด + ๋ก๊น
- spring-boot
- spring-core
- spring-boot-starter-logging
- logback, slf4j
- spring-boot
- ํ ์คํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- spring-boot-starter-test
- junit: ํ ์คํธ ํ๋ ์์ํฌ
- mockito: ๋ชฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- assertj: ํ ์คํธ ์ฝ๋ ์์ฑ์ ํธํ๊ฒ ํด ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- spring-test: ์คํ๋ง๊ณผ ํตํฉํ์ฌ ํ ์คํธํ ์ ์๊ฒ ์ง์ํด ์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
# ๋ก๊น ์ ๋ํ์ฌ

> ์ค๋ฌด์์๋ System.out.println()์ด ์๋ ๋ก๊ทธ๋ก ์ถ๋ ฅํด์ผ ํจ
๋ก๊ทธ๋ก ๋จ๊ฒจ์ผ ์ฌ๊ฐํ ์๋ฌ๋ฅผ ๋ฐ๋ก ๋ชจ์๋ณด๋ ๋ฑ ๋ก๊ทธ ํ์ผ์ ๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ
logback(์ค์ ๋ก๊ทธ๋ฅผ ์ด๋ค ๊ตฌํ์ฒด๋ก ์ถ๋ ฅํ ๊ฒ์ธ๊ฐ), slf4j(์ธํฐํ์ด์ค) ์กฐํฉ์ ๋ง์ด ์ฌ์ฉ,
ํ์ค์ผ๋ก ์ฑํํ๊ธฐ ๋๋ฌธ์ spring-boot-starter-logging์ ์ฌ์ฉํ๋ฉด ์์กด๊ด๊ณ์ ์ํด ์๋์ผ๋ก ์ฃผ์ ๋จ
View ํ๊ฒฝ์ค์
# Welcome Page ๋ง๋ค๊ธฐ
์คํ๋ง ๋ถํธ๋ static/index.html ์ ์ฌ๋ ค๋๋ฉด Welcome page ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
src > main > resources > 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>
# Thymeleaf ํ ํ๋ฆฟ ์์ง
Controller๋ฅผ ์ด์ฉํ ๊ฐ ๋ฐํ ๋ฐ ์ฌ์ฉ ๊ฐ๋ฅ
> src/main/java/hello.hellospring/controller/HelloController.java
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
}
> resources/templates/hello.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'์๋
ํ์ธ์. ' + ${data}" >์๋
ํ์ธ์. ์๋</p>
</body>
</html>
๋ ๊ฐ์ ์ฝ๋๋ฅผ ์ถ๊ฐํ ๋ค ํ๋ก์ ํธ๋ฅผ Stop > ์คํํ๋ฉด ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋ํ๋๋ค.
# Thymeleaf ํ ํ๋ฆฟ ์์ง ๋์ ํ๊ฒฝ
> ์น๋ธ๋ผ์ฐ์ ์์ localhost:8080/hello๋ฅผ ๋์ง๋ฉด ๋ด์ฅ ํฐ์ผ ์๋ฒ๊ฐ ๋ฐ์์ Spring์๊ฒ ๋์ง
> helloController์ @GetMapping์ ํตํด "hello" url์ ๋งคํ๋จ
> Spring์ด model(data: hello!!)๋ฅผ ๋ง๋ค์ด์ ๋ฃ์ด์ฃผ๊ณ hello ๋ฆฌํด
> hello ๋ฆฌํด = resources/templates/hello๋ฅผ ์ฐพ์์ ๋ ๋๋ง ์์ผ๋ผ (data : hello!!๋ฅผ ํ๋ฉด์ ๋๊ธฐ๋ฉด์)
์ฆ, ์ปจํธ๋กค๋ฌ์์ ๋ฆฌํด ๊ฐ์ผ๋ก ๋ฌธ์๋ฅผ ๋ฐํํ๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ(viewResolver)๊ฐ ํ๋ฉด์ ์ฐพ์์ ์ฒ๋ฆฌ
> ๊ธฐ๋ณธ์ ์ธ ์คํ๋ง ๋ถํธ์ ์ธํ ์ resources/templates ์๋์ {ViewName}.html์ ์ฐพ์์ ๋งคํ
๋น๋ ๋ฐ ์คํ
**๋ฐ๋์ IntelliJ์์ ํ๋ก์ ํธ ์ข ๋ฃ ํ ์คํ
$ ./gradlew build
$ cd build
$ cd libs
> ์์ฑ๋ jar ํ์ผ๋ช ์ ls -arlth๋ฅผ ํตํด ํ์ธ ํ ์คํ(์๋ ์ฝ๋ ์ ๋ ฅ)
$ java -jar hello-spring-0.0.1-SNAPSHOT.jar
> ์ ์์ ์ผ๋ก ๋ฐฐํฌ๋์๋์ง ํ์ธํ๊ธฐ ์ํด localhost:8080 ์ ์
์ฑ๊ณต-!
๋ด์ฉ ๋ฐ ์ด๋ฏธ์ง ์ถ์ฒ : ๊น์ํ์ ์คํ๋ง ์ ๋ฌธ