Spring #5 MVC์™€ ํ…œํ”Œ๋ฆฟ ์—”์ง„

2024. 7. 16. 20:13ใ†๐Ÿ“ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ/SPRING

๋ฐ˜์‘ํ˜•

์ด๋ฒˆ์—๋Š” MVC ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค

 

MVC : Model, View, Controller ๋กœ์จ, ์•ž ๊ธ€์ž๋ฅผ ๋”ฐ์™€์„œ MVC๋ผ๋Š” ๋‹จ์–ด๊ฐ€ ์ƒ์„ฑ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค

Spring์—์„œ๋Š” ๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ MVCํŒจํ„ด์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์งœ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!


 

package helloo.helloo_spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @GetMapping("hello") // get ์š”์ฒญ ์ฒ˜๋ฆฌ
    public String hello(Model model) { // model ๊ฐ์ฒด ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
        model.addAttribute("data", "hello!");
        // model ๊ฐ์ฒด์— data ์†์„ฑ ์ถ”๊ฐ€, ๊ทธ ๊ฐ’์„ hello! ์„ค์ •
        return "hello"; // ๋ทฐ์˜ ์ด๋ฆ„
        // ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋ฅผ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ์ฐพ๊ณ  hello.html ๋ฐ˜ํ™˜
    }

    @GetMapping("hello-mvc") // ์‚ฌ์šฉ์ž๊ฐ€ hello-mvc URL์„ ์ ‘์†ํ•˜๋ฉด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    public String helloMvc(@RequestParam("name") String name, Model model) {
        // @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์€ URL ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์ค‘ "name" ์ด๋ผ๋Š” ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒ ๋‹ค ๋ผ๋Š” ์˜๋ฏธ
        model.addAttribute("name", name);
        return "hello-template";
    }
}

// @RequestParam ์ถ”๊ฐ€์  ์„ค๋ช…
// ์‚ฌ์šฉ์ž๊ฐ€ http://localhost:8080/hello-mvc?name="spring" ์ž…๋ ฅ ํ›„ ์ ‘์†์‹œ
// @RequestParam("name")์—์„œ name์„ ์ฐพ์•„ ์ถ”์ถœ ํ›„ name ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ

 

helloMvc ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด์‹œ๋ฉด ๋ ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

hello-template.html

<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>

 


 

์‚ฌ์šฉ์ž๊ฐ€ http://localhost:8080/hello-mvc?name="spring" ์„ ์ž…๋ ฅํ›„ ์ ‘์†ํ•˜๋ฉด

 

 

์ด๋ ‡๊ฒŒ ๋œจ๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 


์—ฌ๊ธฐ์„œ ์ถ”๊ฐ€์ ์ธ ์ž‘์—…์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค

- ์‚ฌ์šฉ์ž๊ฐ€ index๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ž๋™์ ์œผ๋กœ name ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•จ.

- ์ „๋‹ฌ๋œ ์ธ์ž๋ฅผ ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด์ง€

 

1. ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ํผ์„ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค

templates/input.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ํผ ์ž…๋ ฅ ํŽ˜์ด์ง€</title>
</head>
<body>
  <h1>์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”</h1>
  <form action="/hello-mvc-test" method="get">
    <label for="name">์ด๋ฆ„:</label>
    <input type="text" id="name" name="name" />
    <button type="submit">Submit</button>
  </form>
</body>
</html>

 

์—ฌ๊ธฐ์„œ form action์„ /hello-mvc-test๋กœ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— submit์„ ํ–ˆ์„๋•Œ text์— ์ ํžŒ name์ด /hello-mvc-test๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

 

 

2. input.html์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ Controller๋ฅผ ๋งŒ๋“ค๊ฒ ์Šต๋‹ˆ๋‹ค

