@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
}
Controller는 서버 비즈니스 로직과 같은 뒷단과 관련된 일만!
hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
</body>
</html>
View는 화면과 관련된 일만!
화면에서 필요한 것들을 담아서 화면 쪽에 넘겨줌!
http://localhost:8080/hello-mvc 이렇게 하면 에러 페이지가 뜬다.
http://localhost:8080/hello-mvc?name=spring
로 실행해야 한다.
name=spring!
이 넘어가면 Controller
에서 name
은 spring!
으로 바뀜
model
에 담김
template
으로 넘어감
hello-template.html
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello ' + ${name}">hello! empty</p>
<!--템플릿 엔진이 동작하면 hello! empty 가 'hello ' + ${name} 으로 치환된다. -->
</body>
</html>
name
→ spring!