members/createMemberForm / 회원 등록 폼
<!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>
</body>
</html>
- form태그 안에 이름을 입력받아서 /member/new 컨트롤러를 찾아감
전달받을 폼 데이터의 객체
package com.danny.learningspring.controller;
public class MemberForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MemberController / 회원 등록 폼 컨트롤러
package com.danny.learningspring.controller;
import com.danny.learningspring.domain.Member;
import com.danny.learningspring.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
@RequiredArgsConstructor
public class MemberController {
private final 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:/";
}
}
- Mapping의 url은 같지만 porm태그의 method를 post로 지정해줬기 때문에 PostMapping을 찾아옴
- parameter로 받은 form의 name을 member객체에 담아서 MemberService에 있는 Repository를 찾아가서 insert함
'Spring 입문' 카테고리의 다른 글
스프링MVC 입문18 (0) | 2022.11.11 |
---|---|
스프링MVC 입문17 (0) | 2022.11.11 |
스프링MVC 입문15 (0) | 2022.11.11 |
스프링MVC 입문14 (0) | 2022.11.11 |
스프링MVC 입문13 (0) | 2022.11.11 |
댓글