Web/Spring

[Spring] View 환경 설정

chaenii 2022. 9. 17. 12:57

Welcome Page 만들기

  • 스프링 부트가 제공하는 Welcome Page 기능
    • static/index.html 을 올려두면 Welcome page 기능을 제공한다.
    • 정적 페이지를 띄워준다.
  • Spring boot가 제공하는 기능 찾아보기
    • spring.io
    • projects → Spring Boot → Learn → Reference Doc 참고


thymeleaf 템플릿 엔진

  1. localhost:8080/hello로 접근
    • GetMapping annotation에 의해 hello 함수에 매칭된다.
  2. hello 함수 실행
    • model에 attribute를 추가해준다.
    • hello를 return 해준다.
@GetMapping("hello")
public String hello(Model model){
    model.addAttribute("name","spring");
    return "hello";
}

   3. 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버( viewResolver )가 화면을 찾아서 처리한다.

  • 스프링 부트 템플릿엔진 기본 viewName 매핑
  • resources:templates/ +{ViewName}+ .html
# hello.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="'안녕하세요. ' + ${name}" >안녕하세요. 손님</p>
</body>
</html>

  4. 실행 결과


스프링 입문

 

[무료] 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 - 인프런 | 강의

스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다., - 강의 소개 | 인프런...

www.inflearn.com

 

반응형