반응형

# 스프링 프로젝트 환경설정

사전 준비물

  • Java 11 설치
  • IDE : IntelliJ 또는 Eclipse 설치. (가급적이면 IntelliJ 사용)

## 프로젝트 생성

스프링 부트 스타터 사이트 이용하여 프로젝트 생성

  • https://start.spring.io 
  • 아래와 같이 선택 및 입력 후 GENERATE 버튼 클릭하여 프로젝트 생성 진행.
1. Project : Gradle Project (과거에는 Maven 사용했으나, 최근에는 Gradle 사용 스프링 라이브러리 관리 툴)

2. Spring Boot : 2.x.x (최신버전 사용)

3. Project Metadata 
- Group : hello (보통 기업 도메인 작성)
- Artifact : hello-spring (Build 결과물)
- Packaging : Jar
- Java : 11

4. Dependencies (스프링 기반으로 프로젝트에서 사용할 라이브러리 선택)
- Spring Web
- Thymeleaf (템플릿 엔진)

  • GENERATE로 내려받은 프로젝트 특정 폴더에 압축풀기 진행.

IntelliJ 에서 프로젝트 실행

  • IntelliJ 실행 -> GENERATE로 내려받아서 압축해제한 폴더에서 build.gradle 선택하여 Open as Project 진행.

Gradle 설정

  • build.gradle 실행하여 설정된 내역을 확인할 수 있다.
  • 아래와 같이 설정한 dependencies 등 확인 가능
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'
}

.gitignore

  • git에 소스코드 파일 외에 불필요한 파일이 올라가지 않도록 기본적으로 설정되어있음.

 프로젝트 실행하여 동작 확인

  • 아래와 같이 실행 버튼 클릭하여 메인 실행.

## 라이브러리

  • 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 (로그 표준_logbak, slf4j 2가지 운용)

테스트 라이브러리 (기본 : junit)

  • spring-boot-starter-test
junit : 테스트 프레임워크

mockito : 목 라이브러리

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

spring-test : 스프링 통합 테스트 지원

참고

  • spring-boot-devtools 라이브러리 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다.

## View 환경설정

웰컴 페이지 (첫 화면) 생성

  • 아래 경로에 index.html 생성
resources/static/index.html
  • 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>
  • 톰캣 실행 후 localhost:포트번호 입력하여 실행 시 아래와 같이 웰컴 페이지 표시됨.

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

템플릿 엔진

템플릿 엔진 활용

  • 아래와 같이 controller 및 hello.html 생성
  • HelloController.java  (java/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 (resources/templates/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>
<a href="/hello">hello</a>
</body>
</html>

템플릿 엔진 동작 확인

  • http://localhost:포트번호/hello 입력 시 아래와 같이 확인 가능.

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

resources:templates/ + {ViewName} + .html

## 빌드 및 실행

빌드

  • 윈도우의 경우 cmd 아래와 같이 입력하여 빌드 진행.
gradlew(or gradlew.bat) build

  • 빌드가 잘 안되는 경우 clean build 진행
gradlew clean build

실행

  • 윈도우의 경우 cmd 에서 아래와 같이 입력하여 실행.
java -jar hello-spring-0.0.1-SNAPSHOT.jar

 

반응형

+ Recent posts