์คํ๋ง ์ ๋ฌธ - ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB ์ ๊ทผ ๊ธฐ์ ์ ๋ค์ผ๋ฉฐ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
์ ์ฒด์ ์ธ ํ๋ฆ
- Spring Project ์์ฑ
- Spring boot๋ก ์น ์๋ฒ ์คํ
- ํ์ ๋๋ฉ์ธ ๊ฐ๋ฐ
- ์น MVC ๊ฐ๋ฐ
- DB ์ฐ๋ - JDBC, JPA, Spring data JPA
- ํ ์คํธ ์ผ์ด์ค ์์ฑ
๐ ํ๋ก์ ํธ ์์ฑ
โ ์ฌ์ ์ค๋น
- JAVA 11
- IDE: IntelliJ or Eclipse
Springboot Starter๋ฅผ ์ฌ์ฉํ์ฌ ํ๋ก์ ํธ ์์ฑ โ https://start.spring.io
-
Project: ์ด๋ค ๋น๋ ๊ด๋ฆฌ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ๊ฒ์ธ๊ฐ ?
-
Maven: ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ก๊ฒจ์ค๊ณ ๋น๋ํ๋ lifecycle๊น์ง ๊ด๋ฆฌํ๋ ๋น๋ ๊ด๋ฆฌ ๋๊ตฌ
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์๊ฐ ๋ง์์ง๋ฉด, ๋น๋ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ์ ๋ฐ์ ์๋ค.
-
Gradle: Maven๊ณผ ๋ค๋ฅด๊ฒ, ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ํ ์ค์ ํ์ผ(xmlํ์ผ)์ ์ฝ๋๊ฐ ๋งค์ฐ ๊ฐ๊ฒฐํ ๋น๋ ๊ด๋ฆฌ ๋๊ตฌ
- ์ต๊ทผ ๋ง์ด ์ฌ์ฉ
-
์ด๋ฏธ ์ ๋ฐ์ดํธ๊ฐ ๋ฐ์๋ ๋ถ๋ถ์ ๋ค์ ๋น๋ํ์ง ์๊ธฐ์ ๋น๋ ์๊ฐ์ด maven์ ๋นํด ์ ์ด์ง๊ฒ ๋๋ค.
๋น๋ ๊ด๋ฆฌ ๋๊ตฌ๋ ? ํ๋ก์ ํธ์์ ์์ฑํ ํ์ผ๋ค์ JVM์ด๋ WAS์์ ์ธ์ํ ์ ์๋๋ก ํจํค์งํด์ฃผ๋ ๋๊ตฌ
-
- Project Spring Boot: 2.5.6
- Language: Java
- Group: ๋ณดํต ๊ธฐ์ ๋๋ฉ์ธ๋ช
- Artifact: ๋น๋๋์ด ๋์ค๋ ๊ฒฐ๊ณผ๋ฌผ, ํ๋ก์ ํธ๋ช
-
Packaging: ์ด๋ค ํจํค์ง ๋ฐฉ์์ ์ฌ์ฉํ ๊ฒ์ธ๊ฐ ?
-
Jar(Java Archieve): Java application์ด ๋์ํ ์ ์๋๋ก ํจํค์งํ๋ ๋ฐฉ์
- JRE๋ง ์์ด๋ ์คํ์ด ๊ฐ๋ฅํ๋ค.
-
War(Web Application Archieve): Web application ์ ์ฒด๋ฅผ ํจํค์งํ๋ ๋ฐฉ์
- ๋ณ๋์ Web Server ๋๋ WAS๊ฐ ํ์ํ๋ค.
-
- Java: 11
-
Dependencies: ์ด๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๊ฒ์ธ๊ฐ ?
- Spring Web(Embedded Tomcat ์ถ๊ฐ๋ฅผ ์ํจ, Web ํ๋ก์ ํธ ์์ฑ ์ ํ์)
- Thymeleaf(Controller๊ฐ ์ ๋ฌํ๋ Data๋ฅผ ์ด์ฉํ์ฌ HTML์ ๊พธ๋ฐ ์ ์๋๋ก ํ๋ Template Engine)
- ์ถ๊ฐ์ ์ผ๋ก, lombok(Annotation์ ์ด์ฉํด Compile ์์ ์ Getter, Setter, ์์ฑ์, toString, Builder ๋ฑ์ ์๋์ผ๋ก ๋ง๋ค์ด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ)
์ดํ GENERATE๋ก .zip ํ์ผ์ ๋ค์ด๋ก๋ํ ํ, unzip ํ ํ์ผ์ IntelliJ ๋ก OPEN
Project open์, ์ธ๋ถ์ library๋ฅผ ๋ก๋ฉํ๋ ๊ณผ์ ์ด ํ์ํ๊ธฐ ๋๋ฌธ์, ์๊ฐ์ด ์ข ๊ฑธ๋ฆด ์ ์๋ค.
โ ํ๋ก์ ํธ ๊ตฌ์กฐ

