Cooper's devlog

2-2. 회원가입 기능 구현 본문

Programming/Spring-boot

2-2. 회원가입 기능 구현

cooper_dev 2020. 7. 10. 01:06

2-2. 회원가입 기능 구현

1. 강의 링크(박재성님)

https://www.youtube.com/watch?v=UQ2wPndQ4-0&list=PLqaSEyuwXkSppQAjwjXZgKkjWbFoUdNXC&index=10

 


2. 학습 내용

  • GET/POST에 대한 초간단 설명
  • User 클래스 추가 및 setter 메소드를 통한 자동 매핑

 


3. 과정

(1) submit을 할 때, form tag를 사용한다.

(2)controller을 생성

client에서 데이터를 전달해주고 싶다면 input tag의 namecontroller의 매개인자 이름과 같게 설정되어 있어야 한다.

-> 만약 인자가 너무 많다면 class를 하나 더 생성해서 만들어 주도록 하자.

 

<static/form.html>

  • form action → /create 요청을 하겠다.
  • method → post(요청 값을 url에 공개하지 않겠다) ==> localhost:8787/create 끝.
  • input tag의 name과 User객체의 변수와 이름이 같아야 mapping이 가능!!

 

<User.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package net.slipp.web;
 
public class User {
    private String UserId;
    private String password;
    private String name;
    private String email;
    
    public void setUserId(String userId) {
        UserId = userId;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    
    @Override
    public String toString() {
        return "User [UserId=" + UserId + ", password=" + password + ", name=" + name + ", email=" + email + "]";
    }
}
 
 

 

<userController.java>

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package net.slipp.web;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
 
@Controller
public class UserController {
    @PostMapping("/create")
    public String create(User user) {
        System.out.println("user: " + user);
        return "index";
    }
}
 
cs
  •  
  • form method : post방식이었으므로, PostMapping으로 선언해서 사용하자.
  • input tag의 name과 User객체의 변수와 이름이 같아야 mapping이 가능하다.

 


 

Comments