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, ν
νλ¦Ώ μμ§ μ΄λ―Έμ§.
- μΉ λΈλΌμ°μ μμ
localhost:8080/hello-mvc
λ₯Ό λκΉλλ€. - μ€νλ§ λΆνΈλ₯Ό λμΈ λ ν¨κΉ¨ λμ°λ βλ΄μ₯ ν°μΌ μλ²λ₯Ό
localhost:8080/hello-mvc
κ° κ±°μΉ©λλ€β. -
λ΄μ₯ ν°μΌ μλ²κ° β
localhost:8080/hello-mvc
κ° μμ΄~β νκ³ λ μ€νλ§μκ² λμ§λλ€. - κ·Έλ¬λ©΄ μ€νλ§μ βμ!
localhost:8080/hello-mvc
κ°helloController
λ΄λΆ λ©μλμ λ§€νμ΄ λμ΄μλ€?!βνκ³ λ βκ·Έ λ©μλλ₯Ό νΈμΆν΄μ€λλ€.β - νΈμΆν λ©μλκ° λ¦¬ν΄μ μ΄λ¦μ βhello-templeβμ΄λΌκ³ νκ³ , βmodelμ ν€λ nameμ΄κ³ , κ°μ springμΌλ‘ λ£μκ²βμ μ€νλ§μκ² λ겨μ€λλ€.
- κ·Έλ¬λ©΄ μ€νλ§μ΄ βviewResolverβ (νλ©΄κ³Ό κ΄λ ¨λ ν΄κ²°μ - λ·°λ₯Ό μ°Ύμμ£Όκ³ ν νλ¦Ώ μμ§μ μ°κ²° μμΌμ£Όλ κ²μ λλ€.)κ° λμν©λλ€.
- viewResolverκ° βtemplatesβ μ βhello-templateβ μ΄λΌλ β리ν΄μ String nameκ³Ό λκ°μ μμ΄λ₯Ό μ°Ύμμ Thymeleaf ν νλ¦Ώ μμ§μκ² μ²λ¦¬ν΄λ¬λΌκ³ λκΉλλ€.β
- κ·Έλ¬λ©΄ βν νλ¦Ώ μμ§μ΄ λλλ§βμ νμ¬ βλ³νμ ν HTMLβ μ βμΉ λΈλΌμ°μ μ λ°νν©λλ€.β
-
μ μ μΌ λλ βλ³νμ νμ§ μμμ΅λλ€.β μ¦, κ·Έλλ‘ λ°νμ ν΄μ€¬μ΅λλ€.
- μ΄λ° βν νλ¦Ώ μμ§μμλ λ³νμν΄μ μΉ λΈλΌμ°μ μ λ겨μ€λλ€.β