-추가-
public String list(Model model)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import java.util.List;
@Controller
public class Membercontroller {
private final MemberService memberService;
@Autowired
//Di = 디펜더시 인젝션
public Membercontroller(MemberService memberService){
this.memberService = memberService;
}
@GetMapping("/members/new") //home.html 에있는 회원 가입
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:/";
}
//추가
@GetMapping("/members")
public String list(Model model){
List<Member> members = memberService.findMembers();
model.addAttribute("members",members);
return "members/memberList";
}
}
|
cs |
members/memberList.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!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>
|
cs |
본격적으로 timeleaf 작동한다.
@GetMapping("/members")
public String list(Model model){
List<Member> members = memberService.findMembers();
model.addAttribute("members",members);
return "members/memberList";
}
템플릿 언어에서 ${members} // ${members}"
model 안에서 꺼내온다.
model.addAttribute("members",members);
List로 모든 회원을 다 조회해서 담아놨다.
th:each="member (timeleaf 언어) << 루프를 돈다
루프를 돌면서
<td th:text="${member.id}"></td>
<td th:text="${member.name}"></td>
위 로직을 실행 한다.
아래 결과화면
'프로그래밍 > SpringMVC_기초' 카테고리의 다른 글
회원 관리 예제 - 11(순수 JDBC) (0) | 2023.10.24 |
---|---|
회원관리 예제 - 10(H2 데이터베이스 설치) (0) | 2023.10.23 |
회원관리 예제 - 8 (회원 웹 기능 등록) (2) | 2023.10.22 |
회원관리 예제 - 7 (홈화면 추가하기) (1) | 2023.10.22 |
회원관리 예제-6(자바 코드로 직접 스프링 빈 등록하기) (필드 주입, setter 주입, 생성자 주입) (0) | 2023.10.22 |