diff --git a/SpringBoot-Introduction/Lectures/Contents.md b/SpringBoot-Introduction/Lectures/Contents.md index 5d3629f..16d8b6c 100644 --- a/SpringBoot-Introduction/Lectures/Contents.md +++ b/SpringBoot-Introduction/Lectures/Contents.md @@ -23,4 +23,5 @@ - [자바 코드로 직접 스프링 빈 등록하기](https://github.com/banjjoknim/TIL/blob/master/SpringBoot-Introduction/Lectures/Lecture14.md) ## 회원 관리 예제 - 웹 MVC 개발 -- [회원 웹 기능 - 홈 화면 추가](https://github.com/banjjoknim/TIL/blob/master/SpringBoot-Introduction/Lectures/Lecture15.md) \ No newline at end of file +- [회원 웹 기능 - 홈 화면 추가](https://github.com/banjjoknim/TIL/blob/master/SpringBoot-Introduction/Lectures/Lecture15.md) +- [회원 웹 기능 - 등록](https://github.com/banjjoknim/TIL/blob/master/SpringBoot-Introduction/Lectures/Lecture16.md) \ No newline at end of file diff --git a/SpringBoot-Introduction/Lectures/Lecture16.md b/SpringBoot-Introduction/Lectures/Lecture16.md new file mode 100644 index 0000000..5e1723e --- /dev/null +++ b/SpringBoot-Introduction/Lectures/Lecture16.md @@ -0,0 +1,19 @@ +# 회원 웹 기능 - 등록 + +--- + +## Get방식 +- `url`에서 직접 접근하는 방식 +- 조회할 때 주로 사용하는 방식 +- 전달(데이터 등록)할 때는 `post`방식을 주로 사용한다. + +--- + +## form 태그 +- 값을 입력할 수 있는 `HTML` 태그 +- `method`에 `get`, `post`등을 지정한다. +- `input 태그`에서는 `name`이 서버로 넘어갈 때 `키(key)`가 된다. +- `action`에 지정된 `url`로 `post` 방식으로 값이 넘어간다. +- 값이 넘어갈 때 스프링이 알아서 `setName`을 통해서 `MemberForm`의 `name`에 값을 넣어준다(우리는 `getName`으로 꺼내면 된다). + +--- \ No newline at end of file diff --git a/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberController.java b/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberController.java index d876aa4..e8eb31e 100644 --- a/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberController.java +++ b/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberController.java @@ -1,8 +1,11 @@ package hello.springintroduction.controller; +import hello.springintroduction.domain.Member; import hello.springintroduction.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 { @@ -13,4 +16,19 @@ public class MemberController { 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:/"; + } } diff --git a/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberForm.java b/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberForm.java new file mode 100644 index 0000000..0808118 --- /dev/null +++ b/SpringBoot-Introduction/src/main/java/hello/springintroduction/controller/MemberForm.java @@ -0,0 +1,13 @@ +package hello.springintroduction.controller; + +public class MemberForm { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/SpringBoot-Introduction/src/main/resources/templates/members/createMemberForm.html b/SpringBoot-Introduction/src/main/resources/templates/members/createMemberForm.html new file mode 100644 index 0000000..c0353f2 --- /dev/null +++ b/SpringBoot-Introduction/src/main/resources/templates/members/createMemberForm.html @@ -0,0 +1,14 @@ + + + +
+
+
+ + +
+ +
+
+ + \ No newline at end of file