package helloo.helloo_spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {

    @GetMapping("hello") // get ์š”์ฒญ ์ฒ˜๋ฆฌ
    public String hello(Model model) { // model ๊ฐ์ฒด ๋งค๊ฐœ๋ณ€์ˆ˜, ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
        model.addAttribute("data", "hello!");
        // model ๊ฐ์ฒด์— data ์†์„ฑ ์ถ”๊ฐ€, ๊ทธ ๊ฐ’์„ hello! ์„ค์ •
        return "hello"; // ๋ทฐ์˜ ์ด๋ฆ„
        // ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋ฅผ ํ†ตํ•ด์„œ ๋ทฐ ํŒŒ์ผ์„ ์ฐพ๊ณ  hello.html ๋ฐ˜ํ™˜
    }

    @GetMapping("hello-mvc") // ์‚ฌ์šฉ์ž๊ฐ€ hello-mvc URL์„ ์ ‘์†ํ•˜๋ฉด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
    public String helloMvc(@RequestParam("name") String name, Model model) {
        // @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์€ URL ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ์ค‘ "name" ์ด๋ผ๋Š” ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๊ฒ ๋‹ค ๋ผ๋Š” ์˜๋ฏธ
        model.addAttribute("name", name);
        return "hello-template";
    }

    // ์—ฌ๊ธฐ๋ถ€ํ„ฐ
    @GetMapping("/")
    public String input() {
        return "input";
    }

    @GetMapping("hello-mvc-test")
    public String helloMvcTest(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template-test";
    }
    // ์—ฌ๊ธฐ๊นŒ์ง€
}

// @RequestParam ์ถ”๊ฐ€์  ์„ค๋ช…
// ์‚ฌ์šฉ์ž๊ฐ€ http://localhost:8080/hello-mvc?name="spring" ์ž…๋ ฅ ํ›„ ์ ‘์†์‹œ
// @RequestParam("name")์—์„œ name์„ ์ฐพ์•„ ์ถ”์ถœ ํ›„ name ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌ

 

์ด๋ฏธ static ํด๋”์— index.html์ด ์žˆ๊ธฐ๋•Œ๋ฌธ์— GetMapping์„ ์•ˆํ•ด์ฃผ๋ฉด static/index.html์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—

GetMapping์„ ํ†ตํ•ด์„œ ๊ฒฝ๋กœ๋ฅผ ๋‹ค์‹œ ์„ค์ •ํ•ด์ค๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅ ํผ์„ ํ†ตํ•ด์„œ ์ž…๋ ฅํ•˜๊ณ  ์ œ์ถœํ–ˆ์„๋•Œ ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด์ง€ ๊ฒฝ๋กœ๋ฅผ ์„ค์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

3. ๊ฒฐ๊ณผ๊ฐ’์„ ๋ณด์—ฌ์ฃผ๋Š” ํŽ˜์ด์ง€

templates/hello-template-test.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
    <meta charset="UTF-8">
    <title>์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๊ฐ’ ์ถœ๋ ฅ</title>
</head>
<body>
  <h1>Hello, <span th:text="${name}">!</span></h1>
</body>
</html>

 

4. ๊ฒฐ๊ณผ

localhost:8080์— ๋“ค์–ด์˜ค๊ฒŒ ๋˜๋ฉด

์ด๋Ÿฐ ํŽ˜์ด์ง€๊ฐ€ ๋œจ๊ณ , ์ด๋ฆ„์— spring์„ ์ž…๋ ฅํ•œ ํ›„ submit์„ ๋ˆ„๋ฅด๋ฉด

 

 

์ด๋Ÿฐ ํ™”๋ฉด์ด ๋œจ๊ณ  URL๋„ http://localhost:8080/hello-mvc-test?name=spring 
์œผ๋กœ ๋ณ€๊ฒฝ๋œ๊ฑธ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 


 

์‹คํ–‰๊ณผ์ •

1. localhost:8080์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๋ฉด ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” input.html์„ ๋ณด์—ฌ์คŒ

2. name์„ ์ž…๋ ฅํ•˜๊ณ  submit์„ ๋ˆ„๋ฅด๊ฒŒ ๋˜๋ฉด name๊ฐ’์ด hello-mvc-test๋กœ ์ „๋‹ฌ๋œ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด http://localhost:8080/hello-mvc-test?name=์ž…๋ ฅ๋œ๊ฐ’ ์„ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

3. hello-mvc-test ๊ฒฝ๋กœ์— ๋Œ€ํ•œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ํ›„ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์—์„œ name๊ฐ’์„ ๋ชจ๋ธ์— ์ถ”๊ฐ€ํ•œ ํ›„

4. hello-template-test.html ๋ทฐ๊ฐ€ ๋ Œ๋”๋ง๋œ๋‹ค.

 


์œ„ ๊ธ€์€ ๊น€์˜ํ•œ๋‹˜์˜ spring ์ž…๋ฌธ ๊ฐ•์˜๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ ์—ˆ์Šต๋‹ˆ๋‹ค!

๋ฐ˜์‘ํ˜•