버전 : Java 17
IDE : IntelliJ

javax.persistence.Entity → jakarta.persistence.Entityjavax.annotation.PostConstruct → jakarta.annotation.PostConstructopen - 압축을 푼 디렉토리에 있는build.gradle선택


: IntelliJ IDEA 빌드 시, 기본으로 셋팅되어 있는 Gradle 보다 자체 IDEA로 실행하는 게 더 빠름
ctrl + alt + s or File → Settings
build.gradle - 추가한 라이브러리들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'
}



System.out.print()사용을 지양하고 로그파일을 통해 테스트를 할 것slf4j와 logback가 있음
spring-boot-starter-webspring-boot-starter-tomcat : 톰캣(웹서버)spring-webmvc : 스프링 웹 MVCspring-boot-starter-thymeleaf : 타임리프 템플릿 엔진(View)spring-boot-starter(공통) : 스프링 부트 + 스프링 코어 + 로깅spring-bootspring-corespring-boot-starter-logginglogback, slf4jspring-boot-starter-testjunit : 테스트 프레임워크mockito : 목 라이브러리assertj : 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리spring-test : 스프링 통합 테스트 지원
src/main/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>src/main/hellospring에 Controller패키지 생성 후 HelloController.java 파일 생성
HelloController.java 작성
ViewResolver가 hello.html을 찾아서 화면에 띄운다resources:templates/ + ViewName + .htmlpackage 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";
}
}
src/main/resources/template에 hello.html 생성 후 작성
xmlns:th="http://www.thymeleaf.org"로 템플릿 엔진 선언th : thymeleaf 의미<!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> <!-- data는 model의 키값 -->
</body>
</html> 참고
spring-boot-devtools라이브러리 추가
→html파일을 컴파일만 해주면 서버 재시작 없이 View파일 변경 가능- IntelliJ 컴파일 방법 : 메뉴 build → Recompile
./gradlew buildcd build/libsjava -jar hello-spring-0.0.1-SNAPSHOT.jar출처 - 인프런 '스프링 입문' 강의