๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

WINK-(Web & App)/Spring Boot ์Šคํ„ฐ๋””

[Spring Boot ์Šคํ„ฐ๋””] ํ™ฉํ˜„์ง„ #1 ์ฃผ์ฐจ - ์„น์…˜ 1, 2 "๐Ÿ‘‹ ๐ŸŒธ"

๋ฐ˜์‘ํ˜•

๋‹ค๋“ค ๊ธด์žฅํ•ด์ฃผ์„ธ์š”

 

๐Ÿ“Œ View ํ™˜๊ฒฝ์„ค์ •

1๏ธโƒฃ welcome page ๋งŒ๋“ค๊ธฐ

src / main / resources / 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>

 

2๏ธโƒฃ thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„

thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•˜๋ฉด ๋™์ ์ธ ํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

@Controller
public class HelloController {
 @GetMapping("hello")
 public String hello(Model model) {
 model.addAttribute("data", "hello!!");
 return "hello";
 }
}
<!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>

 

 

๐Ÿ“Œ ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

  1.  ์ฝ˜์†”์ฐฝ์„ ํ‚จ๋‹ค.
  2.  ./gradlew build ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
  3.  cd bulid/libs ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.
  4.  java -jar hello-spring-0.0.1-SNAPSHOT.jar ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•œ๋‹ค.

 

 

๐Ÿ“Œ ์ •์  ์ปจํ…์ธ 

์œ„์—์„œ welcome page๋ฅผ ๋งŒ๋“ค์—ˆ๋˜ ๊ฒƒ์ฒ˜๋Ÿผ ์„œ๋ฒ„์—์„œ ํ•˜๋Š” ์ผ ์—†์ด ํŒŒ์ผ์„ ์›น๋ธŒ๋ผ์šฐ์ €์— ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค์ฃผ๋Š” ๋ฐฉ์‹
(= ํŒŒ์ผ์„ ๊ทธ๋Œ€๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฃผ๋Š” ๊ฒƒ)

<!DOCTYPE HTML>
<html>
<head>
 <title>static content</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
์ •์  ์ปจํ…์ธ  ์ž…๋‹ˆ๋‹ค.
</body>
</html>

 

 

๐Ÿ“Œ MVC์™€ ํ…œํ”Œ๋ฆฟ ์—”์ง„

HTML์„ ๊ทธ๋Œ€๋กœ ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์„œ๋ฒ„์—์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•ด์„œ HTML์„ ๋™์ ์œผ๋กœ ๋ฐ”๊พธ์–ด ๋‚ด๋ ค์ฃผ๋Š” ๋ฐฉ์‹
(= ์„œ๋ฒ„์—์„œ html์„ ์ˆ˜์ •ํ•ด์„œ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ฃผ๋Š” ๊ฒƒ)

 

๐Ÿ’ก ์—ฌ๊ธฐ์„œ MVC๋ž€? Model, View, Controller๋ฅผ ๋งํ•œ๋‹ค.

 

- Controller

@Controller
public class HelloController {
 @GetMapping("hello-mvc")
 public String helloMvc(@RequestParam("name") String name, Model model) {
 model.addAttribute("name", name);
 return "hello-template";
 }
}

- View

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

 

 

๐Ÿ“Œ API

JSON์ด๋ผ๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ ํฌ๋งท์œผ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹ (์„œ๋ฒ„๋ผ๋ฆฌ ํ†ต์‹ ํ•  ๋•Œ ์‚ฌ์šฉ)

 

๐Ÿ’ก ์—ฌ๊ธฐ์„œ @ResponseBody๋ž€?

  • html์— ๋‚˜์˜ค๋Š” body ํƒœ๊ทธ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ http์˜ body ๋ถ€๋ถ„์˜ ๋ฌธ์ž ๋‚ด์šฉ์„ ์ง์ ‘ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
  • view ์ด๋Ÿฐ ๊ฑฐ ์—†๊ณ  ๊ทธ ๋ฌธ์ž๊ฐ€ ๊ทธ๋Œ€๋กœ ๋‚ด๋ ค๊ฐ„๋‹ค.
    (์›น ์‚ฌ์ดํŠธ์—์„œ ์†Œ์Šค๋ฅผ ๋ณด๋ฉด html ์ฝ”๋“œ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌธ์ž๊ฐ€ ๊ทธ๋Œ€๋กœ ๋ณด์ด๊ฒŒ ๋œ๋‹ค.)

- @ResponseBody ๋ฌธ์ž ๋ฐ˜ํ™˜

@Controller
public class HelloController {
 @GetMapping("hello-string")
 @ResponseBody
 public String helloString(@RequestParam("name") String name) {
 return "hello " + name;
 }
}

- @ResponseBody ๊ฐ์ฒด ๋ฐ˜ํ™˜

@Controller
public class HelloController {
 @GetMapping("hello-api")
 @ResponseBody
 public Hello helloApi(@RequestParam("name") String name) {
 Hello hello = new Hello();
 hello.setName(name);
 return hello;
 }
 static class Hello {
 private String name;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 }
}

 

๋ฐ˜์‘ํ˜•