-
Notifications
You must be signed in to change notification settings - Fork 1
입문 ‐ 섹션 5. 회원 관리 예제 ‐ 웹 MVC 개발
영주 edited this page May 3, 2024
·
3 revisions
HomeController
@Controller
public class HomeController {
@GetMapping("/")
public String home() {
return "home";
}
}
- 스프링 컨테이너에서 먼저 찾고 맵핑된 URL이 없으면 정적 컨텐츠에서 찾음
=> 이로 인해 index.html이 무시됨
MemberController에 추가
@GetMapping("/members/new")
public String createForm() {
return "members/createMemberForm";
}
@PostMapping("/members/new")
public String create(MemberForm form) {
Member member = new Member();
member.setName(form.getName());
memberService.join(member);
return "redirect:/"; // 홈화면으로 리다이렉트
}
controller에 MemberForm 만듦
public class MemberForm {
private String name;
public String getName() {
return name;
}
public void setName() {
this.name = name;
}
}
createMemberForm.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="container">
<form action="/members/new" method="post"> <!--/members/new에 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>
MemoryController
@GetMapping(value = "/members")
public String list(Model model) {
List<Member> members = memberService.findMembers();
model.addAttribute("members", members);
return "members/memberList";
}
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}"> <!-- thymeleaf의 반복문 문법-->
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
</tr>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
- 단축키
-
Command+E
: 최근 연 파일 확인
-