[Spring 스터디] 05. 회원 관리 예제 - 웹 MVC 개발

주영진·2024년 11월 20일

Spring 스터디

목록 보기
5/5
post-thumbnail

회원 웹 기능; 홈 화면 추가

위와 같이 HomeController 클래스를 만들어 웹 요청을 처리하는 컨트롤러를 생성한다. @Getmapping 어노테이션을 통해 '/'경로로 들어오는 GET 요청을 해당 메서드와 매핑시켜, home.html 파일을 실행시킨다. (2주차 스프링 스터디 참고)

  • home.html
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org">

<body>

<div class = "container">
  <div>
    <h1>Hello Spring</h1>
    <p>회원 기능</p>
    <p>
      <a href="/members/new">회원 가입</a>
      <a href="/members">회원 목록</a>
    </p>
  </div>
</div>

</body>
</html>
  • localhost8080 실행결과

회원 웹 기능; 등록

  • MemeberForm class
package hello.hello_Spring.Controller;

public class MemberForm {
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}
  • createForm.html
<!DOCTYPE html>
<html xmlns:th = "http://www.thymeleaf.org">

<body>

<div class = "container">
  <form action="/members/new" method="post"></form>
  <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>

회원 웹 기능; 조회

  • 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}">
        <td
            th:text="${member.id}"></td>
      </tr>
      </tbody>
    </table>
  </div>
  th:text="${member.name}"></td>
</div> <!-- /container -->
</body>
</html>
profile
'개발사(社)' (주)영진

0개의 댓글