728x90
1. ํ์ ์น ๊ธฐ๋ฅ - ํ ํ๋ฉด ์ถ๊ฐ
- localhost:8080 ์ผ๋ก ํธ์ถ
- container์์ ๋ค์ง
- localhost:8080/ < ์ปจํธ๋กค๋ฌ ์์
- ์ฐ๊ฒฐ!
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";
}
}
2. ํ์ ์น ๊ธฐ๋ฅ - ๋ฑ๋ก
package hello.hellospring.controller;
import hello.hellospring.domain.Member;
import hello.hellospring.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class MemberController {
private final MemberService memberService;
@Autowired
public MemberController(MemberService memberService) {
this.memberService = memberService;
}
// get mapping -> ๋ฐ์ดํฐ๋ฅผ ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด์ฃผ๋ ๊ฒฝ
@GetMapping("/members/new")
public String createForm() {
return "members/createMemberForm";
}
// post mapping -> ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ๋ฐ๋ ๊ฒฝ์ฐ
@PostMapping("members/new")
public String create(MemberForm form) {
Member member = new Member();
member.setName(form.getName());
memberService.join(member);
return "redirect:/";
}
}
name ๋ง๊ณ ๋ค๋ฅธ ํ๋ ์ฌ๋ฌ๊ฐ ๋ง๋ค์ด์ ์์์ ๋ค์ด๊ฐ๋์ง ์คํํด๋ณด๊ธฐ
package hello.hellospring.controller;
public class MemberForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3. ํ์ ์น ๊ธฐ๋ฅ - ์กฐํ
<!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>
- ${members} ⇒ model๋ก template engine์์ ๋ฐ์์จ ๊ทธ๊ฑฐ
- each ์์์ ๋ฐ๋ณต๋ฌธ์ฒ๋ผ ์ฌ์ฉ๋จ
- jQuery์ for each๋ ๋๊ฐ์
- member.id์ member.name ์ getter๋ก ๋ฐ์์ด
@GetMapping(value = "/members")
public String list(Model model) {
List<Member> members = memberService.findMembers();
model.addAttribute("members", members);
return "members/memberList";
}
728x90
'๐ฅ Web > โ Back-end | Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring ์ ๋ฌธ] 4. ์คํ๋ง ๋น๊ณผ ์์กด๊ด๊ณ (0) | 2023.09.25 |
---|---|
[Spring ์ ๋ฌธ] 3. ํ์ ๊ด๋ฆฌ ์์ - ๋ฐฑ์๋ ๊ฐ๋ฐ (0) | 2023.09.25 |
[Spring ์ ๋ฌธ] 2. ์คํ๋ง ์น ๊ฐ๋ฐ ๊ธฐ์ด (0) | 2023.06.30 |
[Spring ์ ๋ฌธ] 1. ํ๋ก์ ํธ ํ๊ฒฝ ์ค์ (0) | 2023.06.29 |