[프로젝트 환경설정] View 환경설정 과정

김지수·2023년 7월 20일
0
💡 생성한 폴더/파일명은 [] 안에 표시
  • src/main/java/[hello.hellospring]/[HelloSpringApplication].java
    • hello.hellospring은 Options(왼쪽 메뉴 상단 톱니바퀴) -> Tree Apearance -> Compact Middle Packages 방식이다.
package hello.hellospring;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloSpringApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloSpringApplication.class, args);
	}

}

  • src/main/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>

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", "spring!!");
        return "hello";// 'resources/templates/hello.html' 을 스프링이 찾아서 Thymeleaf 템플릿 엔진이 렌더링 해 줌// 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버(`viewResolver`)가 화면을 찾아서 처리한다.// 스프링 부트 템플릿 엔진 기본 viewName 매핑// `resources(폴더):templates/` + {ViewName} + `.html`
    }
}

  • src/main/resources/templates/[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>
profile
안녕하세요

1개의 댓글

comment-user-thumbnail
2023년 7월 20일

많은 도움이 되었습니다, 감사합니다.

답글 달기