[스프링 개발 입문] 1. 프로젝트 환경설정

HJ·2022년 7월 17일
0
post-thumbnail

김영한 님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹 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


1. 프로젝트 생성

  • 프로젝트 선택
    • Project: Gradle Project
    • Spring Boot: 2.7.1
    • Language: Java
    • Packaging: Jar
    • Java: 11
  • Dependencies
    • Spring Web
    • Thymeleaf



2. 라이브러리 살펴보기

2-1. 빌드 관리 툴

  • gradle과 maven 같은 buile tool은 의존관계를 관리해준다

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

    • ex> spring-boot-starter-web 라이브러리를 가져오면 tomcat, web과 같이 의존관계가 있는 라이브러리들을 자동으로 가져온다


2-2. 스프링부트 라이브러리

  • spring-boot-starter-web

    • spring-boot-starter-tomcat: 톰캣 (웹서버)

    • spring-webmvc: 스프링 웹 MVC

  • spring-boot-starter-thymeleaf: 타임리프 템플릿 엔진(View)

  • spring-boot-starter : springboot와 관련된 프로젝트를 사용하면 의존관계로 자동으로 import

    • spring-core

    • spring-boot-dependencies

    • spring-boot-autoconfigure

    • spring-boot-starter-logging ( log 파일 관리를 위해 사용 )

      • logback ( 요즘에 많이 사용 )

      • slf4j ( 인터페이스 )


2-3. 테스트 라이브러리

  • spring-boot-starter-test

    • junit: 테스트 프레임워크 (JUnit5)

    • mockito: 목 라이브러리

    • assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리

    • spring-test: 스프링과 통합해서 테스트 지원하는 라이브러리




3. View 환경설정

3-1. 정적 페이지와 동적 페이지

  • resources/static에 index.html을 생성 (정적페이지)

    • springboot는 static에서 가장 먼저 index.html을 찾게됨
  • 템플릿 엔진 사용가능 ( 동적 페이지 )

    • FreeMarker

    • Groovy

    • Thymeleaf

    • Mustache


3-2. view 동작 흐름

  • url에 매칭된 Controller의 메소드를 실행

  • 메소드는 model을 생성하고 html 파일의 이름을 return

    • html의 위치는 resuources/templates

    • model을 해당 html 파일에 넘긴다

  • viewResolverresuources/templates에서 return값에 해당하는 파일명을 찾아서 처리


3-3. Controller

  • Controller : 웹 어플리케이션에서 첫 번째 진입점
< HelloController >

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model) {
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}

< 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>
  • model을 사용할 때는 model의 이름으로 사용

0개의 댓글