JAVA/스프링 입문

인프런 | 스프링 입문 - 프로젝트 환경설정

한 면만 쓴 종이 2022. 7. 15. 14:34

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

 

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

 

" 스프링 부트 라이브러리 "

spring-boot-starter-web

  • sping-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: 스프링 통합 테스트 지원

 

( log에 관해 궁금하면 slf4j랑 logback 공부해보기 )

 

View 환경설정

 

Welcome Page 만들기

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>

 

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

  • static/index.html 을 올려두면 Welcome page 기능을 제공
  • https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-featurse.html#boot-features-spring-mvc-welcome-page

tymeleaf 템플릿 엔진

  • tymeleaf 공식 사이트: https://www.thymeleaf.org/
  • 스프링 공식 튜토리얼: https://spring.io/guides/gs/serving-web-content/
  • 스프링부트 메뉴얼: https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-template-engines

resouces/template/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>

th: tymeleaf

 

"thymeleaf 템플릿엔진 동작 확인"

  • 실행: https://localhost:8080/hello

동작 환경 그림

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

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

 

빌드하고 실행하기

콘솔로 이동

  1. `gradlew.bat build`
  2. `cd build/libs`
  3. `java -jar hello-spring-0.0.1-SNAPSHOT.jar`
  4. 실행확인