์คํ๋ง ์น ๊ฐ๋ฐ ๋ฐฉ๋ฒ
์ ์ ์ปจํ ์ธ ; ํ์ผ์ ๊ทธ๋๋ก ์น๋ธ๋ผ์ฐ์ ์ ๋ด๋ ค์ฃผ๋ ๊ฒ
MVC์ ํ ํ๋ฆฟ ์์ง ; ์๋ฒ์์ HTML์ ํ๋ก๊ทธ๋๋ฐํด์ ๋์ ์ผ๋ก ๋ด๋ฆฌ๋ ๊ฒ
API ; JSON์ด๋ผ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ ํฌ๋งท์ผ๋ก ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์
์ ์ ์ปจํ ์ธ
# ์ ์ ์ปจํ ์ธ ๊ธฐ๋ฅ
resources/static/hello-static.html
<!DOCTYPE HTML>
<html>
<head>
<title>static content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
์ ์ ์ปจํ
์ธ ์
๋๋ค.
</body>
</html>
> hello-static.html ํ์ผ ์์ฑ ํ ํ๋ก์ ํธ ์คํ
localhost:8080/hello-static.html ์ ์ ์ ์์ ๊ฐ์ ํ๋ฉด์ด ์ถ๋ ฅ๋๋ค.
# ์ ์ ์ปจํ ์ธ ๋์ ํ๊ฒฝ
> ์น ๋ธ๋ผ์ฐ์ ์์ localhost:8080/hello-static.html ์ ์
> ๋ด์ฅ ํฐ์ผ ์๋ฒ๊ฐ ์์ฒญ์ ๋ฐ์ Spring์๊ฒ ์ ๋ฌ
> Spring์ด ์ปจํธ๋กค๋ฌ ์ชฝ์ hello-static์ด ์๋์ง ๋จผ์ ์ฐพ์๋ด
(์ปจํธ๋กค๋ฌ๊ฐ ์ฐ์ ์์๋ฅผ ๊ฐ์ง)
> ๋งคํ์ด ๋ hello-static ์ปจํธ๋กค๋ฌ๊ฐ ์์ผ๋ฏ๋ก resources์ static/hello-static.html์ ๋ฐํ
์ ์ ์ปจํ ์ธ ๋ฐฉ์์ ์ ์ธํ๊ณ ๋
MVC์ฒ๋ผ View๋ฅผ ์ฐพ์์ ํ ํ๋ฆฟ ์์ง์ ํตํด ํ๋ฉด์ ๋ ๋๋งํด์ HTML์ ์น ๋ธ๋ผ์ฐ์ ์ ๋๊ฒจ์ฃผ๋ ๋ฐฉ์๊ณผ
API๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ฐ๋ก ํด๋ผ์ด์ธํธ์๊ฒ ๋๊ฒจ์ฃผ๋ ๋ฐฉ์์ผ๋ก ๋๋จ
MVC์ ํ ํ๋ฆฟ ์์ง
- MVC : Model, View, Controller
View์์ ๋ชจ๋ ๊ฒ์ ํ๋ก๊ทธ๋๋ฐํ๋ Model1 ๋ฐฉ์ > View์ Controller๊ฐ ๋ถ๋ฆฌ๋ MVC ๋ฐฉ์์ผ๋ก ๋ณํ
View : ํ๋ฉด์ ๊ทธ๋ฆฌ๋ ๋ฐ์ ๋ชจ๋ ์ญ๋์ ์ง์ค
Controller : ๋น์ฆ๋์ค ๋ก์ง๊ณผ ๊ด๋ จ์ด ์๊ฑฐ๋ ๋ด๋ถ์ ์ธ ๊ฒ๋ค์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ง์ค
# MVC ๊ธฐ๋ฅ
Controller
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
//์๋ ์ฝ๋ ์ถ๊ฐ
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
resources/templates/hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
> ์ ๋ ์ฝ๋๋ฅผ ์ ์ฉํ๋๋ฐ localhost:8080/hello-mvc์ ์ ์ํด๋ณด๋ฉด ์๋ฌ ํ์ด์ง๊ฐ ๋จ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
> ์๋ฌ๊ฐ ๋ด๋ค๋ฉด ๋ก๊ทธ๋ฅผ ๋ฐ๋์ ํ์ธํด์ผ ํ๋ค. WARN์ด ๋ณด์ด๋ ๋ถ๋ถ์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
> ๋ก๊ทธ๋ฅผ ์์ธํ ๋ณด๋ฉด Parameter ๊ฐ์ ์ฃผ์ง ์์์ ์๊ธด ์ค๋ฅ์์ ์ ์ ์๋ค.
# Command + P๋ฅผ ํ์ฉํ ํ๋ผ๋ฏธํฐ ์ ๋ณด ํ์ธ
Command + P๋ฅผ ํตํด Parameter ์ ๋ณด๋ฅผ ํ์ธํด๋ณด๋ value() ํน์ name()์ด ์๊ณ ,
required()์ default๊ฐ true์์ ์ ์ ์๋ค.
true์ธ ๊ฒฝ์ฐ์๋ parameter ๊ฐ์ ๋ฐ๋์ ์ ๋ ฅํด์ค์ผ ์๋ฌ๊ฐ ๋์ง ์๋๋ค.
> ๋๋ name์ spring!!!์ด๋ผ๋ ๊ฐ์ ๋ฃ์ด์คฌ๋ค.
์๋ก๊ณ ์นจ ํ๋ฉด ์์ ๊ฐ์ด ์ ์์ ์ผ๋ก ์คํ๋๋ค.
# MVC, ํ ํ๋ฆฟ ์์ง ๋์ ํ๊ฒฝ
> ๋ด์ฅ ํฐ์ผ ์๋ฒ๊ฐ ์น ๋ธ๋ผ์ฐ์ ๋ก๋ถํฐ ์์ฒญ์ ๋ฐ์ Spring์๊ฒ ์ ๋ฌ
> helloController์ hello-mvc ๋ฉ์๋์ ๋งคํ์ด ๋์ด ์์ผ๋ฏ๋ก ํด๋น ๋ฉ์๋ ํธ์ถ
(name์ ๋ฃ์ ๊ฐ๋ ํจ๊ป Spring์ ๋ฐํ)
> Spring์ด viewResolver๋ฅผ ํตํด templates/hello-template.html์ ์ฐพ์ Thymeleaf ํ ํ๋ฆฟ์ ์ฒ๋ฆฌ ์์ฒญ
> ํ ํ๋ฆฟ ์์ง์์ ๋ณํํ HTML์ ์น ๋ธ๋ผ์ฐ์ ์ ํ์
API
# API ๊ธฐ๋ฅ
[ String ๋ฐํ ]
//Controller์ ์๋ ์ฝ๋ ์ถ๊ฐ
@GetMapping("hello-string")
@ResponseBody
public String helloString(@RequestParam("name") String name) {
return "hello " + name; // "hello spring"
}
@ResponseBody : ViewResolver๋ฅผ ์ฌ์ฉํ์ง ์๊ณ HTTP์ BODY์ "hello " + name ๋ถ๋ถ์ ์ง์ ๋ฃ์ด์ค
**HTML์ด ์๋ HTTP์ ์๋ต ๋ฐ๋์ด๋ฏ๋ก ํผ๋ํ์ง ๋ง ๊ฒ
[ ๊ฐ์ฒด ๋ฐํ ]
//Controller์ ์๋ ์ฝ๋ ์ถ๊ฐ
@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;
}
}
@ResponseBody : ๊ฐ์ฒด๋ฅผ JSON ํํ๋ก ๋ณํ
[ String ๋ฐํ vs ๊ฐ์ฒด ๋ฐํ ]
[ ํ ํ๋ฆฟ ์์ง vs API ํ์ด์ง ์์ค ๋น๊ต ]
ํ ํ๋ฆฟ ์์ง์ HTML ํ๊ทธ๋ค์ด ๋ณด์ด๋ ๋ฐ๋ฉด API ์ฌ์ฉ ์์๋ HTML ํ๊ทธ ์์ด ๋ด๊ฐ ์ ์ ๋ฌธ์์ด์ด ๊ทธ๋๋ก ๋ํ๋จ
์ด๋, ํ ํ๋ฆฟ ์์ง๊ณผ์ ์ฐจ์ด์ ์ View ์์ด return ๋ถ๋ถ์ด ๊ทธ๋๋ก ์น ๋ธ๋ผ์ฐ์ ์ ํ์๋๋ค๋ ์ ์ด๋ค.
# API ๋์ ํ๊ฒฝ
> @ResponseBody ์ฌ์ฉ ์ HTTP์ BODY์ ๋ฌธ์ ๋ด์ฉ์ ์ง์ ๋ฐํ
viewResolver ๋์ ์ HttpMessageConverter ๊ฐ ๋์
๊ธฐ๋ณธ ๋ฌธ์์ฒ๋ฆฌ: StringHttpMessageConverter๊ฐ ๋ด๋น
๊ธฐ๋ณธ ๊ฐ์ฒด์ฒ๋ฆฌ: MappingJackson2HttpMessageConverter๊ฐ ๋ด๋น
(๊ฐ์ฒด ๋ฐํ ์ ๊ธฐ๋ณธ ์ ์ฑ : JSON ํ์์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ง๋ค์ด์ HTTP ์๋ต์ ๋ฐํ)
๋ด์ฉ ๋ฐ ์ด๋ฏธ์ง ์ถ์ฒ : ๊น์ํ์ ์คํ๋ง ์ ๋ฌธ