일단 시작하기에 앞서 MVC를 이해를 해야한다.

이 구조를 바탕으로 아래 그림을 참고해서 생각을 해보자.
먼저 hello.hellospring.HelloSpringApplication의 메인문을 실행하면, 8080포트로 서버가 실행이 된다.
그러면 http://localhost:8080 로 접속하게 되면 resources.static.index.html이 표시가 될것이다.
여기서 http://localhost:8080/hello로 get요청을 해보면 java Spring에서는 controller가 있는 HelloController로 들어가서 get 메소드를 실행하게 된다.
이 get 메소드는 addAttribute로 model에 데이터를 저장하고, return으로 view 템플릿을 어떤거로 할지 결정하게 된다. (view resolver이 알아서 return값에 해당하는 페이지를 찾아줌)
이렇게 되면 우리 눈에 보여지는 페이지는 get 메소드의 return 값을 통해 resources.templates.returnValue.html 로 보여지게된다.
template엔진인 Thymeleaf로 모델 데이터들을 가져와서 보여진다.

controller.HelloController
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
@Controller?@GetMapping("hello")http://localhost:8080/hello 라고 입력하면 아래 메소드로 들어오게된다.public String hello(Model model)public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}addAttribute모델에다가 key / value 를 입력해준다.@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}@GetMapping("hello-mvc")http://localhost:8080/hello-mvc 라고 입력하면 아래 메소드로 들어오게된다.public String helloMvc(@RequestParam("name") String name, Model model)http://localhost:8080/hello-mvc?name=parametervalue라고 입력하면 parametervalue가 name으로 입력이되어 함수의 파라미터로도 전달이 된다.model.addAttribute("name", name);return "hello-template";resources.templates.hello.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" > 안녕하세요. 손님</p>
</body>
</html>
<p th:text="'안녕하세요. ' + ${data}" > 안녕하세요. 손님</p>resources.templates.hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello empty</p>
</body>
</html>
${name}의 value값이 치환된다.