https://start.spring.io/ 스프링 부트 스타터 사이트로 이동

Gradle)빌드는 소스코드 파일들을 컴퓨터에서 실행할 수 있는 소프트웨어로 변환하는 일련의 과정으로 컴파일, 패키징, 테스트, 배포 등의 작업들을 말한다.
빌드 도구는 이러한 빌드 과정을 자동으로 수행해주는 도구를 의미한다.
과거에는 Maven을 주로 사용하였고, 요즘에는 Gradle을 사용한다.
SNAPSHOT, M1 같은 미정식 버전을 제외하고 최신 버전을 사용하면 된다.
추가하고자 하는 라이브러리를 선택한다.
강의에서는 Spring Web과 Thymeleaf를 선택. 추후에는 여러가지 많이 넣어서 편리하게 사용한다.
IntelliJ를 실행
Open > hello-spring/build.gradle을 선택 후 Open
기본 메인 클래스 HelloSpringApplication을 실행

✅ 디렉토리 구조
.idea : Intellij가 사용하는 설정 파일
resources : java 제외한 모든 파일 (html, css, js 등)
test : 테스트 코드 관련 파일
build.gradle : 라이브러리 등의 내용이 포함되어 있음 ⭐
.gitignore : git에 올라가지 않을 파일 설정 가능

오라클 서버가 8080을 사용하여 포트번호를 8081로 변경
✅ 포트 변경 방법 - application.properties
#서버 포트 번호
server.port=8081

✅ IntelliJ Gradle 대신에 자바 직접 실행
최근 IntelliJ 버전은 Gradle을 통해서 실행 하는 것이 기본 설정이다. 이렇게 하면 실행속도가 느리다.
다음과 같이 변경하면 자바로 바로 실행해서 실행속도가 더 빠르다.
File > Settings > Build, Execution, Deployment > Build Tools > Gradle
Build and run using: Gradle > IntelliJ IDEA
Run tests using: Gradle > IntelliJ IDEA

Gradle은 의존관계가 있는 라이브러리를 함께 다운로드 하기 때문에 필요한 것들은 모두 자동으로 추가되어 있다.
<!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>
static/index.html을 올려두면 Welcome page 기능을 제공한다.
스프링 부트는 index.html을 찾고, 없다면 index template를 찾아 자동으로 Welcome page로 사용한다.
thymeleaf 공식 사이트: https://www.thymeleaf.org/
스프링 공식 튜토리얼: https://spring.io/guides/gs/serving-web-content/
[ex] java/hello.hellospring > controller pakage 생성 > HelloController 자바 클래스 생성
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){
model.addAttribute("data", "hello!!!"); //data로 hello!!!가 들어간다, key : value 쌍으로 전달됨.
return "hello"; //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>

컨트롤러에서 Get으로 매핑되어 있는 hello라는 메서드를 찾아 실행시킨다.resources:templates/ + {ViewName} + .html✅ viewResolver
응답 view를 렌더링하는 역할로, view 이름으로부터 view 객체를 매핑하는 역할이다.
viewResolver의 종류는 여러 가지가 있는데,
DispatcherServlet은 디폴트로 InternalResourceView를 사용한다.
🔗 스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술
🔗 https://dmaolon00.tistory.com/121