์นดํ…Œ๊ณ ๋ฆฌ ์—†์Œ

STEP 02 - ์‚ฌ์šฉ์ž ๊ด€๋ฆฌ ๊ธฐ๋ณธ ์ฝ”๋“œ ์ถ”๊ฐ€ ๋ฐ ์„ค์ •

๋ฏธ๋กœ910 2024. 10. 8. 09:04
๐Ÿ’ก
ํ”„๋กœ์ ํŠธ ์ง„ํ–‰์„ ์œ„ํ•œ ๊ธฐ๋ณธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
HttpServletRequest ์™€ Model ๋ฅผ ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

UserController
package com.tenco.blog_v1.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

@Slf4j
@Controller
public class UserController {

    /**
     * ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ ์š”์ฒญ
     * ์ฃผ์†Œ์„ค๊ณ„ : http://localhost:8080/join-form
     *
     * @param model
     * @return ๋ฌธ์ž์—ด
     * ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉฐ
     * ๋จธ์Šคํƒœ์น˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.
     */
    public String joinForm(Model model) {
        log.info("ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€");
        model.addAttribute("name", "ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€");
        return "user/join-form"; // ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ : user/join-form.mustache
    }

    /**
     * ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์š”์ฒญ
     * ์ฃผ์†Œ์„ค๊ณ„ : http://localhost:8080/login-form
     *
     * @param model
     * @return ๋ฌธ์ž์—ด
     * ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉฐ
     * ๋จธ์Šคํƒœ์น˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.
     */
    public String loginForm(Model model) {
        log.info("๋กœ๊ทธ์ธ ํŽ˜์ด์ง€");
        model.addAttribute("name", "๋กœ๊ทธ์ธ ํŽ˜์ด์ง€");
        return "user/login-form"; // ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ : user/join-form.mustache
    }

    /**
     * ํšŒ์› ์ •๋ณด ์ˆ˜์ • ํŽ˜์ด์ง€ ์š”์ฒญ
     * ์ฃผ์†Œ์„ค๊ณ„ : http://localhost:8080/user/update-form
     *
     * @param model
     * @return ๋ฌธ์ž์—ด
     * ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉฐ
     * ๋จธ์Šคํƒœ์น˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.
     */
    public String updateForm(Model model) {
        log.info("ํšŒ์› ์ˆ˜์ • ํŽ˜์ด์ง€");
        model.addAttribute("name", "ํšŒ์› ์ˆ˜์ • ํŽ˜์ด์ง€");
        return "user/update-form"; // ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ : user/join-form.mustache
    }

}

 

๐Ÿ’ Spring์—์„œ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์‚ฌ์šฉํ•  ๋•Œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ทฐ๋กœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹

HttpServletRequest ๊ฐ์ฒด ์‚ฌ์šฉ

  • HttpServletRequest๋Š” ๊ธฐ๋ณธ ์„œ๋ธ”๋ฆฟ ๊ฐœ๋…์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“  HTTP ์š”์ฒญ์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์„ธ์…˜์ด๋‚˜ ์ฟ ํ‚ค ๊ฐ™์€ ์ •๋ณด๋„ ํ•จ๊ป˜ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ API์™€ ์—ฐ๊ด€๋˜์–ด ์žˆ์–ด ๊ตฌ์กฐ๊ฐ€ ์•ฝ๊ฐ„ ๋ณต์žกํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Model ๊ฐ์ฒด ์‚ฌ์šฉ

  • Model์€ Spring MVC์—์„œ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ ์ „์šฉ์œผ๋กœ ๋งŒ๋“ค์–ด์กŒ์Šต๋‹ˆ๋‹ค.
  • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ทฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜๋ฉฐ, Spring ํ”„๋ ˆ์ž„์›Œํฌ์™€ ๊นŠ์ด ์—ฐ๊ด€๋˜์–ด ์žˆ์–ด ์œ ์ง€๋ณด์ˆ˜๋‚˜ ์ฝ”๋“œ ๊ฐ€๋…์„ฑ

ํ•ต์‹ฌ ์ •๋ฆฌ

  • Model: ๊ฐ„๋‹จํ•˜๊ณ  Spring MVC์—์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ์ผ๋ฐ˜์ ์ธ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.
  • HttpServletRequest: ๋ณต์žกํ•œ ์š”์ฒญ ์ฒ˜๋ฆฌ, ์„ธ์…˜ ๊ด€๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค. ์„œ๋ธ”๋ฆฟ ๊ธฐ๋ฐ˜ ์ž‘์—…์— ์ ํ•ฉํ•ฉ๋‹ˆ๋‹ค.

 

