๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒฟ Spring

[์ž…๋ฌธ] ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ ์„ค์ •

by nitronium102 2021. 6. 28.

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

https://start.spring.io/ ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •ํ•œ ํ›„ ๋‹ค์šด๋ฐ›์•„ IntelliJ๋กœ ์—ด์–ด์ค€๋‹ค.

dependency์—์„œ 

spring web์€ ์›น์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ์ด๊ณ  thymeleaf๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„์˜ ์ผ์ข…์ด๋‹ค. 

 

๋‹ค์šด๋ฐ›์€ ํŒŒ์ผ์—์„œ gradle file์„ ์—ด์–ด์ค€๋‹ค.

 

ํŒŒ์ผ ๊ตฌ์กฐ

  • main-java : ์‹ค์ œ ํŒŒ์ผ
  • test : test ํŒŒ์ผ
  • resources : ์„ค์ • ํŒŒ์ผ(์ž๋ฐ” ํŒŒ์ผ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€)
  • build.gradle : ๋ฒ„์ „ ์„ค์ • ๋ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋งํฌ
  • repositories : mavenCentral - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์ดํŠธ(ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ๋‹ค์šด๋ฐ›๋Š”๋‹ค)

 

์„ค์ •

settings - gradle ๊ฒ€์ƒ‰ํ•œ ํ›„ ๊ธฐ๋ณธ ์—”์ง„์„ gradle์ด ์•„๋‹Œ IntelliJ IDEA๋กœ ์„ค์ •ํ•œ๋‹ค. gradle์„ ํ†ตํ•˜์ง€ ์•Š๊ณ  IDE ์ž์ฒด์—์„œ ์‹คํ–‰ํ•˜๋ฏ€๋กœ ์‹คํ–‰ ์†๋„๊ฐ€ ๋” ๋นจ๋ผ์ง„๋‹ค. 

 

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ดํŽด๋ณด๊ธฐ

gradle์€ ์˜์กด๊ด€๊ณ„๊ฐ€ ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ•จ๊ป˜ ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

1. spring-boot-starter-web

  • spring-boot-starter-tomcat : ํ†ฐ์บฃ(์›น์„œ๋ฒ„)
  • spring-webmvc : ์Šคํ”„๋ง ์›น MVC

2. spring-boot-starter-thymeleaf : ํƒ€์ž„๋ฆฌํ”„ ํ…œํ”Œ๋ฆฟ ์—”์ง„(view)

3. spring-boot-starter(๊ณตํ†ต) : ์Šคํ”„๋ง ๋ถ€ํŠธ + ์Šคํ”„๋ง ์ฝ”์–ด + logging

  • spring-boot -> spring-core
  • spring-boot-starter-logging -> logback, slf4j

ํ…Œ์ŠคํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

- spring-boot-starter-test

  • junit : ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ
  • mockito : ๋ชฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • assertj : ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • spring-test : ์Šคํ”„๋ง ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์ง€์›

 

View ํ™˜๊ฒฝ์„ค์ •

- ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” src/main/static/index.html์— Welcome page ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

01. static/index.html์— ์†Œ์Šค์ฝ”๋“œ ๋„ฃ์–ด์„œ Welcome page ๊ตฌํ˜„ํ•˜๊ธฐ

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

๋งํฌ๊ฐ€ ๊ฑธ๋ฆฌ์ง€๋งŒ ๋งํฌ๋กœ ๋“ค์–ด๊ฐ€๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚œ๋‹ค.

 

02. localhost:8080/hello ํŽ˜์ด์ง€ ๊ตฌํ˜„ํ•˜๊ธฐ

 

/* hello.hellospring/controller/HelloController */
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") // GET : ๊ฐ€๋ ค๋Š” ์ฃผ์†Œ์ฐฝ
    public String hello(Model model){ // MVC์—์„œ์˜ model
        model.addAttribute("data", "hello!!");
        return "hello"; // resources/templates/hello.html๋กœ ์ „๋‹ฌํ•˜์—ฌ ๋ Œ๋”๋งํ•ด๋ผ.
    }
}

๋‹ค์Œ์œผ๋กœ resources/templates์— localhost:8080/hello๋ฅผ ๋‚˜ํƒ€๋‚ผ hello.html์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.

<!DOCTYPE HTML>
<html xlmns: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> // Controller์˜ model์—์„œ data ๊ฐ€์ ธ์˜จ๋‹ค
</body>
</html>

 

 

์ž‘๋™ ์›๋ฆฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

01. ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ํŠน์ • URL์„ ๊ฐ€์ง€๊ณ  ์„œ๋ฒ„๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

02. ์„œ๋ฒ„์—์„œ GET ๋ฐฉ์‹์˜ hello์™€ ๋งค์นญํ•œ ํ›„ Controller๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

03. ์Šคํ”„๋ง์ด Model์„ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ’์„ ๋„ฃ์–ด๋†“์€ ํ›„ "hello"๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ์—ฌ๊ธฐ์—์„œ hello๋ž€ resources/templates์— ์žˆ๋Š” hello.html์„ ๋งํ•œ๋‹ค.

04. view์—์„œ thymeleaf์˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•˜์—ฌ hello.html์„ ๋ Œ๋”๋งํ•œ๋‹ค.

 

๋‹ค์‹œ ํ•œ๋ฒˆ ์ •๋ฆฌํ•ด๋ณด์ž.

์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ„(viewResolver)๊ฐ€ ํ™”๋ฉด์„ ์ฐพ์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.

- ์Šคํ”„๋ง๋ถ€ํŠธ ํ…œํ”Œ๋ฆฟ์—”์ง„ viewName ๋งคํ•‘

-> `resources:templates/` + {ViewName} + `.html`

 

์ฐธ๊ณ ) spring-boot-devtools ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด `html` ํŒŒ์ผ์„ ์ปดํŒŒ์ผ๋งŒ ํ•ด์ฃผ๋ฉด ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ์—†์ด View ํŒŒ์ผ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. (IntelliJ ์ปดํŒŒ์ผ ๋ฐฉ๋ฒ• : ๋ฉ”๋‰ด build -> Recompile)

https://dev-minji.tistory.com/16

 

spring-boot-devtools ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€ํ•˜๊ธฐ

๊ฐ•์˜ ๋“ฃ๋‹ค๊ฐ€ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ปดํŒŒ์ผ๋งŒ ํ•˜๋ฉด ์„œ๋ฒ„ ์žฌ์‹œ์ž‘ ์—†์ด view ํŒŒ์ผ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ํ•ด์„œ ์•Œ์•„๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค. https://docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/html/using-boot..

dev-minji.tistory.com

๋Œ“๊ธ€