Spring

spring - 회원관리 웹 mvc개발

팅탱팅탱 2024. 3. 4. 02:54

우선 homeController을 만들어서 localhost:8080으로 들어가면 바로 home.html이라는 페이지가 뜨게 만들어줍니다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div>
        <h1>hello spring</h1>
        <p>회원 기능</p>
        <p>
            <a href="/members/new">회원가입</a>
            <a href="/members">회원 목록</a>
        </p>
    </div>

</body>
</html>

이런식으로 만약 회원가입이나 회원 목록을 누르게된다면 해당 경로로 이동하게 만들어줍니다.

여기서 우선 회원가입 기능부터 추가할것입니다.

 

전에 만들어놨던 멤버 컨트롤러에서 만약 "/members/new"라는 경로로 이동했을시에 

템플릿 폴더 하위에있는 members/createMemberForm이라는 html이 실행되도록 해줍니다.

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <form action="/members/new" method="post">
        <div class="form-group">
            <label for="name">이름</label>
            <input type="text" id="name" name="name" placeholder="이름을 입력하세요">
        </div>
        <button type="submit">등록</button>
    </form>
</div>

</body>
</html>

여기서 폼 액션으로 해당 경로에 포스트로 name이라는 벨류로 사용자가 입력한 값을 보내줍니다.

그렇다면 저기서 보낸 것을 post매핑으로 받아와서 setName메서드를 통해 form에 있는 이름을 getName메서드로 넘겨줍니다.

그 후 join메서드를 통해 회원가입을 시켜주고 "/"경로로 리다이렉팅 시켜줍니다.

멤버 폼 클래스

 

이렇게 되면 회원가입기능은 구현이됐고 이제 회원 조회 기능을 구현합니다.

이 조회기능은 그냥 get매핑으로 해당 url에 접근시 finMembers라는 모든 회원을 조회하는 메서드를 통해 리스트로 받아오고 addattribute를 통하여 해당 return에 있는 html파일에 정보들을 넘겨줍니다.

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <table>
        <thead>
        <tr>
            <th>#</th>
            <th>이름</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="member : ${members}">
            <td th:text="${member.id}">
            <td th:text="${member.name}">

        </tbody>
    </table>
</div>

</body>
</html>

 

이렇게되면 제대로 회원 가입 기능 및 조회기능이 나오는걸 볼 수 있습니다.