MapleStory Cookie With Halo

๐Ÿƒ Spring/๐Ÿƒ Spring Boot

[SpringBoot][Thymeleaf]์›น ๊ฐœ๋ฐœ ๊ธฐ์ดˆ-MVC์™€ ํ…œํ”Œ๋ฆฟ ์—”์ง„

๋‰ด์ด NUEY 2025. 1. 3. 23:08
๋ฐ˜์‘ํ˜•

 

๐Ÿ’ก MVC pattern
Model, View, Controller
๋ฐ์ดํ„ฐ, ํ™”๋ฉด, ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌํ•œ ๊ฒƒ์ด๋‹ค.

 


 

์ฝ”๋“œ

 

  1. templatesํด๋”์— htmlํŒŒ์ผ ํ•˜๋‚˜๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
    ๋”๋ณด๊ธฐ
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>hello template</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <html>
    <body>
        <p th:text="'hello ' + ${name}">hello! template</p>
    </body>
    </html>
    </html>
  2. Controllerํด๋”์— HelloController๋ผ๋Š” ์ž๋ฐ”ํŒŒ์ผ์— helloMvc๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค.
    ๋”๋ณด๊ธฐ
    package hello.hellospring.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")
        public String hello(Model model) {
            model.addAttribute("data", "hihi");
            return "hello";
        }
    
        @GetMapping("hello-mvc")
        public String helloMvc(@RequestParam(name = "name") String name, Model model) {
            model.addAttribute("name", name);
            return "hello-template";
        }
    
    }
  3. ์„œ๋ฒ„์‹คํ–‰ ๊ฒฐ๊ณผ

 

๐ŸŒฟ ์ฝ”๋“œ ์„ค๋ช…๐ŸŒฟ

 

  1. ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:8080/hello-mvc?name=spring!๋ฅผ ์น˜๋ฉด

    1. localhost:8080
      ๋กœ์ปฌํ˜ธ์ŠคํŠธ์˜ 8080ํฌํŠธ์—์„œ

    2. /hello-mvc
      hello-mvc๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ช…์„ ์ฐพ์•„๊ฐ‘๋‹ˆ๋‹ค.
      → @GetMapping("hello-mvc") 
    3. ?name=spring!
      • ? ๋’ท ๋ถ€๋ถ„์€ ์ธ์ˆ˜์ž…๋‹ˆ๋‹ค. 
      • name์ด๋ž€ ํŒŒ๋ผ๋ฏธํ„ฐ์— spring! ์ด๋ž€ ๊ฐ’์„ ๋„ฃ์–ด ๋ณด๋ƒ…๋‹ˆ๋‹ค.

  2. Controller
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam(name = "name") String name, Model model) {
    	model.addAttribute("name", name);
    	return "hello-template";
    }


    1. @RequestParam(name = "name")
      • "name"์ด๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’์„ String name์— ๋‹ด์•„์ค๋‹ˆ๋‹ค. 

    2. model.addAttribute("name", name);
      • model์€ return์— ์ ์ง€ ์•Š์•„๋„ ์ž๋™์œผ๋กœ ๊ฐ™์ด ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ์ดํ„ฐ์ž…๋‹ˆ๋‹ค.
      • addAttribute ๊ฐ’์„ ๋‹ด๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

    3. return "hello-template";
      • "hello-template"์ด๋ผ๋Š” ์ด๋ฆ„์˜ view๋ฅผ ์ฐพ์•„๊ฐ‘๋‹ˆ๋‹ค.
      • ์ด๋ ‡๊ฒŒ๋งŒ ์ ์–ด๋„ ๋™์ž‘ํ•˜๋Š” ๊ฑด Thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„์˜ viewResolver๋•๋ถ„์ž…๋‹ˆ๋‹ค.

  3. view
    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>hello template</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <html>
    <body>
        <p th:text="'hello ' + ${name}">hello! template</p>
    </body>
    </html>
    </html>
    1. <html xmlns:th="http://www.thymeleaf.org">
      • html์—์„œ th๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ํƒ€์ž„๋ฆฌํ”„๊ฐ€ ์—ฐ๊ฒฐ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

    2. <p th:text="'hello ' + ${name}">hello! template</p>
      • ํƒ€์ž„๋ฆฌํ”„ ํ…œํ”Œ๋ฆฟ ์—”์ง„์„ ์ด์šฉํ•ด model์— ๋‹ด๊ฒจ์˜จ name์ด๋ž€ ์ด๋ฆ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.

 


โ€ป ์ฐธ์กฐ : https://www.inflearn.com/roadmaps/373

๋ฐ˜์‘ํ˜•