김영한님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 정리
MVC(Model-View-Controller)는 소프트웨어 설계의 관심사 분리(Separation of Concerns)를 위한 대표적인 아키텍처입니다. 웹 개발에서 이 패턴은 특히 유지보수와 확장성을 확보하는 데 강력한 장점을 제공합니다.
과거에는 JSP 안에 비즈니스 로직, 화면 처리, DB 연동이 한 파일에 몰려있는 Model 1 방식이 많았지만, 이는 유지보수 지옥을 불러왔습니다. 현대적 방식인 MVC는 이를 구조적으로 분리합니다.
간단한 Hello 예제를 통해 MVC 구조를 설명하겠습니다.
@Controller
public class HelloController {
@GetMapping("hello-mvc")
public String helloMvc(@RequestParam("name") String name, Model model) {
model.addAttribute("name", name);
return "hello-template"; // templates/hello-template.html 뷰 반환
}
}
hello-template.html)<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
➡️ 브라우저 출력: hello spring
Thymeleaf는 HTML을 그대로 작성하면서, 서버 사이드에서 동적으로 데이터를 바인딩할 수 있는 템플릿 엔진입니다.
템플릿 파일은 resources/templates 경로에 .html 파일로 작성하며, ${} 문법으로 모델 값을 치환합니다.
/hello-mvc?name=springHelloController.helloMvc() 메서드 매핑Model에 담고 "hello-template" 리턴hello-template.html을 탐색