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

Spring๐Ÿƒ/๊น€์˜ํ•œ์˜ ์Šคํ”„๋ง ์™„์ „์ •๋ณต

์Šคํ”„๋ง ์ž…๋ฌธ ๊ฐ•์˜ ๋…ธํŠธ ์ •๋ฆฌ(4) : ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - ์›น MVC ๊ฐœ๋ฐœ

728x90

ํšŒ์› ๊ด€๋ฆฌ ์˜ˆ์ œ - ์›น MVC ๊ฐœ๋ฐœ

 

1) ํ™ˆ ์ปจํŠธ๋กค๋Ÿฌ ๋ฐ ํ™ˆ html

package hello.helloSpring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

  @GetMapping("/")
  public String home() {
    return "home";
  }
}
<!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> <!-- /container -->
</body>
</html>

*์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์ •์  ํŒŒ์ผ๋ณด๋‹ค ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค!

์š”์ฒญ์ด ์˜ค๋ฉด ๋จผ์ € ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ์žˆ๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰ํ•œ๋‹ค. ์—†๋Š” ๊ฒฝ์šฐ static ํŒŒ์ผ ๊ฒ€์ƒ‰

 

ํšŒ์› ์›น ๊ธฐ๋Šฅ - ๋“ฑ๋ก

์ปจํŠธ๋กค๋Ÿฌ

@Controller
public class MemberController {
 private final MemberService memberService;
 @Autowired
 public MemberController(MemberService memberService) {
 this.memberService = memberService;
 }
 @GetMapping(value = "/members/new")
 public String createForm() {
 return "members/createMemberForm";
 }
}

html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
 <form action="/members/new" method="post"> <!-- 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>

Form ๊ฐ์ฒด : ์›น ํ™”๋ฉด์—์„œ ์ž…๋ ฅํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๊ฐ์ฒด

package hello.hellospring.controller;
public class MemberForm {
 private String name;
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
}

Post ๋งคํ•‘ API

@PostMapping(value = "/members/new")
public String create(MemberForm form) {
 Member member = new Member();
 member.setName(form.getName());
 memberService.join(member);
 return "redirect:/";
}

 

ํšŒ์› ์›น ๊ธฐ๋Šฅ - ์กฐํšŒ

@GetMapping("/members")
public String list(Model model) {
    List<Member> members = memberService.findMembers();
    model.addAttribute("memebers", members);
    return "members/memberList";
}

model.addAttribute() ๋ฉ”์„œ๋“œ ์ •๋ฆฌ

1) (String attributeName, @Nullable Object attributeValue) : ์ด๋ฆ„๊ณผ ๊ฐ’์„ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

Add the supplied attribute under the supplied name.

attributeName – the name of the model attribute (never null) attributeValue – the model attribute value (can be null)

 

2) (Object attributeValue) : ๊ฐ’๋งŒ ๋งค๊ฐœ๋ณ€์ˆ˜์ธ ๊ฒฝ์šฐ

Add the supplied attribute to this Map using a generated name.
Note: Empty Collections are not added to the model when using this method because we cannot correctly determine the true convention name. View code should check for null rather than for empty collections as is already done by JSTL tags.
attributeValue – the model attribute value (never null)

 

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