[Spring] MVC, 템플릿 엔진

노유성·2023년 7월 7일
0
post-thumbnail

MVC

MVC 패턴에 의해서 View는 View에만 집중하고 controller는 사용자의 입력에 따라 로직을 처리해서 사용자에게 응답을 보내는 에제이다.

Controller

@Controller
  public class HelloController {
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam("name") String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
	}
}

query parameter에서 name에 해당하는 값을 받아서 model에 추가한 후, hello-template.html 파일에 값을 넘겨서 화면에 렌더링한다.

hello-template.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <p th:text="'hello ' + ${name} ">hello! empty</p>
</body>
</html>

전체적인 그릠을 보면 위와 같다.

이와 다르게 정적 컨텐츠는

위와 같이 controller가 없으므로 static 폴더 내에 있는 파일을 가져다 주는 것이다.

profile
풀스택개발자가되고싶습니다:)

0개의 댓글