Framework/Spring Boot

회원 웹 기능 - 조회

잔망루피 2020. 12. 12. 20:40
package start.hellospring.controller;

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 start.hellospring.domain.Member;
import start.hellospring.service.MemberService;

import java.util.List;

@Controller
public class MemberController {
    private final MemberService memberService;

    @Autowired
    public MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @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:/";
    }
    
    @GetMapping("/members")
    public String list(Model model){
        List<Member> members=memberService.findMembers();
        model.addAttribute("members", members);
        return "members/memberList";
    }
}

 

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

</body>
</html>

 

th는 thymeleaf 문법으로 반복문임.

<tr th:each="member : ${members}">는 member에서 값을 꺼내서 member에 담는다.

<td th:text="${member.id}"></td>와 <td th:text="${member.name}"></td>는 출력

 

실행 결과

 

반응형

'Framework > Spring Boot' 카테고리의 다른 글

스프링 데이터 JPA  (0) 2020.12.19
JPA  (0) 2020.12.13
회원 웹 기능 - 등록  (0) 2020.12.12
회원 웹 기능 - 홈 화면 추가  (0) 2020.12.12
스프링 빈과 의존관계  (0) 2020.12.12