프로젝트 생성
스프링 부트 스타터 사이트로 이동해서 스프링 프로젝트 생성
- 프로젝트 선택
- Project: Gradle - Groovy Project
- Spring Boot: 3.x.x
- Language: Java
- Packaging: Jar
- Java: 17 또는 21
- Project Metadata
- groupId: hello
- artifactId: hello-spring
- Dependencies: Spring Web, Thymeleaf
스프링 부트 3.0 이상 사용시 주의점
1. Java 17 이상
2. javax 패키지 이름을 jakarta로 변경
3. H2 데이터베이스를 2.1.214 버전 이상 사용
- 동작 확인
- 기본 메인 클래스 실행
- 스프링 부트 메인 실행 후 에러페이지로 간단하게 동작 확인(
http://localhost:8080
)
IntelliJ Gradle 대신에 자바 직접 실행
최근 IntelliJ 버전은 Gradle을 통해서 실행 하는 것이 기본 설정이다.
이렇게 하면 실행속도가 느리다. 다음과 같이 변경하면 자바로 바로 실행해서 실행속도가 더 빠르다.
- Preferences(윈도우의 경우 File -> Setting) -> Build, Execution, Deployment -> Build Tools -> Gradle
- Build and run using: Gradle -> IntelliJ IDEA
- Run tests using: Gradle -> IntelliJ IDEA
라이브러리 살펴보기
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
- spring-boot-starter-logging
- logback, slf4j
테스트 라이브러리
- spring-boot-starter-test
- junit: 테스트 프레임워크
- mockito: 목 라이브러리
- assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
- spring-test: 스프링 통합 테스트 지원
View 환경설정
Welcome Page 만들기
<!-- src/main/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>
- 스프링 부트가 제공하는 Welcome Page 기능
- static/index.html 을 올려두면 Welcome page 기능을 제공한다.
- https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-bootfeatures.html#boot-features-spring-mvc-welcome-page
thymeleaf 템플릿 엔진
- 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
Thymeleaf
Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati
www.thymeleaf.org
Getting Started | Serving Web Content with Spring MVC
Static resources, including HTML and JavaScript and CSS, can be served from your Spring Boot application by dropping them into the right place in the source code. By default, Spring Boot serves static content from resources in the classpath at /static (or
spring.io
Spring Boot Features
Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest
docs.spring.io
// src/main/java/hello/hellospring/controller/HelloController.java
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") //웹 어플리케이션에서 /hello 일 때
public String hello(Model model){ // MVC 할때 그 모델
model.addAttribute("data","spring!!");
return "hello"; //hello.html을 찾음
}
}
<!-- src/main/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>
<!--{data} : 데이터 모델 attribute에 key로 넣었던 데이터의 value가 들어감-->
</body>
</html>
thymeleaf 템플릿엔진 동작 확인
src/main/java/hello/hellospring/HelloSpringApplication.java
의 main 함수 실행- 실행 : http://localhost:8080/hello
- 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버( viewResolver )가 화면을 찾아서 처리한다.
- 스프링 부트 템플릿엔진 기본 viewName 매핑
- resources:templates/ +{ViewName}+ .html
spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 view 파일 변경이 가능하다.
인텔리J 컴파일 방법: 메뉴 build Recompile
빌드하고 실행하기
콘솔로 이동
- ./gradlew build
- cd build/libs
- java -jar hello-spring-0.0.1-SNAPSHOT.jar
- 실행 확인
윈도우
- 콘솔로 이동 명령 프롬프트(cmd)로 이동
./gradlew
->gradlew.bat
실행- 명령 프롬프트에서
gradlew.bat
를 실행하려면gradlew
하고 엔터 gradlew build
- 폴더 목록 확인
ls
->dir
- 윈도우에서 Git bash 터미널 사용하기
[[ 강의 시청 Tip ]] 윈도우라서 맥의 iTerm이 없는데 어떡하나!? - 인프런 | 질문 & 답변
`윈도우라서 맥의 iTerm이 없는데 어떡하나!?``Windows 환경에서 Terminal 사용 시, 리눅스 명령어 사용해볼 수 없나?`고민하시는 분들 아래 링크를 참고하시면, 리눅스 명령어 사용하며 공부하실 기회
www.inflearn.com
GitHub 댓글