본문 바로가기
Spring

[스프링 입문] 회원 웹 기능 - 조회

by 박성민 2021. 8. 31.

회원 컨트롤러에서 조회 기능

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 {

    ...

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

}

(MemberController.java)

회원 리스트 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}">
                <td th:text="${member.id}"></td>
                <td th:text="${member.name}"></td>
            </tr>
            </tbody>
        </table>
    </div>
</div> <!-- /container -->
</body>
</html>

(memberList.html)

  • ${members}
    • Model 안의 member 값을 가져옵니다.
    • Java의 foreach 문법과 비슷합니다.
  • ${member.id}
    • Member 클래스의 getName()을 통해 name 값을 가져옵니다.

1
(회원 목록 조회 화면)

참조

댓글