Home > Archive > Spring & Spring Boots > πŸƒ[Spring] MVC와 ν…œν”Œλ¦Ώ μ—”μ§„

πŸƒ[Spring] MVC와 ν…œν”Œλ¦Ώ μ—”μ§„
Spring Framework

MVC와 ν…œν”Œλ¦Ώ μ—”μ§„

  • MVC: Model, View, Controller

Controller

package com.devkobe.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-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }
}

View

<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

MVC, ν…œν”Œλ¦Ώ μ—”μ§„ 이미지.

  1. μ›Ή λΈŒλΌμš°μ €μ—μ„œ localhost:8080/hello-mvcλ₯Ό λ„˜κΉλ‹ˆλ‹€.
  2. μŠ€ν”„λ§ λΆ€νŠΈλ₯Ό λ„μšΈ λ•Œ 함깨 λ„μš°λŠ” β€œλ‚΄μž₯ ν†°μΌ“ μ„œλ²„λ₯Ό localhost:8080/hello-mvc κ°€ κ±°μΉ©λ‹ˆλ‹€β€.
  3. λ‚΄μž₯ ν†°μΌ“ μ„œλ²„κ°€ β€˜localhost:8080/hello-mvcκ°€ μ™”μ–΄~’ ν•˜κ³ λŠ” μŠ€ν”„λ§μ—κ²Œ λ˜μ§‘λ‹ˆλ‹€.
  4. 그러면 μŠ€ν”„λ§μ€ β€˜μ•„! localhost:8080/hello-mvcκ°€ helloController λ‚΄λΆ€ λ©”μ„œλ“œμ— 맀핑이 λ˜μ–΄μžˆλ„€?!β€™ν•˜κ³ λŠ” β€œκ·Έ λ©”μ„œλ“œλ₯Ό ν˜ΈμΆœν•΄μ€λ‹ˆλ‹€.”
  5. ν˜ΈμΆœν•œ λ©”μ„œλ“œκ°€ λ¦¬ν„΄μ‹œ 이름을 β€œhello-temple”이라고 ν•˜κ³ , β€œmodel에 ν‚€λŠ” name이고, 값은 spring으둜 넣은것”을 μŠ€ν”„λ§μ—κ²Œ λ„˜κ²¨μ€λ‹ˆλ‹€.
  6. 그러면 μŠ€ν”„λ§μ΄ β€œviewResolver” (ν™”λ©΄κ³Ό κ΄€λ ¨λœ ν•΄κ²°μž - λ·°λ₯Ό μ°Ύμ•„μ£Όκ³  ν…œν”Œλ¦Ώ 엔진을 μ—°κ²° μ‹œμΌœμ£ΌλŠ” κ²ƒμž…λ‹ˆλ‹€.)κ°€ λ™μž‘ν•©λ‹ˆλ‹€.
  7. viewResolverκ°€ β€œtemplates” 의 β€œhello-template” μ΄λΌλŠ” β€œλ¦¬ν„΄μ˜ String nameκ³Ό λ˜‘κ°™μ€ 아이λ₯Ό μ°Ύμ•„μ„œ Thymeleaf ν…œν”Œλ¦Ώ μ—”μ§„μ—κ²Œ μ²˜λ¦¬ν•΄λ‹¬λΌκ³  λ„˜κΉλ‹ˆλ‹€.”
  8. 그러면 β€œν…œν”Œλ¦Ώ 엔진이 λžœλ”λ§β€œμ„ ν•˜μ—¬ β€œλ³€ν™˜μ„ ν•œ HTML” 을 β€œμ›Ή λΈŒλΌμš°μ €μ— λ°˜ν™˜ν•©λ‹ˆλ‹€.”
  • 정적일 λ•ŒλŠ” β€œλ³€ν™˜μ„ ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.” 즉, κ·ΈλŒ€λ‘œ λ°˜ν™˜μ„ ν•΄μ€¬μŠ΅λ‹ˆλ‹€.
    • 이런 β€œν…œν”Œλ¦Ώ μ—”μ§„μ—μ„œλŠ” λ³€ν™˜μ„ν•΄μ„œ μ›Ή λΈŒλΌμš°μ €μ— λ„˜κ²¨μ€λ‹ˆλ‹€.”