본문 바로가기

:: System Log

스프링 입문 강의 노트 #3. View 환경설정

 

> 이전글 읽기 : 

1. View 환경설정

 Welcome Page 만들기 -  resources/static/index.html
<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>

 

> 결과 확인

 

2. 스프링 부트가 제공하는 Welcome Page 기능

 

 - static/index.html 을 올려두면 Welcome page 기능을 제공한다 

- thymeleaf 템플릿 엔진

 

> 컨트롤러 기능 추가 

 1) hello.hellospring 하위 controller 패키지 생성

 2) HelloController.java 만들기 

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 hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}

 

 3) resources/templetes/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="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>

 

> 결과확인

   localhost:8080/hello

 
 

> 동작 환경 그림

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

 

  • 스프링부트 템플릿엔진 기본 viewName 매핑
  • resources/templates/ + (ViewName) + .html

 

 

> 다음글 읽기 : 

2022.02.16 - [:: System Log] - 스프링 입문 강의 노트 #4. 빌드하고 실행하기

 

스프링 입문 강의 노트 #4. 빌드하고 실행하기

> 이전글 보기 : 2022.02.10 - [:: System Log] - 스프링 입문 강의 노트 #3. View 환경설정 스프링 입문 강의 노트 #3. View 환경설정 2022.02.10 - [:: System Log] - 스프링 입문 강의 노트 #2. 라이브러리 살..

everythingiok.tistory.com