본문 바로가기

Java/Spring

[Spring] View 환경설정

1. Welcome Page

: spring boot는 static/index.html을 올려두면 Welcome page 기능을 제공

 

 

 

 

 

 

2. 정적 페이지

: 파일을 웹서버가 웹브라우저에 그냥 던짐

<!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>

 

 

 

 

3. Thymeleaf 템플릿 엔진

: 템플릿 엔진을 이용해 루프를 넣는 등 원하는 대로 바꿀 수 있음

  스프링 부트는 FreeMarker, Groovy, Thymeleaf, Mustache 제공

 

<!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>

 

 

 

 

 

 

 

4. Controller

  • @Controller 붙여줘야 함
  • @GetMapping("hello") : 웹 어플리케이션에서 /hello로 들어오면 해당 메소드 호출
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";
	}
    
}

 

 

 

 

 

5. 동적 페이지

<!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>

 

 

 

 

 

 

 

 

6. 동작 방법

1. 웹 브라우저에서 localhost:8080/hello 던짐

2. 스프링 부트가 내장하고 있는 톰캣 서버에서 스프링에 던짐

3. Controller에서 @GetMapping("hello") 찾아 해당 메소드 실행

4. 스프링이 Model을 만들어서 넣어주고 model에 Key : data, Value : hello!! 세팅

5. return "hello"에 의해 resources/templates 밑에 동일한 이름의 html 화면을 실행시킴

6. hello.html 화면을 Thymeleaf 템플릿 엔진 처리

 

 

 

컨트롤러에서 리턴 값으로 문자를 반환하면 viewResolver(뷰 리졸버)가 화면을 찾아서 처리함
스프링 부트 템플릿 엔진은 기본적으로 viewName 매핑이 됨
resources/templates/{viewName}.html

 

 

 

 

 

 

 

 

 

 

# 서버 재시작 없이 변경 파일 실행하기

  • spring-boot-devtools 라이브러리 추가하면 html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경 가능
  • IntelliJ 컴파일 방법 : Build - Recompile

2021.10.19 - [기타/IntelliJ] - [IntelliJ] html 서버 재시작 없이 변경 파일 실행

 

[IntelliJ] html 서버 재시작 없이 변경 파일 실행

: spring-boot-devtools 라이브러리 추가하면 html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경 가능 # build.gradle dependencies에 spring-boot-devtools 추가  서버 재시작 없이 Build - Recomp..

hhlin.tistory.com

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

참고 - 인프런 무료 강의 : 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술 [김영한님]
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/dashboard

 

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

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

www.inflearn.com

 

 

spring doc : https://docs.spring.io/spring-boot/docs/current/reference/html/web.html#web

 

Web

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

 

thymeleaf : https://www.thymeleaf.org/

 

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

 

스프링 공식 튜토리얼 : https://spring.io/guides/gs/serving-web-content/

 

Serving Web Content with Spring MVC

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io

스프링 부트 매뉴얼 : https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features

 

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