์คํ๋ง ๋ถํธ ์คํํฐ ์ฌ์ดํธ๋ก ์ด๋ํด์ ์คํ๋ง ํ๋ก์ ํธ ์์ฑ
์คํ๋ง ๋ถํธ ๊ธฐ๋ฐ์ผ๋ก ์คํ๋ง ๊ด๋ จ๋ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์ด์ฃผ๋ ์ฌ์ดํธ
Maven
๋ Gradle
์ ๋น๋ ๊ด๋ฆฌ ๋๊ตฌ
ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ, ์ ์ฒด์ ์ธ ๋ผ์ดํ์ฌ์ดํด์ ๊ด๋ฆฌํ๋ ๋๊ตฌ
๐ ๋น๋ ๊ด๋ฆฌ ๋๊ตฌ
1. ์ข
์์ฑ ๋ค์ด๋ก๋ - ์ ์ฒ๋ฆฌ(Preprocessing)
2. ์์ค์ฝ๋๋ฅผ ๋ฐ์ด๋๋ฆฌ ์ฝ๋๋ก ์ปดํ์ผ(Compile)
3. ๋ฐ์ด๋๋ฆฌ ์ฝ๋๋ฅผ ํจํค์ง(Packaging)
4. ํ
์คํธ ์คํ(Testing)
5. ํ๋ก๋์
์์คํ
์ ๋ฐฐํฌ(distribution)
โ ์์ฆ์๋ Gradle
์ ์ฌ์ฉ
SNAPSHOT
: ์์ง ๋ง๋ค๊ณ ์๋ ๋ฒ์
SNAPSHOT
, M1
: ์ ์ ๋ฆด๋ฆฌ์ฆ ๋์ง ์์
Group
: ๊ธฐ์
๋๋ฉ์ธ๋ช
Artifact
: ๋น๋ ๊ฒฐ๊ณผ๋ฌผ(ํ๋ก์ ํธ๋ช
)
์คํ๋ง๋ถํธ ๊ธฐ๋ฐ ํ๋ก์ ํธ ์์ ์ ์ฌ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ ํ
Spring web
: ์น ํ๋ก์ ํธ
Thymeleaf
: HTML์ ๋ง๋ค์ด์ฃผ๋ ํ
ํ๋ฆฟ ์์ง
.idea
: IntelliJ๊ฐ ์ฌ์ฉํ๋ ์ค์ ํ์ผ
gradle
: gradle ์ฌ์ฉ ๊ด๋ จ ํด๋
src
: main, test ํด๋๊ฐ ๋๋์ด์ ธ ์์
src/main/java
: ์ค์ ํจํค์ง, ์์คํ์ผ
src/main/resources
: ์๋ฐ ์ฝ๋ ํ์ผ์ ์ ์ธํ xml, properties, html ๋ฑ ์ค์ ํ์ผ
src/test
: ํ
์คํธ ์ฝ๋ ๊ด๋ จ ์์ค
.gitignore
: ์์ค ์ฝ๋ ๊ด๋ฆฌ
build.gradle
: ๋ฒ์ ์ค์ , ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ฐ์ ธ์ค๊ธฐ
Gradle์ ์์กด๊ด๊ณ๊ฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํจ๊ป ๋ค์ด๋ก๋ํจ
๋๋ฉ์ธ์ ์ ๋ ฅํ๊ณ ๋ค์ด์์ ๋ ๊ฐ์ฅ ๋จผ์ ๋ํ๋๋ ํ์ด์ง
โ ์คํ๋ง ๋ถํธ๋ resources/static
์ index.html
ํ์ผ์ ์์ฑํ๋ฉด ํด๋น ํ์ผ์ Welcome Page๋ก ์ธ์ํ๋ค -> ์ ์ ํ์ด์ง
๋์ ํ์ด์ง๋ฅผ ์์ฑํ๊ธฐ ์ํด์๋ ํ ํ๋ฆฟ ์์ง์ ์ฌ์ฉ
๐ ๋์ ๋ฐฉ์
์น ๋ธ๋ผ์ฐ์ ์์ http://localhost:8080/hello
๋ฅผ ๋์ง๋ฉด ๋ด์ฅ ํฐ์บฃ ์๋ฒ๊ฐ ๋ฐ์ ์คํ๋งํํ
์ ๋ฌํ๋ค.
์ ๋ฌ๋ฐ์ ์คํ๋ง์ ๋งค์นญ ๊ฐ๋ฅํ Controller๋ฅผ ์ฐพ์ ํด๋น ๋ฉ์๋๋ฅผ ์คํ์ํจ๋ค. (@GetMapping)
์คํ๋ง์ Model์ด๋ผ๋ ๊ฒ์ ๋ง๋ค์ด์ ํจ๊ป ๋ฃ์ด์ฃผ๋๋ฐ ์ด ๋ชจ๋ธ์ ์ฌ๋ฌ Attribute๋ฅผ ์ถ๊ฐํ์ฌ View์ ๋ณด์ผ ์ ์๋๋ก ํ๋ค. (model.addAttribute)
๋ฆฌํด ๊ฐ์ผ๋ก ViewName์ ๋ฐํํ๋ฉด viewResolver๊ฐ resources/templates/{ViewName}.html
ํ๋ฉด์ ์ฐพ์์ ์ฒ๋ฆฌํ๋ค. (return "hello";)
๐ Controller
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") //์น ์ ํ๋ฆฌ์ผ์ด์
์์ /hello๊ฐ ๋ค์ด์ค๋ฉด hello ๋ฉ์๋๋ฅผ ํธ์ถํด์ค
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello"; //๋ฌธ์์ด ๊ฐ์ ๋ง๋ html์ ์ฐพ์ ํ๋ฉด์ ๋ณด์ฌ์ค
}
}
๐ View
th
๋ก ์ ์ธ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>
๐ ์คํํ๋ฉด
http://localhost:8080/hello
1. ๋ช
๋ น ํ๋กฌํฌํธ(cmd)์์ ํ๋ก์ ํธ ํด๋๋ก ์ด๋
2. gradlew build๋ก build ์์ฑ
3. build ํด๋ ์์ libs๋ก ์ด๋ (cd build/libs)
4. ์๋ฐ ๋ช
๋ น์ด๋ก ์คํ (java -jar hello-spring-0.0.1-SNAPSHOT.jar)
โ dir ๋ช
๋ น์ด๋ก ๋๋ ํ ๋ฆฌ ํ์ธ
โ gradlew clean ๋ช
๋ น์ด๋ก ๋ชจ๋ build ํ์ผ ์ญ์
์คํ๋ง ์ ๋ฌธ - ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB ์ ๊ทผ ๊ธฐ์