스프링 입문 - 프로젝트 환경설정 #1

5196 단어 SpringSpring
  • Controller 를 사용하여 웹 정적 페이지를 작성한다.

    package hello.hellospring.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class HelloController {
    
        @GetMapping("hello")
        public String helle(Model model) {
            model.addAttribute("data", "hello!! haha");
            return "hello";
        }
    }
  • templates 라이브러리를 통해 html 작성

    <!DOCTYPE HTML>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Hello</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
    </body>
    </html>
    • 여기서 &{data}는 Controller 의 model.addAttribute("data", "hello!! haha"); 의 리턴값으로 문자를 변환한다.
    • 변환된 문자는 viewResolver 가 화면을 찾아서 처리한다.
      • 스프링 부트 템플릿엔진 기본 viewName 매핑
      • resources:templates/ +{ViewName}+ .html

thymeleaf 템플릿엔진 동작 확인

좋은 웹페이지 즐겨찾기