-
[Spring] Spring Boot Project ์์ฑ, ๋น๋ ๊ทธ๋ฆฌ๊ณ ์คํํ๊ธฐ (+ View)๐ณDev/Dev 2022. 4. 14. 11:18
๊น์ํ ๋์ <์คํ๋ง ์ ๋ฌธ - ์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ, ์น MVC, DB ์ ๊ทผ ๊ธฐ์ > ๊ฐ์๋ฅผ ๋ฃ๊ณ ์ ๋ฆฌํ ๋ด์ฉ์ ๋๋ค.
์์ฑํ๊ธฐ
ํ๋ก์ ํธ ์์ฑ์ ์ํด Spring Initializer๋ก ์ด๋ํ์
Project : ๋น๋ ๊ด๋ฆฌ ๋๊ตฌ์ ๋ํ ๊ฒฐ์
Language : ์ธ์ด์ ๋ํ ๊ฒฐ์
Spring Boot : Spring Boot ๋ฒ์ ์ ๋ํ ๊ฒฐ์
Project Metadata : ํ๋ก์ ํธ MetaData๋ฅผ ๊ฒฐ์ , Group ์ด๋ฆ๊ณผ Artifact ์ด๋ฆ, Package ํ์ฅ์๋ช ๊ณผ Java ๋ฒ์ ์ ๋ฃ์ด์ค๋ค.
Dependencies : ํ๋ก์ ํธ์ ์ถ๊ฐํ ์์กด์ฑ์ ๊ฒฐ์ , ๋๋ Spring Web๊ณผ Thymeleaf๋ฅผ ์ถ๊ฐํด ์ฃผ์๋ค.
์ค์ ํ์ GENERATE ๋ฒํผ์ ๋๋ฅด๋ฉด ์์ถ ํ์ผ์ด ๋ด๋ ค ๋ฐ์์ง๋ค.
๊ทธ๋ฆฌ๊ณ ์์ถ์ ํผ ํ์ผ์ IntelliJ๋ฅผ ํตํด์ ์ด์ด์ค๋ค.
build.gradle ํ์ผ์ ๋ค์ด๊ฐ๋ฉด
์๊น ์์ฑํ ๋ ์ค์ ํ ๊ฐ๋ค์ ํ์ธํ ์ ์๋ค.
dependencies์ ๊ฒฝ์ฐ
๋ด๊ฐ ์ถ๊ฐํ thymeleaf์ web์ธ์ test๊ฐ ์๋์ผ๋ก ๋ค์ด์๋ค.
repositories๋ ๊ฐ์ข ์์กด์ฑ๋ค์ ์ด๋์ ๋ฐ์์ฌ์ง๋ฅผ ๊ฒฐ์ ํ๋ค.
mavenCentral๊ณผ jcenter๊ฐ ๋ํ์ ์ด๋ฉฐ,
์ฐ๋ฆฌ๋ ์ ์๋ฅผ ์ฌ์ฉํ๋ค.
https://mvnrepository.com/repos/central
IntelliJ์์ run์ ์คํํ๋ฉด
์๋์ ์ผ๋ก ์น์๋ฒ์ธ tomcat์ด ์คํ๋๊ณ
์ด๋ localhost:8080์์ ํ์ธ์ด ๊ฐ๋ฅํ๋ค.
+ View
์์ ๊ฐ์ Error 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> Spring <a href="/hello">hello</a> </body> </html>
static/index.html์ ์คํ๋ง ๋ถํธ๊ฐ ์ ๊ณตํ๋ Welcome Page ๊ธฐ๋ฅ์ด๋ค
Web
Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest
docs.spring.io
tymeleaf๋ฅผ ์ด์ฉํ๋ฉด ๋์ํ๋ View๋ฅผ ์ ๊ณตํ ์ ์๋ค.
์ด๋ฒ์๋ 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}">Hello</p> </body> </html>
๋๋ฒ์งธ ์ค<html>์์ thymeleaf๋ฅผ ์ ์ธํ๋ฉด, ์ฐ๋ฆฌ๋ ํ์๋ฆฌํ ๋ฌธ๋ฒ์ ์ฌ์ฉํ ์ ์๋ค.
๊ทธ๋ฆฌ๊ณ 8๋ฒ์งธ ์ค<p>์ ๋ณด๋ฉด ์ฐ๋ฆฌ๋ th๋ฅผ ํตํด, ํ์๋ฆฌํ ๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ ๋ถ๋ถ์์ ์ ์ ์๋ค.
์ดํ ${data}์ ๋ณ์์ ๊ฐ์ ์ญํ ์ ํ๋ค.
๋ค์์ java/hello/hellospring์ controller ํจํค์ง๋ฅผ ๋ง๋ค๊ณ HelloController.java๋ฅผ ์์ฑํ๋ค.
@Controller public class HelloController { @GetMapping("hello") public String hello(Model model){ model.addAttribute("data", "hello!!"); return "hello"; } }
๊ฐ์ฅ ๋จผ์ Annotation(@)์ ํตํด ํด๋น ๋ถ๋ถ์ด Controller์์ ์ ์ธํ๋ค.
@GetMapping์ ๊ฒฝ์ฐ, ์ธ์๋ก ๋ค์ด ์๋ ๊ฐ์ผ๋ก ์์ฒญ์ ๊ตฌ๋ถํ๋ค.
5๋ฒ์งธ ์ค์ data๋ ์์ thymeleaf๋ก ์ ์ธํ data๋ฅผ ์๋ฏธํ๋ฉฐ,
return๊ฐ์ templates ํด๋ ์์ ํด๋น ์ ์ฉํ ํ์ผ ์ด๋ฆ์ ์๋ฏธํ๋ค.
(Controller์์ return์ผ๋ก ๋ฐํํ ํ๋ฉด์ ViewResolver๊ฐ ์ฐพ์์ ์ฒ๋ฆฌํ๋ค)
์ฆ, localhost:8080/hello๋ผ๊ณ ์์ฒญํ๋ฉด hello๊ฐ ์คํ๋์ด, hello.html์ data ๋ถ๋ถ์ hello๋ฅผ ๋ฃ์ด์ ๋ฐํํ๋ค.
๋น๋ ๋ฐ ์คํ
./gradlew clean build ./gradlew build
์์ ๋ช ๋ น์ด๋ก build ํ๋ค
(window์ powershell ํ๊ฒฝ)
cd build/libs java -jar project-name.jar
์ดํ build/libs ํด๋์ ์์ฑ๋ jar ํ์ผ์ ์คํํ๋ฉด ๋๋ค!
์ ์คํ๋๋ localhost:8080๋ ๋ณผ ์ ์๋ค!
'๐ณDev > Dev' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Network] HTTP์ HTTPS (๋น์ฐ๊ฒฐ์ฑ๊ณผ ๋ฌด์ํ) (0) 2022.04.21 [Dev] Build ์ Gradle, Maven (0) 2022.04.15 Bluetooth Low Energy, BLE๋ฅผ ๊ณต๋ถํด๋ณด์ (0) 2022.01.29 Python Comprehension์ ์์๋ณด์ (0) 2022.01.01 React์์ Kakao Map API ์์ํ๊ธฐ (0) 2021.12.31