src
-
main,test๋ก ๋๋๋ค.main/java์ ์ค์ ํจํค์ง๋ค๊ณผ ์์ค ํ์ผ์ด ์กด์ฌmain/resources์๋ ์ค์ ํ์ผ๋ค์ด ์กด์ฌ, java ํ์ผ์ ์ ์ธํ ๋ชจ๋ ํ์ผ-
test/java์ test ๊ด๋ จ ์ฝ๋๋ค์ด ์กด์ฌ- ์ด๋ ๊ณง TEST๋ผ๋ ๊ฒ์ด ๋งค์ฐ ์ค์ํ ์กด์ฌ์์ ์๋ ค์ค๋ค.
build.gradle
plugins {
id 'org.springframework.boot' version '2.5.6' // starter์์ ์ ํํ ๋ฒ์
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java' // ์ ํํ ์ธ์ด
}
// Metadata
group = 'hello'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11' // java version
// ์๋์ ์ ํํ library๋ฅผ ๋ค์ด๋ก๋๋ฐ๋ ๊ณณ, ํ์ํ๋ฉด ํน์ ์ฌ์ดํธ url ์ง์ ๊ฐ๋ฅ
repositories {
mavenCentral() //
}
// ์ ํํ library๋ค
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:2.5.6'
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.6'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.6'
}
test {
useJUnitPlatform()
}
์ด์ src/main/java/hello.hellospring ์์ ์๋ ๋ฉ์ธ ์ฝ๋๋ฅผ ์คํ์์ผ๋ณด์.

์คํํ ํ, http://localhost:8080 ์ผ๋ก ์ ์ํด๋ณด์.

