스프링 부트 프로젝트 생성 및 환경설정

KyungHoon Kim·2022년 9월 20일
0

스프링

목록 보기
1/6

1. 프로그램 설치

• Java JDK 11
• IDE: InteliJ


2. 프로젝트 생성

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

https://start.spring.io/

프로젝트 생성 시 참고

Spring Boot 버전은 SNAPSHOT, M1 같은 미정식 버전을 제외하고 최신 버전을 사용하면 된다.

예) 2.7.4 (SNAPSHOT) 이것은 아직 정식 버전이 아니므로 선택하면 안된다.
예) 2.7.3 이렇게 뒤에 영어가 붙어있지 않으면 정식 버전이므로 이 중에 최신 버전을 선택하면 된다.

프로젝트 생성 후 동작 확인

• 기본 메인 클래스 실행
• 스프링 부트 메인 실행 후 에러페이지로 간단하게 동작 확인( http://localhost:8080 )


3. 환경설정

IntelliJ Gradle 대신에 자바 직접 실행

최근 IntelliJ 버전은 Gradle을 통해서 실행 하는 것이 기본 설정인데, 이렇게 하면 실행속도가 느리다. 그러므로 다음과 같이 자바로 바로 실행하도록 변경하면 실행속도가 더 빠르다.
• File - Setting - Build, Execution, Deployment - Build Tools - Gradle
• Build and run using: Gradle에서 IntelliJ IDEA로 변경
• Run tests using : Gradle에서 IntelliJ IDEA로 변경

IntelliJ JDK 설치 확인

• 프로젝트 JDK 설정
File - Project Structure - SDK 11로 변경

• gradle JDK 설정
File - Setting - Build, Execution, Deployment - Build Tools - Gradle - Gradle JVM 11로 변경

View 환경설정

Welcome Page 만들기

resources/static/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>

스프링 부트는 static/index.html 을 올려두면 Welcome page 기능을 제공한다.

thymeleaf 템플릿 엔진

main/java/hello.hellospring/controller/HelloController

public class HelloController {
    @GetMapping("hello") //웹 어플리케이션에서 /hello로 들어오면 이 메소드를 실행
    public String hello(Model model) { //model에 속성을 준다 (여기서 model은 mvc의 model)
        model.addAttribute("data", "hello!!");
        return "hello"; 
        //resources/templates의 hello.html를 찾아서 가는데, 이 역할은 viewResolver가 수행한다
    }

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>
</body>
</html>

thymeleaf 템플릿엔진 동작 확인

• 실행: http://localhost:8080/hello

컨트롤러에서 리턴 값으로 문자를 반환하면, viewResolver가 화면을 찾아서 처리한다.
스프링 부트 템플릿엔진 viewName 매핑
resources:templates/ +{ViewName}+ .html

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

profile
주니어개발자 시켜줘요

0개의 댓글