Bibi's DevLog ๐Ÿค“๐ŸŽ

ํšŒ์›๊ฐ€์ž… ๋ฐ ํšŒ์›์กฐํšŒ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ - <form>, GET/POST, ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Spring ์Šคํ”„๋ง

ํšŒ์›๊ฐ€์ž… ๋ฐ ํšŒ์›์กฐํšŒ ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ - <form>, GET/POST, ๋ฆฌ๋‹ค์ด๋ ‰์…˜

๋น„๋น„ bibi 2021. 3. 9. 18:31

ํšŒ์›๊ฐ€์ž… ๊ธฐ๋Šฅ ๊ตฌํ˜„

ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€์—์„œ ํšŒ์›๊ฐ€์ž…์„ ํ•˜๋ฉด, ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„์—์„œ ๋ฐ›์•„ ์ถœ๋ ฅํ•œ๋‹ค.

<form>

์„œ๋ฒ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ <form>ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

  • ์†์„ฑ action : ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ ์–ด๋–ค URL๋กœ ๋ณด๋‚ผ์ง€๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ.

    <form action="/create"> : ํ•ด๋‹น form์œผ๋กœ๋ถ€ํ„ฐ /create๋กœ ์„œ๋ฒ„์— ์š”์ฒญ์„ ๋ณด๋ƒ„

URL์ด ์ถ”๊ฐ€๋˜๋ฉด ํ•ญ์ƒ ์ƒ์‘ํ•˜๋Š” Controller ๋ฐ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

  • formํƒœ๊ทธ์— ์•„๋ฌด ์†์„ฑ์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ GET๋ฐฉ์‹์œผ๋กœ ํ†ต์‹ ํ•œ๋‹ค.

index.mustache

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Index</title>
</head>
<body>
<form action="/create">
    <div>
        <label for="userid">์•„์ด๋””</label>
        <input type="text" name="userid" placeholder="์•„์ด๋””">
    </div>
    <div>
        <label for="password">๋น„๋ฐ€๋ฒˆํ˜ธ</label>
        <input type="password" name="password" placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ">
    </div>
    <div>
        <label for="username">์ด๋ฆ„</label>
        <input type="text" name="username" placeholder="์ด๋ฆ„">
    </div>
    <div>
        <label for="useremail">์ด๋ฉ”์ผ</label>
        <input type="email" name="useremail" placeholder="์ด๋ฉ”์ผ">
    </div>
    <button type="submit">์ œ์ถœ</button>
</form>
</body>
</html>

์•„๋ฌด๊ฒƒ๋„ ์ž…๋ ฅํ•˜์ง€ ์•Š๊ณ  ์ œ์ถœํ–ˆ์„ ๋•Œ์˜ URL

: http://localhost:8080/create?userid=&password=&username=&useremail=

UserController.java

package com.codessquad.qna.controller;

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

@Controller
public class UserController {

    @GetMapping("/create")
    public String create(String userid, String password, String username, String useremail) {
        System.out.println("์•„์ด๋”” : " + userid);
        System.out.println("๋น„๋ฐ€๋ฒˆํ˜ธ : " + password);
        System.out.println("์ด๋ฆ„ : " + username);
        System.out.println("์ด๋ฉ”์ผ : " + useremail);
        return "index";
    }
}

์ถœ๋ ฅ ๊ฒฐ๊ณผ : ์•„์ด๋”” : a ๋น„๋ฐ€๋ฒˆํ˜ธ : b ์ด๋ฆ„ : c ์ด๋ฉ”์ผ : d@e

์ฆ‰, html์—์„œ name์— ํ•ด๋‹นํ–ˆ๋˜ ๊ฐ’๋“ค (userid, password, username, useremail)์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ๋‹จ์  : GET๋ฐฉ์‹์€ URL๋กœ ๊ฐ’์ด ์ „๋‹ฌ๋˜์–ด ๋น„๋ฐ€๋ฒˆํ˜ธ ๋“ฑ ๋…ธ์ถœ์œ„ํ—˜์ด ์žˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š”

  • ํ•ด๊ฒฐ : formํƒœ๊ทธ์— method="post"์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์ปจํŠธ๋กค๋Ÿฌ ์–ด๋…ธํ…Œ์ด์…˜์„ @PostMapping()์œผ๋กœ ๋ณ€๊ฒฝํ•ด POST๋ฐฉ์‹์œผ๋กœ ์ „๋‹ฌํ•˜๋ฉด ๋œ๋‹ค.

    POST๋ฐฉ์‹์€ ๋ฐ์ดํ„ฐ๋ฅผ URL๋กœ ์ „๋‹ฌํ•˜์ง€ ์•Š์•„ ์ •๋ณด๊ฐ€ ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.

GET๋ฐฉ์‹๊ณผ POST๋ฐฉ์‹

GET ๋ฐฉ์‹ : ๋ฐ์ดํ„ฐ ์š”์ฒญ ๋ฐ ์กฐํšŒํ•  ๋•Œ.

POST ๋ฐฉ์‹ : ์„œ๋ฒ„์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „์†กํ•ด ์ถ”๊ฐ€ ๋ฐ ์ˆ˜์ •ํ•  ๋•Œ.

์šฐ์„ ์€ ์ด ์ •๋„๋งŒ ์•Œ๊ณ  ์‚ฌ์šฉํ•˜์ž.

