반응형

# 라이브러리

  • 기존에 스프링 부트로 생성한 프로젝트의 build.gradle 을 살펴보면 아래와 같이 3가지의 라이브러리 존재.
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
  • External Libraries를 확인해 보면 다수의 라이브러리 존재.
  • gradle, maven 의 경우 의존관계에 의해 build.gradle 에 존재하는 라이브러리와 의존관계에 있는 것들을 모두 가져와서 External Libraries에 가져오게 됨.

라이브러리 의존관계 확인.

  • 위 처럼 인텔리제이의 경우 왼쪽하단 네모에 마우스 호버 후 gradle 클릭시 우측상단에 gradle 표시. 해당 팝업에서 dependencies 에서 라이브러리 의존관계 확인 가능.

  • 실무에서는 System.out.println 으로 출력하면 않되고 log로 출력해야 함 그래야 관리가 됨. (logback, slf4j 조합하여 많이 사용)
  • 테스트 관련하여 사용하는 핵심라이브러리는 junit. (추가로 테스트 도와주는 라이브러리로 mockito, assertj 사용)

## 핵심 라이브러리 정리

  • spring-boot-starter-web
// 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 환경설정

## Welcome 페이지 만들기

  • resources/static/index.html 생성. (정적 페이지)
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Title</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
HELLO
<a href="/hello">hello</a>
</body>
</html>

## thymeleaf 템플릿 엔진

 

Thymeleaf

Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati

www.thymeleaf.org

 

Serving Web Content with Spring MVC

this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team

spring.io

 

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

  • 컨트롤러 : 웹 에플리케이션에서 첫 번째 진입점.

패키지 생성 &gt; 컨트롤러 생성

패키지 생성 : hello.hellospring.controller

컨트롤러 생성 : HelloController
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";
    }
}
  • 동적 페이지 생성 (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>

  • 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버(viewResolver) 가 화면을 처리.
스프링 부트 템플릿엔진 기본 viewName 매핑

resources : templates/ + {ViewName} + .html
  • spring-boot-devtools 라이브러리 : 해당 라이브러리 추가 시, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다. 
  • 인텔리제이 컴파일 방법 : 메뉴 build > Recompile

 

# 빌드 후 실행하기

  • 명령 프롬프트 cmd 실행하여 해당 프로젝트 레포지토리로 이동.
  • gradlew 입력하여 빌드 진행.

  • 완료 후 gradlew build 입력.

  • 완료 후 build/libs 진입하여 java -jar jar파일명 입력하여 실행.

 

반응형

+ Recent posts