Java/[인프런] Spring 강의

[SpringBoot] #06 - MVC와 템플릿 엔진

냠냠쿠 2023. 8. 10. 22:31
728x90
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8/dashboard

📌 MVC와 템플릿 엔진

  • MVC : Model, View, Controller
    → 이전에는 View에 모두 구현을 했었음.

🔸MVC 구현 해 보기

  • 기본적으로 RequestParam은 값을 넘겨야 하지만, required = false 면 값을 안 넘겨도 됨. 미 설정 시 기본 True 설정되어있음.
//controller
    @GetMapping("hello-mvc")
    public String helloMvc(@RequestParam(name="name", required = false) String name, Model model) {
        model.addAttribute("name", name);
        return "hello-template";
    }
<!--html -->
...
<p th:text="'hello ' + ${name}">hello! empty</p>
...

  • 웹 브라우저에서 http://localhost:8080/hello-mvc를 넘기면 내장서버가 스프링 컨테이너에 전달
  • 스프링은 매핑을 확인하여 메서드를 호출하고 리턴할 때 리턴은 hello-tamplate 모델에는 키는 name, 값은 spring 을 담아 viewResolver로 보냄
  • viewResolver(화면 해결자)가 동작을 하여 뷰를 찾고 템플릿 엔진을 연결해 줌
  • 템플린 엔진이 랜더링 하여 html으로 변환 후 웹 브라우저로 변환
    → 정적에서는 html을 변환하지 않았음.
728x90