@GetMapping("/create")
public String create(String userid, String password, String username, String useremail) 

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ด๋ณด๋‹ค ๋” ๋งŽ์€ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ๋ฉ”์†Œ๋“œ ์„ ์–ธ๋ถ€๊ฐ€ ์ ์  ๊ธธ์–ด์ง„๋‹ค.

๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

User.java

package com.codessquad.qna.user;

public class User {
    private String userId;
    private String password;
    private String userName;
    private String userEmail;

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserEmail(String userEmail) {
        this.userEmail = userEmail;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", password='" + password + '\'' +
                ", userName='" + userName + '\'' +
                ", userEmail='" + userEmail + '\'' +
                '}';
    }
}

<form>ํƒœ๊ทธ์— ๋ถ€์—ฌ๋œ name ๊ฐ’๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ setter ๋ฐ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ฉด ์ด๋ ‡๊ฒŒ ๋ณ„๋„์˜ ํด๋ž˜์Šค๋กœ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

โ— ์ด๋ฆ„์ด ๊ฐ™์•„์•ผ ์Šคํ”„๋ง์ด ์ž๋™์œผ๋กœ setter๋ฅผ ํ†ตํ•ด private๋ณ€์ˆ˜์— setter๋กœ ๊ฐ’์„ ๋„ฃ์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค!

  • private๋กœ ์„ ์–ธํ•˜๊ณ  setter๋กœ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.
  • ์ถœ๋ ฅ ํŽธ์˜์„ฑ์„ ์œ„ํ•ด toString()์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•œ๋‹ค.

์‚ฌ์šฉ์ž ๋ชฉ๋ก ๊ธฐ๋Šฅ ๊ตฌํ˜„

ํšŒ์›๊ฐ€์ž…์„ ์™„๋ฃŒํ•œ ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ๋‹ค.

UserController.java

package com.codessquad.qna.controller;

import com.codessquad.qna.user.User;
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 java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private List<User> users = new ArrayList<>();

    @GetMapping("/create")
    public String create() {
        return "index";
    }

    @PostMapping("/create")
    public String register(User user) {
        System.out.println("user : " + user);
        users.add(user);
        return "redirect:/list";
    }

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("users", users);
        return "list";
    }
}

private List<User> users = new ArrayList<>();

  • ์‚ฌ์šฉ์ž ๋ชฉ๋ก์„ ์ €์žฅํ•  ์ปฌ๋ ‰์…˜ ์ถ”๊ฐ€

public String list(Model model)

  • list.html์— ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด Model ์‚ฌ์šฉ

list.mustache

<!DOCTYPE HTML>
<html>
<head>
    <title>List</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>ํšŒ์› ๋ชฉ๋ก</h1>
<div>
    <table>
        <caption>์‚ฌ์šฉ์ž ๋ชฉ๋ก</caption>
        <thead>
        <tr>
            <th>์‚ฌ์šฉ์ž ์•„์ด๋””</th>
            <th>์ด๋ฆ„</th>
            <th>์ด๋ฉ”์ผ</th>
        </tr>
        </thead>
        <tbody>
        {{#users}}
        <tr>
            <td>{{userId}}</td>
            <td>{{name}}</td>
            <td>{{email}}</td>
        </tr>
        {{/users}}
        </tbody>
    </table>
</div>
</body>
</html>

{{#users}}, {{/users}}

: mustache์˜ for๋ฌธ ๋ฌธ๋ฒ•. users๋ผ๋Š” ๋ฐ˜๋ณต๋Œ€์ƒ๊ฐ์ฒด๋ฅผ ๋‘ ํƒœ๊ทธ ์‚ฌ์ด์—์„œ ๋ฐ˜๋ณตํ•œ๋‹ค

{{userId}}, {{userName}}, {{userEmail}}

: ๋ฐ˜๋ณต๋Œ€์ƒ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์š”์†Œ (User์˜ ์ธ์Šคํ„ด์Šค๋ณ€์ˆ˜)๋ฅผ ์ง€์ •ํ•ด ๊ทธ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค

๋ฆฌ๋‹ค์ด๋ ‰์…˜

ํ•ด๋‹น URL๋กœ ์ด๋™ํ•˜๋„๋ก ๋งŒ๋“ค๊ธฐ.

return "redirect:/list" : ๋ฉ”์„œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ /list ๋กœ ํŽ˜์ด์ง€๋ฅผ ์ด๋™์‹œํ‚จ๋‹ค.

ํ˜„์žฌ ์ €์žฅํ•œ ์ปฌ๋ ‰์…˜(ArrayList)์€ ์„œ๋ฒ„๊ฐ€ ์žฌ์‹œ์ž‘ํ–ˆ์„ ๋•Œ ๊ทธ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ชจ๋‘ ์‚ฌ๋ผ์ง„๋‹ค.

์™œ? ๋ฐ์ดํ„ฐ๊ฐ€ RAM ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ๋˜๊ธฐ ๋•Œ๋ฌธ์—. (์ „์› ์ข…๋ฃŒ์‹œ ๋ฐ์ดํ„ฐ ์‚ฌ๋ผ์ง)

์ด์ œ ํ•˜๋“œ๋””์Šคํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํŒŒ์ผ์ด๋‚˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์‹œ์ ์ด๋‹ค.