MVC란? Model, View, Controller
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
hello-template.html
파일을 생성하여 아래의 코드를 작성한다.<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Hello</title>
</head>
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
스프링 서버를 실행한 후, localhost:8080/hello-mvc
로 서버에 접속한다.
에러가 난다. ???
Required request parameter 'name' for method parameter type String is not present
(name의 값이 없다)
localhost:8080/hello-mvc?name=출력값
으로 name
에 값을 넣어주어야 한다.
ex) localhost:8080/hello-mvc?name=hello spring!!!!!!!
웹브라우저가 localhost:8080/hello-mvc
를 넘기면
내장 톰켓 서버를 거치고, 내장 톰켓 서버는 스프링에게 해당 서버를 넘긴다.
helloController
에 매핑된 url
을 확인하고 매핑이 되어있다면 리턴값과 키, 값을 viewResolver
로 넘긴다.
viewResolver
는 리턴된 url 이름과 똑같은 파일을 찾아 Thymeleaf
로 넘긴다.
Rendering
을 해서 변환을 한 파일을 웹 브라우저에 띄운다.