상세 컨텐츠

본문 제목

[스프링 핵심 원리 - 기본편] week01

[SW]/[Spring 스터디] 2022

by 시원00 2022. 8. 4. 22:59

본문

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

https://www.inflearn.com/course/스프링-입문-스프링부트/dashboard

 

섹션 0. 강의 소개

 

간단한 웹 애플리케이션 개발

- 스프링 프로젝트 생성

- 스프링 부트로 웹 서버 실행

- 회원 도메인 개발

- 웹 MVC 개발

- DB 연동 - JDBC, JPA, 스프링 데이터 JPA

- 테스트 케이스 작성

 

 

섹션 1. 프로젝트 환경설정

 

1.1 프로젝트 생성

https://start.spring.io -> 기본 설정
파일 확인 -> InteliJ에서 실행
main과 test 확인
build gradle
RUN -> 8080 확인
RUN -> http://localhost:8080 (성공)
RUN 종료 -> 페이지 열기 실패

 

 

1.2 라이브러리 살펴보기

 

기본 라이브러리 살펴보기

 

 

핵심 라이브러리

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: 테스트 프레임워크 (최근 junit5 사용)
    • mockito: 목 라이브러리
    • assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
    • spring-test: 스프링 통합 테스트 지원

 

 

1.3 View 환경설정

 

Welcome Page 만들기

 

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

  • static/index.html 을 올려두면 Welcome Page 기능을 제공한다.

src/main/resource/static - New - File
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>

재실행 (완료)

 

spring boot 기능 확인

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

 

Spring Boot Reference Documentation

The reference documentation consists of the following sections: Legal Legal information. Getting Help Resources for getting help. Documentation Overview About the Documentation, First Steps, and more. Getting Started Introducing Spring Boot, System Require

docs.spring.io

 

welcome page 기능 확인 (spring.io)

spring.io
welcome page 설명 확인

 

thymeleaf 템플릿 엔진

  • thymeleaf 공식 사이트: https://www.thymeleaf.org/

New Package : hello.hellospring.controller New File : HelloController
주소/hello (@GetMapping("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";
   }
}

templates - New File : hello.html
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>

http://localhost:8080/hello (결과)

 

동작 환경 그림

 

  • return "hello"; - resources/templates/hello.html 실행
  • 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버('viewResolver')가 화면을 찾아서 처리한다.
    • 스프링 부트 템플릿엔진 기본 viewName 매핑
    • resources:templates/ + (ViewName) + .html
  • (참고) spring-boot-devtools 라이브러리를 추가하면 html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다.
  • 인텔리J 컴파일 방법: 메뉴 build -> Recompile

 

 

 

1.4 빌드하고 실행하기

 

콘솔로 이동

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

build 성공

 

FIN.

관련글 더보기

댓글 영역