본문 바로가기
Study/Java Spring Boot

[Spring] 스프링 입문5 - 회원 웹 화면

회원을 등록 , 조회가 가능한 사이트를 만들고,

지금까지 만든 동작을 연결해보자!

 

일단, 접속했을때 보여줄 화면을 만들자.

1. 홈 화면

접속 컨트롤러

com.example.intro.controller HomeController

package com.example.intro.controller;

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

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

src.main.resources 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. 회원가입

가입은 "회원 가입"을 눌렀을때 연결되는 members/new 로직을 만들자.

 

com.example.intro.controller MemberController 

package com.example.intro.controller;

import java.util.List;

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 com.example.intro.domain.Member;
import com.example.intro.service.MemberService;

@Controller
public class MemberController {
	
	private final MemberService memberService; 

	@Autowired // 구동시 @service로 구동된 MemberService를 가져와 넣어준다
	public MemberController(MemberService memberService) {
		this.memberService = memberService;
	}
	
	@GetMapping("/members/new") // url 접근시 members/createMemberForm 페이지 
	public String createForm() {
		return "members/createMemberForm";
	}	
}

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>

 

createMemberForm 에서 Post방식으로 members/new 에 접근하는 경우,

저장되도록 controller에 추가하자

package com.example.intro.controller;

import java.util.List;

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 com.example.intro.domain.Member;
import com.example.intro.service.MemberService;

@Controller
public class MemberController {
	
	private final MemberService memberService; 

	@Autowired // 구동시 @service로 구동된 MemberService를 가져와 넣어준다
	public MemberController(MemberService memberService) {
		this.memberService = memberService;
	}
	
	@GetMapping("/members/new") // url 접근시 members/createMemberForm 페이지 
	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:/"; 
	}
	
}

@PostMapping 을 통해 submit 버튼으로 값을 넘겨받은것을

객체를 만들어 저장하고,

redirect를 통해 home 화면으로 이동해준다.

com.example.intro.controller MemberForm

package com.example.intro.controller;

public class MemberForm {
	private String name;

	public String getName() {
		return name;
	}

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

넘겨받은 값은 memberForm 객체에 name 필드에 들어가고,

해당 객체를 통해 회원가입을 진행한다.

 

 

3. 회원 조회

홈화면에서 회원조회 버튼을 통해 접근하는 경우,

전체 회원을 조회하여 웹화면에 반환해준다.

package com.example.intro.controller;

import java.util.List;

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 com.example.intro.domain.Member;
import com.example.intro.service.MemberService;

@Controller
public class MemberController {
	
	private final MemberService memberService; 

	@Autowired // 구동시 @service로 구동된 MemberService를 가져와 넣어준다
	public MemberController(MemberService memberService) {
		this.memberService = memberService;
	}
	
	@GetMapping("/members/new") // url 접근시 members/createMemberForm 페이지 
	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();
		// List를  "members/memberList" body에서 members 속성에 반환
		model.addAttribute("members",members);
		return "members/memberList";
	}
	
}

 

모든 객체를 조회하여 List를  아래 html에서 members속성에 반환

src.main.resources members/memberList.html

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

forEach를 통해 하나씩 출력