> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/jpa/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/jpa/7./undefined-1.md).

# 회원 등록

구현할 내용

1. 화면에 랜더링할 Form 데이터 MemberForm 객체 생성\
   컨트롤러에서 모델이 빈 객체 memberForm을 담아서 보낸다. 이 객체는 validation 역할을 한다.

   ```java
   package jpabook.jpashop.controller;

   import lombok.Getter;
   import lombok.Setter;

   import javax.validation.constraints.NotEmpty;

   @Getter @Setter
   public class MemberForm {
       @NotEmpty(message = "회원 이름은 필수 입니다")
       private String name;

       private String city;
       private String street;
       private String zipcode;
   }

   ```
2. 컨트롤러\
   컨트롤러는 서비스에 서비스를 요청한다.\
   ① GetMapping : HTML Form 페이지 조회.

   ```
   @GetMapping("/members/new")
   public String createForm(Model model){
       //빈 MemberForm 클래스를 모델이 넣어준다. validation을 해주기 때문.
       model.addAttribute("memberForm",new MemberForm());
       return "members/createMemberForm";
   }
   ```

   ② PostMapping : HTML Form 데이터 전송받는다.\
   @NotEmpty, @Valid : 애노테이션 기반으로 간편하게 validation 하기\
   \=> 필수 입력 필드에 값이 없으면 컨트롤러에서 해당 메서드 실행이 안 되지만, BindingResult를 함께 파라미터로 넣어주면 없는 채로 메서드가 실행될 수 있다!

   스프링과 타임리프 연동이 매우 잘 되어 있어서 스프링 컨트롤러에서 타임리프 form 페이지로 보내면 BindingResult에서 에러를 알 수 있는 메서드가 굉장히 많은데 이것들을 이용해서 화면을 구성할 수 있다!

   ```
   @PostMapping("/members/new")
   public String create(@Valid MemberForm form, BindingResult result){//MemberForm에서 NotEmpty였던 필드 validation 해준다.
       if(result.hasErrors()){
           return "members/createMemberForm";
       }
       Address address = new Address(form.getCity(), form.getStreet(), form.getZipcode());

       Member member = new Member();
       member.setName(form.getName());
       member.setAddress(address);

       memberService.join(member);
       return "redirect:/";
   }
   ```
3. 타임리프 템플릿 : createMemberForm.html\
   \&#xNAN;**\<form action="/members/new"...method="post">** : 작성한 HTML Form 데이터는 동일한 url로 POST 방식으로 보낸다!\
   **th:object="${memberForm}** : 타임리프 내에서 memberForm 객체를 변수처럼 계속 사용하겠다는 뜻이다. \
   \&#xNAN;**\*{...}** : 프로퍼티 접근법으로 객체의 속성에 접근할 수 있다.\
   \=>\*{name}, \*{city}, \*{streeet}...

![](/files/-MePXVTwD9jbDebiYEDe)

![insert문 확인(저장 완료)](/files/-MePUil6tJAgnB0yw6b4)

![H2 DB 업데이트 확인](/files/-MePUw27sWjlHacfjURs)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://heunnajo.gitbook.io/jpa/7./undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
