스프링

스프링부트 라이브러리 및 view 설정

devjones 2021. 12. 25. 21:53

Gradle은 의존관계가 있는 라이브러리를 함께 다운로드 한다.

 

스프링 부트 라이브러리

spring-boot-starter-web spring-boot-starter-tomcat: 톰캣 (웹서버) spring-webmvc: 스프링 웹 MVC

spring-boot-starter-thymeleaf: 타임리프 템플릿 엔진(View) spring-boot-starter(공통): 스프링 부트 + 스프링 코어 + 로깅

spring-boot spring-core

spring-boot-starter-logging logback, slf4j

테스트 라이브러리

spring-boot-starter-test
junit: 
테스트 프레임워크
mockito: 목 라이브러리
assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리 spring-test: 스프링 통합 테스트 지원

 

View 설정

 

프로젝트의 resources/static 폴더에 index.html을 작성한다.

이는 스프링부트의 웰컴페이지다.

https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-welcome-page

 

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

 

thymeleaf 템플릿 엔진 구동하기

타임리프를 테스트할 컨트롤러를 만든다.

그리고 resources/templates/ 경로에 hello.html 파일을 만든다.

서버를 키고, localhost:8080/hello 입력

타임리프가 잘 동작한것을 볼수 있다.

 

컨트롤러의 메소드가 문자열을 리턴할 경우, 뷰 리졸버(view resolver)가 화면을 찾는다.

스프링 부트 템플릿엔진 기본 viewName 매핑

resources:templates/ + {ViewName} + .html

 

※ spring-boot-devtools 라이브러리 추가 시 html 파일을 컴파일만 해주면 서버 재시작없이 view 파일 변경가능

인텔리제이에서 : 메뉴 build -> Recompile