
MVC는 Model-View-Controller 의 약자로, SW개발에서 사용되는 디자인 패턴이다.
이 패턴은 애플리케이션을 세 가지 컴포넌트(Model, View, Controller)로 나누어 관리해 유지보수성과 확장성을 높인다고 한다.
먼저 기존에 있던 HelloController에 컨트롤러를 추가했다.
// controller/HelloController.java
...
import org.springframework.web.bind.annotation.RequestParam;
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template";
}
Get요청을 hello-mvc로 맵핑하고 @RequestParam을 통해 name 파라미터를 받는다.
받은 name 파라미터를 템플릿의 name에 전달하고 hello-template로 반환한다.
반환할 hello-template.html이 없으므로 만들었다.
<!-- templates/hello-template.html -->
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<p th:text="'hello. ' + ${name}">hello! empty</p>
</body>
</html>
hello-mvc로 접근하게 되면 name 파라미터로 입력된 값이 전달되어 "hello. name파라미터값" 이 화면에 출력될 것이다.
서버를 실행해서 확인해봤다.

?name=자바스프링! 을 통해 name 파라미터에 자바스프링! 을 전달했고 전달된 파라미터가 웹 페이지에 잘 출력되는 것을 확인할 수 있다.