์์ง์ ์๋ฌด๋ฐ View๋ฅผ ๊ตฌ์ฑํ์ง ์์๊ธฐ์, ์์ ๊ฐ์ ํ๋ฉด์ด ๋จ๊ฒ ๋๋ค.
๐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ
ํ๋ก์ ํธ์์ build.gradle ์ ๋ณด๋ฉด, ์๋์ ๊ฐ์ด ์คํํฐ๋ก ํ๋ก์ ํธ ์์ฑํ ๋ ์ถ๊ฐํ๋ 3๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ํ์ธํ ์ ์๋ค.
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf:2.5.6'
implementation 'org.springframework.boot:spring-boot-starter-web:2.5.6'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.6'
}
๊ทธ๋ฆฌ๊ณ , External Libraries ์์๋ ํ๋ก์ ํธ ์์ฑ ์ ๋ก๊ฒจ์จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ํ์ธํ ์ ์๋๋ฐ, ์ฒ์์ ์ถ๊ฐํ๋ 3๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ณด๋ค ํจ์ฌ ๋ ๋ง์ ์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ก๊ฒจ์์ง ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด๋ฌํ ์ด์ ๋ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ๊ธฐ ์ํด ์ถ๊ฐํ 3๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๋์ํ๊ธฐ ์ํด์๋ ํ์ํ ๋ค๋ฅธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์กด์ฌํ๋ค. Gradle์ ์ด๋ฌํ ์ถ๊ฐ์ ์ธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์๋์ผ๋ก External Libraries ์ ์ถ๊ฐํด์ฃผ๊ธฐ ๋๋ฌธ์ ๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์ถ๊ฐ๋์ด ์๋ ๊ฒ์ด๋ค.
์ ํ๋ฉด์์ ๋ณด์ด๋ฏ, Gradle > hello-spring > Dependencies์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ์ ์์กด๊ด๊ณ๋ฅผ ํ์ธํ ์ ์๋ค.
์ฌ๊ธฐ์ ์ค์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก๋ ๋จผ์ spring-boot-starter-tomcat ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋๋ฐ, ์ด๋ Spring boot๊ฐ ํฐ์บฃ ์น์๋ฒ๋ฅผ ๋ด์ฅ๋์ด ์๊ธฐ ๋๋ฌธ์ ์กด์ฌํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ด๋ค.
๋ํ, spring-boot-starter-logging ์ด๋ผ๋ ๊ฒ์ด ์๋๋ฐ, ์ด๋ ์๋ฒ ๊ฐ๋ฐ์๋ค์ด ์ด๋ ํ ์๋ฌ๋ค์ ์ถ๋ ฅํ๊ณ ํ ๋์ ๋ชจ์์ ๋ณด๊ธฐ ์ํด ์ฌ์ฉํ๋ ๊ฒ์ด๋ค. ๋ด๋ถ์๋ logback, slf4j ๋ผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ด์ฅ๋์ด ์๋ค.
๐ View ํ๊ฒฝ์ค์
ํ์ฌ ์๋ฒ๋ฅผ ์คํํ๊ณ http://localhost:8080 ์ผ๋ก ์ ์ํ๋ฉด, ์๊น ์์์ ๋ณธ ํ๋ฉด์ด ๋ฌ๋ค.
์ด์ ํ๋ฉด ๊ตฌ์ฑ์ ํ ๋ฒ ํด๋ณด๋๋ก ํ์.
โ Welcome Page
resources/static/index.html
์ ์์ฑํ๊ฒ ๋๋ฉด, ์ด๋ฅผ ์๋์ผ๋ก welcome page๋ก ์ค์ ํด์ค๋ค. ์ด๋ ์ ์ ํ์ด์ง๋ก์, ์์ฑํ ์ฝ๋๋ฅผ ๊ทธ๋๋ก ๋ธ๋ผ์ฐ์ ์ ๋์ ธ์ฃผ๋ ๊ฒ์ด๋ค.
<!-- 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>
์ฝ๋๋ฅผ ์์ฑํ๊ณ ์๋ฒ๋ฅผ ์ฌ์์ํ๋ฉด, ์๋์ ํ๋ฉด์ ๋ณผ ์ ์์ ๊ฒ์ด๋ค.
Spring์ ๊ทธ ์ํ๊ณ๊ฐ ์ด๋ง์ด๋งํ๊ฒ ํฌ๊ธฐ ๋๋ฌธ์, ๋จธ๋ฆฟ์์ ๋ชจ๋ ๊ฒ์ ๋ค ๋ฃ์ ์๋ ์๋ค. ๋ฐ๋ผ์, ์ํ๋ ๊ฒ์ ์ ์ฐพ๋ ๋ฅ๋ ฅ์ด ํ์ํ๋ค. https://docs.spring.io/spring-boot/docs/current/reference/html/ ์ ํํ์ด์ง์์ ๊ด๋ จ ๋ฌธ์๋ฅผ ์ฐพ์๋ณด๋๋ก ํ์.
์ ํํ์ด์ง์์, Welcome page์ ๋ํ ์ค๋ช
์ ์ฐพ์๋ณด์๋ค.
์ค๋ช
์ ๊ฐ์ ธ์ค์๋ฉด, ์คํ๋ง ๋ถํธ๋ static & templated welcome page ๋ชจ๋ ์ง์ํ๋ค. ๋จผ์ static/index.html ์ ์ฐพ๊ณ , ์๋ค๋ฉด template/index.html ์ ์ฐพ์ Welcome Page๋ก์ ์ฌ์ฉํ๋ค.
resources/templates
static ๊ณผ ๋ค๋ฅธ ๋์ ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋๋ฐ ์ฌ์ฉํ๋ค. ์ฌ๊ธฐ์ ์ฐ๋ฆฌ๋ ์ฒ์์ ์ถ๊ฐํ๋ thymeleaf ๋ผ๋ ํ
ํ๋ฆฟ ์์ง์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
์ด์ ๊ด๋ จ๋ ๋ฌธ์๋ก๋ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ๋ฉด ๋๋ค.
์คํ๋ง ๋ถํธ๋ FreeMarker, Groovy, Thymeleaf, Mustache์ ์ด 4๊ฐ์ ํ
ํ๋ฆฟ ์์ง์ ๋ํ auto-configuration ์ง์์ ํ๋ค.
auto-configuration์ด๋?
์ด์ , ์ค์ ๋ก ๋์ํ๊ณ , ํ๋ก๊ทธ๋๋ฐ์ด ๋๋ ํ๋ฉด์ ๊ตฌํํด๋ณด๋๋ก ํ์.
๋จผ์ , src/main/java/hello.hellospring ์ controller ๋ผ๋ Package๋ฅผ ์์ฑํ๋ค. ์ดํ HelloController ๋ผ๋ Class๋ฅผ ์์ฑํ๋ค.
// HelloController
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
@Controller
public class HelloController {
}
์ด๋, @Controller ๋ผ๋ Annotation์ด ํ์ํ๋ฐ, ์ด๋ ์ผ์ข
์ ๋ฉํ๋ฐ์ดํฐ๋ก์, ์ปดํ์ผ๋ฌ๊ฐ ํน์ ์ค๋ฅ๋ฅผ ์ต์ ํ๋๋ก ์ง์ํ๋ ๊ฒ๊ณผ ๊ฐ์ด ํ๋ก๊ทธ๋จ ์ฝ๋์ ์ผ๋ถ๊ฐ ์๋ ํ๋ก๊ทธ๋จ์ ๊ดํ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณต, ์ฝ๋์ ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋๋ฐ ์ฌ์ฉ๋๋ค. ์ฐธ๊ณ
@Controller
public class HelloController {
@GetMapping("hello")
public String Hello(Model model) {
}
}
ํด๋์ค ์์ @GetMapping("hello") ๋ Web application์์ "/hello"๊ฐ ๋ค์ด์ค๋ฉด, Hello(Model model) ์ ์คํํ๋๋ก ํ๋ค. ์ฌ๊ธฐ์ model์ Model, View, Controller์ model์ด๊ณ , Get์ Get or Post method๋ฅผ ์๋ฏธํ๋ค.
@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>
๋ฅผ ์์ฑํ๋๋ก ํ๋ค. ์ผ๋จ์ ๋ฐ๋ผ์ ์ณ๋ณด๋๋ก ํ์.
์ ์ฝ๋์์ th ๋ผ๋ ๊ฒ์ ๋ณผ ์ ์๋๋ฐ, ์ด๋ ์ ์ผ ์ ์ฝ๋์์ ์ ์ธํ xmlns:th="http://www.thymeleaf.org" ๋ฅผ ์๋ฏธํ๊ณ , ์ด ์ ์ธ์ผ๋ก thymeleaf ๋ฌธ๋ฒ์ ์ฌ์ฉํ ์ ์๊ฒ ๋ ๊ฒ์ด๋ค.
๊ทธ๋ฆฌ๊ณ <p th:text="'์๋
ํ์ธ์. ' + ${data}">์๋
ํ์ธ์. ์๋</p> ์์ data๋ ์๊น ์์ HelloController ์์ ์์ฑํ model.addAttribute("data", "hello!!!"); ์ data๋ฅผ ์๋ฏธํ๋ค. ์ด ๋ฉ์๋์์ "data"๋ attributeName์ด๊ณ , "hello!!!"๋ attributeValue๊ฐ ๋๋ค. ์ดํ ์ผ๋จ ์๋ฒ๋ฅผ ์ฌ์์ํด๋ณด์.
์์ ๊ฐ์ด /hello๋ก ์ ์ํ๊ฒ ๋๋ฉด, ${data}๊ฐ "hello!!!"๋ก ์นํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
-
localhost:8080/hello์GET๋ฐฉ์์ผ๋ก ์ ๊ทผํ๊ฒ ๋๋ฉด, ์์ฑํHelloController์์ statement๋ค์ ์คํํ๊ฒ ๋๋ค.- ์ด๋,
return hello;์์ hello๋templates/hello.html์ hello๋ก ๋งค์นญ๋์ด ํด๋น html ํ์ผ์ ๋ ๋๋งํ๋ผ๋ ์๋ฏธ์ด๋ค. (ViewName = hello) - SpringBoot๋ ๊ธฐ๋ณธ์ ์ผ๋ก
resources:templates/ + {ViewName} + .htmlํ์ผ์ ์ฐพ๋๋ค.
- ์ด๋,
๐ ๋น๋ํ๊ณ ์คํํ๊ธฐ
๊ผญ ์๋ฒ๋ฅผ ์ข ๋ฃํ ํ,
./gradlew buildโ "ํ์ํ lib๋ค์ ๋ค์ด๋ฐ๊ณ ๋น๋๊ฐ ์ํ"
์ด์ ๊ฐ์ด ๋จ๋ฉด ๋น๋๊ฐ ์ฑ๊ณต๋ ๊ฒ์ด๋ค.cd build/libsโ "ํด๋น ํด๋์ ๋น๋๋jarํ์ผ์ด ์์ฑ๋จ" ์ด๋ ์คํํฐ๋ก ์ค์ ํ Packaging ๋ฐฉ์์ผ๋ก ์ธํดjarํ์ผ์ด ์์ฑ๋๋ค.
์์ ๊ฐ์ด hello-spring-0.0.1-SNAPSHOT-plain.jar์ด๋ผ๋ ํ์ผ๋ ๊ฐ์ด ์์ฑ๋๋๋ฐ, ์ด๋ Spring-boot 2.5.0 ์ด์๋ถํฐ ์์ฑ๋๋ ๋ฏํ๋ค. ์ด๋ฅผ ์์ ๋ ค๋ฉด,build.gradleํ์ผ ์์jar { enabled = false }์ฝ๋๋ฅผ ์ถ๊ฐํด์ฃผ๊ณ build๋ฅผ ์ํํ๋ฉด ๋๋ค.java -jar hello-spring-0.0.1-SNAPSHOT.jar
์๋ฒ๊ฐ ๊ฐ๋๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด์ ๋น๋๋ ๊ฒฐ๊ณผ๋ฌผ์ธ hello-spring-0.0.1-SNAPSHOT.jar ๋ง ํ์ผ๋ง ์คํ์ํค๋ฉด ์๋ฒ๊ฐ ๋์ํ ์ ์๊ฒ ๋์๋ค.
./gradlew clean:buildํด๋๊ฐ ์ญ์ ๋๋ค../gradlew clean build:buildํด๋๋ฅผ ์ญ์ ํ๊ณ ์๋ก build ์ํ
๐ ์ฐธ๊ณ
์คํ๋ง ์ ๋ฌธ-์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB ์ ๊ทผ ๊ธฐ์
Spring Boot DevTools
:ํ์ผ ์์ ์ดํ ๋ณ๋๋ก ์๋ฒ๋ฅผ ์ฌ์์ํ ํ์ ์์ด ๋ณ๊ฒฝ ์ฌํญ์ด ์ ์ฉ๋๋๋ก ํด์ฃผ๋ ๋๊ตฌ

// application.properties
spring:
devtools:
restart:
enabled: true
thymeleaf:
cache: false
prefix: file:src/main/resources/templates/
Preferences โ Build, Execution, Deployment โ Compiler โ Build project automatically check!