HttpServletRequest ํด๋ž˜์Šค ์‚ฌ์šฉ
package com.tenco.blog_v1.user;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Slf4j
@Controller
public class UserController {

    /**
     * ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€ ์š”์ฒญ
     * ์ฃผ์†Œ์„ค๊ณ„ : http://localhost:8080/join-form
     *
     * @param model
     * @return ๋ฌธ์ž์—ด
     * ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉฐ
     * ๋จธ์Šคํƒœ์น˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.
     */
    @GetMapping("/join-form")
    public String joinForm(Model model) {
        log.info("ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€");
        model.addAttribute("name", "ํšŒ์›๊ฐ€์ž… ํŽ˜์ด์ง€");
        return "user/join-form"; // ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ : user/join-form.mustache
    }

    /**
     * ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€ ์š”์ฒญ
     * ์ฃผ์†Œ์„ค๊ณ„ : http://localhost:8080/login-form
     *
     * @param model
     * @return ๋ฌธ์ž์—ด
     * ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉฐ
     * ๋จธ์Šคํƒœ์น˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.
     */
    @GetMapping("/login-form")
    public String loginForm(Model model) {
        log.info("๋กœ๊ทธ์ธ ํŽ˜์ด์ง€");
        model.addAttribute("name", "๋กœ๊ทธ์ธ ํŽ˜์ด์ง€");
        return "user/login-form"; // ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ : user/join-form.mustache
    }

    /**
     * ํšŒ์› ์ •๋ณด ์ˆ˜์ • ํŽ˜์ด์ง€ ์š”์ฒญ
     * ์ฃผ์†Œ์„ค๊ณ„ : http://localhost:8080/user/update-form
     *
     * @param model
     * @return ๋ฌธ์ž์—ด
     * ๋ฐ˜ํ™˜๋˜๋Š” ๋ฌธ์ž์—ด์„ ๋ทฐ ๋ฆฌ์กธ๋ฒ„๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋ฉฐ
     * ๋จธ์Šคํƒœ์น˜ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค.
     */
    @GetMapping("/user/update-form")
    public String updateForm(Model model) {
        log.info("ํšŒ์› ์ˆ˜์ • ํŽ˜์ด์ง€");
        model.addAttribute("name", "ํšŒ์› ์ˆ˜์ • ํŽ˜์ด์ง€");
        return "user/update-form"; // ํ…œํ”Œ๋ฆฟ ๊ฒฝ๋กœ : user/join-form.mustache
    }

}

 

join-form.mustache
{{> layout/header}}

<main class="container p-5 content">
    <div class="card">
        <div class="card-header"><b>{{name}}</b></div>
        <div class="card-body">
            <form action="/join" method="post" enctype="application/x-www-form-urlencoded">
                <div class="mb-3">
                    <input type="text" class="form-control" placeholder="enter username" name="username">
                </div>

                <div class="mb-3">
                    <input type="password" class="form-control" placeholder="enter password" name="password">
                </div>

                <div class="mb-3">
                    <input type="email" class="form-control" placeholder="enter email" name="email">
                </div>

                <button type="submit" class="btn btn-primary form-control">ํšŒ์›๊ฐ€์ž…</button>
            </form>

        </div>
    </div>

</main>

{{> layout/footer}}

 

login-form.mustache
{{> layout/header}}

<main class="container p-5 content">
    <div class="card">
        <div class="card-header"><b>{{name}}</b></div>
        <div class="card-body">
            <form action="/login" method="post" enctype="application/x-www-form-urlencoded">
                <div class="mb-3">
                    <input type="text" class="form-control" placeholder="enter username" name="username">
                </div>

                <div class="mb-3">
                    <input type="password" class="form-control" placeholder="enter password" name="password">
                </div>

                <button type="submit" class="btn btn-primary form-control">๋กœ๊ทธ์ธ</button>
            </form>

        </div>
    </div>

</main>

{{> layout/footer}}

 

udpate-form.mustache
{{> layout/header}}

<main class="container p-5 content">
    <div class="card">
        <div class="card-header"><b>{{name}}</b></div>
        <div class="card-body">
            <form action="/user/update" method="post" enctype="application/x-www-form-urlencoded">
                <div class="mb-3">
                    <input type="text" class="form-control" placeholder="enter username" name="username" disabled>
                </div>

                <div class="mb-3">
                    <input type="password" class="form-control" placeholder="enter password" name="password">
                </div>

                <div class="mb-3">
                    <input type="email" class="form-control" placeholder="enter email" name="email">
                </div>

                <button type="submit" class="btn btn-primary form-control">ํšŒ์›์ •๋ณด์ˆ˜์ •</button>
            </form>

        </div>
    </div>

</main>

{{> layout/footer}}