2 분 소요


1. 회원 웹 기능 - 홈 화면 추가

홈 컨트롤러 추가

controller > HomeController 생성

package hello.hello_project.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {
    @GetMapping("/")
    public String home(){
        return "home";
    }
}


회원 관리용 홈

templates > 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>
    <!-- /container -->
  </body>
</html>



2. 회원 웹 기능 - 등록

2.1 회원 등록 폼 개발

회원 등록 폼 컨트롤러

controller > MemberController

package hello.hello_project.controller;

import hello.hello_project.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@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";
    }
}


회원 등록 폼 HTML

resources > templates > members 디렉토리 생성 > createMemberForm.html 생성 후 아래 코드 복붙

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <body>
    <div class="container">
      <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>
    <!-- /container -->
  </body>
</html>


2.2 회원 등록 컨트롤러

웹 등록 화면에서 데이터를 전달 받을 폼 객체

controller > MemberForm 클래스 생성

package hello.hello_project.controller;

public class MemberForm {
    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
}


회원 컨트롤러에서 회원을 실제 등록하는 기능

package hello.hello_project.controller;

import hello.hello_project.domain.Member;
import hello.hello_project.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@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:/";
    }
}



3. 회원 웹 기능 - 조회

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

package hello.hello_project.controller;

import hello.hello_project.domain.Member;
import hello.hello_project.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
    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";
    }
}


회원 리스트 HTML

templates > members > 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>
              <td th:text="${member.name}"></td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </body>
</html>

메모리에 저장되어있기 때문에 서버 재시작시 데이터가 사라지게 된다.


카테고리:

업데이트:

댓글남기기