Workspace → Refresh using native hooks or polling 체크
폰트 설정 변경 (선택사항)
(주의) : 설치 후 lombok 설정까지 완료해야 프로젝트 오류가 없다.
Lombok 설치
설치 방법
해당 sts 폴더에 lombok 파일 설치 또는 복사 (수업시간때 이미 설치한 파일이 있어 우리는 복사했다)
shift 누른 상태로 우클릭 -> power shell 창 생성
java -jar lo // 입력후 Tab
(주의) : Lombok 미설치 시 @Getter, @Setter 등이 작동하지 않는다.
4. Spring Starter로 프로젝트 생성
Spring Starter 프로젝트 생성
New → Spring Starter Project 선택
Build Tool: Gradle - Grovy
Java Version: 21
Package 3단계 설정
(유의) : import 시에도 Gradle project 를 이용하여 가져와야한다.
(주의) : Group, Artifact, Package 네이밍은 일관성 있게 작성할 것.
Dependency 추가
Spring Web
Spring Boot DevTools
Lombok
Spring Configuration Processor
Java Mail Sender
WebSocket
JDBC API
MyBatis Framework
Oracle Driver
Spring Security
Thymeleaf -> src/main/resources/templates 폴더 자동 생성
(주의) : 필요한 Dependency를 빼먹으면 기능 구현 시 오류가 발생할 수 있다.
5. Build.gradle과 application.properties 설정
build.gradle 설정
수정시 dependedcies {} 내 구문을 수정할 예정
application.properties 설정
# 해당 파일에 설정 작성 시 프로젝트 전반적으로 설정이 적용됨
spring.application.name=demoProject1
# port번호 80으로 변경 (80dms HTTP의 기본 포트 번호로, 브라우저에서 포트번호 없이 접속)
server.port=80
# spring DevTools (자동 리로드, 서버 재시작) 설정
# 브라우저 자동 새로고침 기능
spring.devtools.livereload.enabled=true
# 코드 변경 시 서버 자동 재시작
spring.devtools.restart.enabled=true
# 타임리프 캐시 비활성화 (HTML 파일 수정 시 바로 변경사항 확인할 수 있도록)
spring.thymeleaf.cache=false
# 로그 레벨 지정
logging.level.edu.kh.demo=debug
port 번호가 80 번이면 주소창에 localhost만 입력해도 접속 가능
logging.level 로 설정한 패키지면 주의 **
저장시 UTF-8로 저장 (하니면 주석 한글이 깨진다)
(주의) : 데이터베이스 연결 설정은 보안상 다른 파일로 분리하는 것이 좋다.
6. Spring Boot 프로젝트 구조 이해
기본 폴더 구성
src/main/java
Java 소스 코드
폴더 내에 프로젝트명.Application 파일 자동 생성됨 : main 메서드를 가지고 있으며 프로젝트로 만든 애플리케이션의 실행을 담당하는 클래스
src/main/resources
설정 파일 및 정적 리소스
templates
Thymeleaf HTML 파일 저장
static
CSS, JS, 이미지 파일 저장
application.properties
프로젝트 전역 설정 파일
(주의) : templates 폴더 HTML은 URL로 직접 접근이 불가하고 Controller를 통해 접근해야 한다.
7. index.html 생성 및 기본 서버 실행
index.html 생성
templates 폴더 내 index.html 파일 생성
서버 실행 후 http://localhost로 접속해 기본 화면 확인
(주의) : Spring Boot 기본 설정에서는 /templates/index.html 파일을 자동으로 찾아 보여준다.
@Controller
@RequestMapping("/example")
public class ExampleController {
@GetMapping("/test")
public String testPage() {
return "test";
// templates/test.html 이동
}
}
forward 하려는 html 파일 경로 retun 구문에 작성하는데
ViewResolver 가 제공하는 타임리프의 접두사, 접미사 제외하고 작성한다
return example 인 경우 classpath:/templates/example.html 으로 이동