Spring Boot 기초 (01)

yenn·2021년 6월 23일
0

Spring

목록 보기
1/12
post-thumbnail

Spring 공부는 웹 애플리케이션 개발을 해보면서 공부하는 것이 좋음

Spring boot 기반으로 Spring 시작

https://start.spring.io

✔️ Gradle Project

✔️ Group(기업 명)

✔️ Artifact (프로젝트 명)

[InteliJ] 소스파일 알아보기

✔️src : main / test

main (java&resources) : java 파일 제외한 source 파일들

test (java) : test code

✔️ build.gradle

: version, library 설정

✔️ gradlew, gradlew.bat

라이브러리 살펴보기

gradle은 서로 의존관계에 있는 library들을 전부 연결해서 가져옴

"Spring-boot Library"

  1. thyme-leaf

    : 타임리프 템플릿 엔진(View)

  2. starter-web

    • spring-boot-starter-tomcat : tomcat(웹 서버)
    • spring-webmvc : Spring Web MVC
  3. boot-starter : spring-boot + spring-core + logging

    • spring-boot
      ▷ spring-core

    • spring-boot-starter-logging
      ▷ logback, slf4j

"Test Library"

  1. test
    • junit : test framework
    • mockito : mock library
    • assertj : test code 작성을 편하게 도와주는 라이브러리
    • spring-test : 스프링 통합 테스트 지원

View 환경 설정

thyme-leaf templates 엔진

controller package 생성

/hello.hellospring.Controller/HelloController.java

package hello.hellospring.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

// MVC
@Controller
public class HelloController {
    // Get Method
    @GetMapping("hello")
    public String hello(Model model) {
				// model(키,값)
        model.addAttribute("data", "hello!!");
        // return resource/templates hello
        return "hello";
    }
}

/resources/templates/hello.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>
  • controller에서 return 값으로 문자를 반환하려면 viewResolver 가 화면을 찾아서 처리
    • Spring boot templates엔진 기본 viewName mapping
    • resources:templates/ + {ViewName} + .html

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

profile
Junior BackendEngineer 😎

0개의 댓글