Skip to content

섹션1. 프로젝트 환경설정 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# 섹션1. 프로젝트 환경설정
## 프로젝트 생성

스프링 부트
start.spring.io

과거에는 Maven 자주 사용했으나 최근에는 Gradle을 많이 사용하는 추세
-> 지금은 Gradle이 버전 설정하고 라이브러리 가지고 오는구나 정도로 이해하면 충분

스프링 프로젝트를 만들고 src 폴더를 보면 main과 test가 나뉘어 있음
-> Test 폴더 안에 테스트 코드가 들어감
-> 분리해서 따로 두었다는 것은 테스트 코드가 그만큼 중요하다는 것

@SpringBootApplicaion아래
Main 메소드를 실행하면 SpringApplication을 run해서 HelloSpringAppplication 클래스를 넣어주면 스프링 애플리케이션이 실행 됨
-> 톰캣(TomCat) 이라는 웹서버를 자체적으로 띄워줌

번외
설정 – gradle 검색 – build and run using: IntelliJ IDEA/ Run tests using: IntelliJ IDEA로 설정
-> Gradle을 거쳐서 java를 띄우지 않고 IntelliJ에서 java를 띄우기 때문에 더 빠름

## 라이브러리 살펴보기
스프링 부트 라이브러리
- spring-boot-starter-tomcat : 톰캣(웹서버)
-> 톰캣은 자바를 기반으로 하는 웹 애플리케이션 서버(Web Application Server, WAS) 중 하나로, 자바 서블릿(Java Servlet)을 구동하는 데 필요한 환경을 제공. 스프링 부트에서는 이러한 톰캣을 내장하여, 별도의 웹서버 설정 없이도 웹 애플리케이션을 빠르게 구동하고 테스트할 수 있게 함.
- spring-boot-starter-thymeleaf : 타임리프 템플릿 엔진(View)
- spring-boot-starter(공통) : 스프링 부트 + 스프링 코어 + 로깅
- spring-boot
- spring-core
- spring-boot-starter-logging (로그 관리)
- logback, slf4j

테스트 라이브러리
- spring-boot-starter-test
- junit: 테스트 프레임워크
- mokito: 목 라이브러리
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

목이 뭔가요? 간단하게라도 설명해주세요!

- assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
- spring-test: 스프링 통합 테스트 지원


## View 환경설정

### Welcome Page 만들기

resoures/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-boot-features.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

@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
}

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

동작 환경 그림
![](%E1%84%89%E1%85%A6%E1%86%A8%E1%84%89%E1%85%A7%E1%86%AB1.%20%E1%84%91%E1%85%B3%E1%84%85%E1%85%A9%E1%84%8C%E1%85%A6%E1%86%A8%E1%84%90%E1%85%B3%20%E1%84%92%E1%85%AA%E1%86%AB%E1%84%80%E1%85%A7%E1%86%BC%E1%84%89%E1%85%A5%E1%86%AF%E1%84%8C%E1%85%A5%E1%86%BC/image.png)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그림이 안 보여요..

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

> 참고: spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다.> 인텔리J 컴파일 방법: 메뉴 build -> Recompile


## 빌드하고 실행하기

### 콘솔로 이동1. ./gradlew build
2. cd build/libs
3. java -jar hello-spring-0.0.1-SNAPSHOT.jar
4. 실행확인


### 윈도우 사용자를 위한 팁
- 콘솔로 이동 -> 명령 프롬프트(cmd)로 이동
- ./gradlew gradlew.bat 를 실행하면 됩니다.
- 명령 프롬프트에서 gradlew.bat 를 실행하려면 gradlew 하고 엔터를 치면 됩니다.
- gradlew build
- 폴더 목록 확인 ls dir
- 윈도우에서 Git bash 터미널 사용하기
- 링크: https://www.inflearn.com/questions/53961
Expand Down