Search

[springboot] ์Šคํ”„๋ง ์ž…๋ฌธ - 5. ํšŒ์› ๊ด€๋ฆฌ View

ํƒ€์ž…
์Šคํ„ฐ๋””
ํƒœ๊ทธ
springboot
์ƒํƒœ
Published
์ƒ์„ฑ์ผ
2022/07/22 09:45
์ตœ์ข… ํŽธ์ง‘ ์ผ์‹œ
2023/02/27 03:14
์ƒ์œ„ ํ•ญ๋ชฉ
1 more property
์ธํ”„๋Ÿฐ ๊น€์˜ํ•œ๋‹˜ ์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค

ํ™ˆ ํ™”๋ฉด

HomeController ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
controller/HomeController.java
@Controller public class HomeController { @GetMapping("/") public String home(){ return "home"; } }
Java
๋ณต์‚ฌ
ย / ์œผ๋กœ GET ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ์‹œ templates/home.html ์„ ๋ฆฌํ„ดํ•œ๋‹ค.
ย ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ •์ ํŒŒ์ผ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์œผ๋ฏ€๋กœ ์ •์ ํŒŒ์ผ์„ ๋ฌด์‹œํ•˜๊ณ  ํ…œํ”Œ๋ฆฟ ์—”์ง„์ด ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.
์ด์ œ ๊ทธ์— ํ•ด๋‹นํ•˜๋Š” View ๋ฅผ ๋งŒ๋“ ๋‹ค.
templates/home.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <body> <div class="container"> <div> <h1>Hello Spring</h1> <p>ํšŒ์› ๊ธฐ๋Šฅ</p> <p> <a href="/members/new">ํšŒ์› ๊ฐ€์ž…</a> <a href="/members">ํšŒ์› ๋ชฉ๋ก</a> </p> </div> </div> </body> </html>
HTML
๋ณต์‚ฌ
๊ฒฐ๊ณผ

ํšŒ์› ๋“ฑ๋ก ํ™”๋ฉด

form html
ํšŒ์›์„ ๋“ฑ๋กํ•˜๋Š” form ์— ๊ด€ํ•œ view ํ…œํ”Œ๋ฆฟ ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.
resources/templates/members/createMemberForm.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <body> <div class="container"> <form action="/members/new" method="post"> <div class="form-group"> <label for="name">์ด๋ฆ„</label> <input type="text" id="name" name="name" placeholder="์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”"> </div> <button type="submit">๋“ฑ๋ก</button> </form> </div> <!-- /container --> </body> </html>
HTML
๋ณต์‚ฌ
MemberForm ๋ฐ์ดํ„ฐ์— ๊ด€ํ•œ ํด๋ž˜์Šค ์ƒ์„ฑ
controller/MemberForm.java
public class MemberForm { private String name; // ๋ง˜๋Œ€๋กœ ์ง€์–ด๋„ ๋จ // getter ๋ฉ”์„œ๋“œ๋ช…์€ getName ์œผ๋กœ ํ•ด์•ผํ•จ public String getName() { return name; } // setter ๋ฉ”์„œ๋“œ๋ช… setName ์œผ๋กœ ํ•ด์•ผํ•จ public void setName(String name) { this.name = name; // createMemberForm.html ์— ์žˆ๋Š” id = "name" ์ธ input ์—์„œ ๊ฐ’ ๋„˜์–ด์˜ด } }
Java
๋ณต์‚ฌ
ย ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์ด๋ฆ„์€ ๋งˆ์Œ๋Œ€๋กœ ์ง€์–ด๋„ ์ƒ๊ด€์—†์ง€๋งŒ, getter/setter ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ HTML ๋‹จ์—์„œ ๋„˜์–ด์˜ค๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ์ด๋ฆ„์— ๋งž๊ฒŒ๋” ์ง€์–ด์ค˜์•ผ ํ•œ๋‹ค.
name ์†์„ฑ์˜ ๊ฐ’์ด username ์ด๋ผ๋ฉด โ‡’ getUsername(), setUsername()
์ฐธ๊ณ 
MemberController
MemberController ์—์„œ /members/new GET ์š”์ฒญ๊ณผ createMemberForm.html ์„ ๋งคํ•‘ํ•ด์ค€๋‹ค.
controller/MemberController.java
@GetMapping("/members/new") public String createForm(){ return "members/createMemberForm"; }
Java
๋ณต์‚ฌ
form ์—์„œ POST ๋กœ ๋„˜์–ด์˜ค๋Š” form ๋‚ด์šฉ์„ ๋ฐ›์•„์„œ DB์— ์ €์žฅํ•œ๋‹ค.
@PostMapping("/members/new") public String create(MemberForm form){ Member member = new Member(); // form ์— getName() ์œผ๋กœ ๊บผ๋‚ด์„œ member์— Name set member.setName(form.getName()); memberService.join(member); return "redirect:/"; }
Java
๋ณต์‚ฌ
ย html ์˜ form ์„ ํ†ตํ•ด POST ์š”์ฒญ ์‹œ, http ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ์žˆ๋Š” form ๋ฐ์ดํ„ฐ๋ฅผ ์Šคํ”„๋ง ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์•Œ์•„์„œ ํ•ด์„ํ•˜์—ฌ ์ž๋™์œผ๋กœ form ๊ฐ์ฒด์— ๋„ฃ์–ด์ค€๋‹ค.
ย HTML ์—์„œ ๋„˜์–ด์˜ค๋Š” name ์†์„ฑ์˜ ๊ฐ’๊ณผ getter, setter ๋ฉ”์„œ๋“œ๋ช…๊ณผ ๋งคํ•‘ ์‹œํ‚ค๋Š” ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”์ธ๋”ฉํ•ด์ค€๋‹ค.
โ€ข
์˜ˆ์‹œ
โ—ฆ
HTML name ์†์„ฑ : username
โ—ฆ
getter ๋ฉ”์„œ๋“œ๋ช… : getUsername()
โ—ฆ
setter ๋ฉ”์„œ๋“œ๋ช… : setUsername()
์ด์—ฌ์•ผ ํ•œ๋‹ค!

ํšŒ์› ์กฐํšŒ

ํ…œํ”Œ๋ฆฟ ๋ทฐ
templates/members/memberList.html
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <body> <div class="container"> <div> <table> <thead> <tr> <th>#</th> <th>์ด๋ฆ„</th> </tr> </thead> <tbody> <tr th:each="member : ${members}"> <td th:text="${member.id}"></td> <td th:text="${member.name}"></td> </tr> </tbody> </table> </div> </div> <!-- /container --> </body> </html>
HTML
๋ณต์‚ฌ
์ปจํŠธ๋กค๋Ÿฌ
MemberController ์—์„œ /members GET ์š”์ฒญ์ด ๋“ค์–ด์˜ฌ ๋•Œ์˜ ๋กœ์ง ์ž‘์„ฑ
@GetMapping("/members") public String list(Model model) { List<Member> members = memberService.findMembers(); // ํ…œํ”Œ๋ฆฟ์—”์ง„์˜ "members" ์†์„ฑ์— members ๋‹ด์•„์„œ ๋ฟŒ๋ ค์คŒ model.addAttribute("members",members); return "members/memberList"; }
Java
๋ณต์‚ฌ
ย memberService ์˜ findMembers() ๋ฉ”์„œ๋“œ๋กœ ์ „์ฒด ํšŒ์›์„ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ํ…œํ”Œ๋ฆฟ ์—”์ง„์— ๋ฟŒ๋ ค์ค€๋‹ค.
๊ฒฐ๊ณผ