ํ๋ก์ ํธ ๋ฒ์ ๋ณ๊ฒฝ
Spring Initializr์์ ํ๋ก์ ํธ ์์ฑ ๋ 3.4.0 ๋ฒ์ ์ผ๋ก ์์ฑํ์๋๋ฐ, ๊ต์ฌ๋ 3.1.0 ๋ฒ์ ์ ์ฌ์ฉํ์ฌ ์ํํ ์งํ์ ์ํด ์คํ๋ง๋ถํธ ๋ฒ์ ๋ณ๊ฒฝ์ ํด์ฃผ์์ต๋๋ค. ์คํ๋ง๋ถํธ ๋ฒ์ ์ build.gradle ํ์ผ์ ์์ ํ์ฌ ์ฝ๊ฒ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
์ ๋ถ๋ถ์ 3.1.0์ผ๋ก ๋ณ๊ฒฝํ๊ณ , ์์ ๋ํ๋๋ ์ฝ๋ผ๋ฆฌ ์์ด์ฝ์ ํด๋ฆญํ๋ฉด ๋ฒ์ ๋ณ๊ฒฝ ์๋ฃ์ ๋๋ค.
ํผ ๋ฐ์ดํฐ
HTML ์์์ธ <form> ํ๊ทธ์ ์ค๋ ค ์ ์ก๋๋ ๋ฐ์ดํฐ
<form> ํ๊ทธ๋ ์น ๋ธ๋ผ์ฐ์ ์์ ์๋ฒ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ๋ ์ฌ์ฉํฉ๋๋ค. ์ด๋๋ก(where), ์ด๋ป๊ฒ(how) ๋ณด๋ผ์ง ๋ฑ์ ์ ์ด์, ์๋ฒ์ ์ ์กํ๋ ๊ฒ์ ๋๋ค.
DTO
Data Transfer Object, ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ๋ ๊ฐ์ฒด
์ ๋ ฅ ํผ ์ค์ต
1. ํผ ํ์ด์ง ์์ฑ
resource > templates > articles ํ์์ new.mustache ํ์ผ ์์ฑํ๊ณ , ํผ ์์ฑ
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Document</title>
</head>
<body>
<form>
<input type = "text">
<textarea></textarea>
<button type = "submit" >Submit</button>
</form>
</body>
</html>
java > ํ๋ก์ ํธ๋ช ํด๋ > controller ํ์์ ArticlesController ํด๋์ค ์์ฑํ๊ณ ์ฝ๋ ์์ฑ
package com.example.firstproject.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ArticleController {
@GetMapping("/articles/new")
public String newArticleForm() {
return "articles/new";
}
}
localhost8080:/articles/new ์ ์ํ๋ฉด ์๋์ ๊ฐ์ ํ๋ฉด์ด ๋์ต๋๋ค.
์ผ๋จ ์คํ์ผ๋ง์ ์๋ตํ๊ณ , ํผ ๋ฐ์ดํฐ ์ ์ก ๊ธฐ๋ฅ๋ถํฐ ๊ตฌํํด๋ณด๊ฒ ์ต๋๋ค.
2. ํผ ๋ฐ์ดํฐ ์ ์ก ๊ธฐ๋ฅ
๋ง๋ ํผ์๋ ์ ๋ ฅ ์ฐฝ๊ณผ, ๋ฒํผ์ด ์์ง๋ง ํ ์คํธ๋ฅผ ์ ๋ ฅํ๊ณ ๋ฒํผ์ ์๋ฌด๋ฆฌ ๋๋ฌ๋ดค์ ์๋ฌด๋ฐ ๋์๋ ํ์ง ์์ต๋๋ค. ์ ๋ ฅํ ํ ์คํธ๋ค์ ์ ์กํ๋ ค๋ฉด, action, method ์์ฑ์ ์ค์ ํ์ฌ ์ ์ก ์ฃผ์์ ์ ์ก ๋ฐฉ์์ ์ค์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
๋จผ์ ํผ ํ๊ทธ์ action, method ์์ฑ์ ์ถ๊ฐํด์ค๋๋ค.
<form action="/articles/create" method = "post">
๊ทธ๋ฆฌ๊ณ , controller๋ฅผ ์์ ํด์ค๋๋ค.
@Controller
public class ArticleController {
@GetMapping("/articles/new")
public String newArticleForm() {
return "articles/new";
}
@PostMapping("/articles/create")
public String createArticle() {
return "";
}
}
@PostMapping()์ ์ฌ์ฉํ์ฌ URL ์์ฒญ์ ๋ฐ๊ณ , ์ด์ ๋ํ ๋ฉ์๋์ ๋ฐํ๊ฐ์ ์ถ๊ฐํด์ฃผ์์ต๋๋ค.
3. DTO ์์ฑ
์ปจํธ๋กค๋ฌ์์๋ ํผ ๋ฐ์ดํฐ๋ฅผ DTO์ ๋ด์ ๋ฐ๊ธฐ ๋๋ฌธ์, DTO๋ฅผ ์์ฑํด ์ฃผ์ด์ผ ํฉ๋๋ค.
ํ๋ก์ ํธ๋ช ํด๋ > dto ํจํค์ง๋ฅผ ์์ฑํ๊ณ , ArticleForm ํด๋์ค๋ฅผ ์ถ๊ฐํด์ค๋๋ค.
package com.example.firstproject.dto;
public class ArticleForm {
private String title;
private String content;
//title, content๋ฅผ ํ๋์ ์ ์ฅํ๋ ์์ฑ์
public ArticleForm(String title, String content) {
this.title = title;
this.content = content;
}
@Override
public String toString() {
return "ArticleForm{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
'}';
}
}
4. ํผ ๋ฐ์ดํฐ DTO์ ๋ด๊ณ , ์ ๋ ฅํผ๊ณผ ์ฐ๊ฒฐํ๊ธฐ
public String createArticle(ArticleForm form) {
System.out.println(form.toString());
return "";
}
ArticleForm์ form ๊ฐ์ฒด๋ฅผ creatArticle()์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ ๋ ฅ ํผ์์ ๋ฐ์ดํฐ ํ๋(title, content)๋ฅผ ์ง์ ํ์ฌ ์ ๋ ฅ ํผ๊ณผ DTO ํ๋๋ฅผ ์ฐ๊ฒฐํด์ค๋๋ค.
<form action="/articles/create" method = "post">
<input type = "text" name = "title">
<textarea name = "content"></textarea>
<button type = "submit" >Submit</button>
</form>
์ ๋ ฅ ํผ์ ํ ์คํธ๋ฅผ ์ ๋ ฅํ๊ณ , [Submit]์ ๋๋ฅด๋ฉด, IntelliJ์ ํฐ๋ฏธ๋์์ ๋ฐ์ดํฐ๊ฐ ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์ปจํธ๋กค๋ฌ์์ ํผ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ DTO์ ๋ด๋ ์ฝ๋๋ฅผ ์์ฑํด๋ณด์์ต๋๋ค. ๊ฐ๋จํ ์ค์ต์ด์ง๋ง, form, controller, DTO๊ฐ ๊ด๊ณ ๋๋ฌธ์ ์กฐ๊ธ ๋ ์ ๋ฆฌ๊ฐ ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค...