Spring/스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술

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

alsruds 2023. 9. 5. 22:23

🙂강의🙂

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

 

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

스프링 입문자가 예제를 만들어가면서 스프링 웹 애플리케이션 개발 전반을 빠르게 학습할 수 있습니다., 스프링 학습 첫 길잡이! 개발 공부의 길을 잃지 않도록 도와드립니다. 📣 확인해주세

www.inflearn.com

 

▧ 목차 ▧

- Welcome Page 만들기
- Thymeleaf 템플릿 엔진

 

[ Welcome Page 만들기 ]

✅ src - resources - static - index.html 생성

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>

 

✅ 웹 브라우저 접속 ( http://localhost:8080 )

성공! (정적 페이지)

 

  • https://spring.io/ 접속해서 작동 방식 살펴보기
    • Projects - Spring Boot - Learn - Spring Boot Features - 7. Developing Web Applications - 7.1.6 Welcome Page

 

[ Thymeleaf 템플릿 엔진 ]

 

Thymeleaf

Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati

www.thymeleaf.org

 

Getting Started | Serving Web Content with Spring MVC

Static resources, including HTML and JavaScript and CSS, can be served from your Spring Boot application by dropping them into the right place in the source code. By default, Spring Boot serves static content from resources in the classpath at /static (or

spring.io

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

 

1. Controller 생성

  • src/main/java/hello.hellospring 에 controller package 생성
  • controller package 밑에 HelloController 클래스 생성
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) {
        // key: data , value: hello!!
        // hello.html 에서 ${data} 호출 시 hello!! 반환
        model.addAttribute("data", "hello!!");

        // return 값은 src/resources/templates 내 html 의 이름과 같아야 함 : rendering
        return "hello";
    }
}

 

2. src/resources/templates 에 hello.html 생성

<!DOCTYPE html>
<!-- thymeleaf 문법 사용 가능 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<!-- key 값이 data 인 value 반환 : hello!! (HelloController 에서 정의) -->
<p th:text="'안녕하세요. ' + ${data}">안녕하세요. 손님</p>
</body>
</html>

 

3. 웹 브라우저 확인

  • GetMapping

 

  • 웹에서 HTML 확인

hello!! 로 바뀌어 있다 ~

 

  • 동작 확인

→ Controller 에서 return 값으로 문자 반환 시 viewResolver 가 화면을 찾아 처리

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