인프런 김영한 님의 스프링 입문 - 코드로 배우는 스프링 부트, 웹, MVC, DB 접근 기술 강의를 보고 정리한 내용입니다.
Java 설치 필수 → oracle jdk (원하는 버전) 검색 후 다운로드
스프링 부트 스타터 사이트에서 스프링 프로젝트 생성
프로젝트 선택
다운로드 → intellij open → 다운로드 한 파일의 build.gradle 선택 → open as project
💡 gradle build가 안될 때
1. ⌘; (File → Project structure) Project의 SDK 버전 다운로드 받은 파일의 버전과 같이 변경
2. ⌘, (Intellij → Settings → Build, Execution, Deployment → Build tools → Gradle) gradle JVM 버전 다운로드 받은 파일의 버전과 같이 변경
plugins { // 버전 관련
id 'java'
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
}
group = 'hello'
version = '0.0.1-SNAPSHOT'
java { // java 버전
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
repositories { // 라이브러리를 여기서 다운받아라 라고 설정해두는,,
mavenCentral()
}
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'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
tasks.named('test') {
useJUnitPlatform()
}
Gradle이 프로젝트를 시작할 때 선택한 라이브러리와 의존 관계가 있는 라이브러리를 함께 다운로드 한다.Gradle 버튼을 클릭하면 라이브러리 간의 의존 관계로 함께 당겨와진 라이브러리들을 확인할 수 있다.resources/static 경로에 index.html 생성static/index.html 을 올려두면 Welcome page 기능을 제공한다.index.html 파일을 미리 설정된 위치 (resources/static)에서 찾는다.index 템플릿을 찾는다.💡 Welcome Page는 Application에 실제로
index의 경로가 정의되어 있지 않을 경우에만 동작한다.
→ Application이index라는 경로에 (ex:/index,/)에 대한 명시적인 라우트(route)를 가지고 있다면, 그 라우트가 우선 사용된다.
없을 경우 index.html 파일이나 index 템플릿을 찾아 Welcome Page로 사용한다.
thymeleaf 공식 사이트: https://www.thymeleaf.org/
스프링 공식 튜토리얼: https://spring.io/guides/gs/serving-web-content/
스프링부트 메뉴얼: https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-template-engines
Model 객체에 담긴 데이터를 템플릿으로 쉽게 전달할 수 있다 (Spring Boot에서는 기본 템플릿 엔진으로 Thymeleaf가 설정되어 있어 쉽게 사용할 수 있음)${}: 모델에 있는 데이터를 출력할 때 사용 (예: <span>${user.name}</span>).th:if, th:each: 조건문과 반복문을 HTML 안에서 처리할 수 있게 해줌 (예: <li th:each="item : ${items}">${item.name}</li>).
localhost:8080/hello 입력http://localhost:8080/hello로 접근하면 해당 요청이 애플리케이션으로 전달된다./hello 경로에 매핑된 메서드가 있는지 확인addAttribute)return): 메서드는 "hello"라는 View 이름을 반환한다. Spring Boot는 이 이름을 사용해 특정 HTML 파일을 찾는다.resources/templates 경로의 ViewName을 찾아서 렌더링 한다.resources:templates/ + {ViewName} + .html