세팅
Project : gradle
Language : Java
Spring Boot : 안전한 버전 선택
Packaging : Jar
Java : 11 (공부 당시 버전)
Dependencies : Spring Web, Thymeleaf(템플릿 엔진)
Group : hello (기업 도메인 명)
Artifact : hello-spring (빌드의 결과물, 프로젝트 명)
메인 메소드 실행 (Run)
src > main > java > hello > heelospring > HelloSpringApplication.java
src > main > resources > static > index.html(생성 // welcome 페이지)
💎 위 경로 내에 index.html 파일을 자동으로 잡을 수 있도록 세팅되어있다.
controller 생성 - 라우터 설정
src > main > java >hello.hellospring.controlloer.helloController.java
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller // view(화면) return이 주목적인 annotation
public class helloController {
// @GetMapping : GET 요청 방식의 API를 만들 때 사용
// @PostMapping, @PutMapping, @PatchMapping, @DeleteMapping 등 도 있다.
@GetMapping("hello")
public String hello(Model model) { // xml Model 타입, 인자명 model
// Model addAtrubute("key", "value");
// 뷰코드에 key를통해 값을 전달
model.addAttribute("data", "hello!");
return "hello";
// View 리턴
// templates 패키지에 hello.html파일을 위 코드에 설정한 주소로 실행시킨다.
}
}
View 생성
src > main > resources > templates > hello.html(생성)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> // Thymeleaf 사용 표시
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'안녕하세요. ' +${data}">안녕하세요. 손님</p>
</body>
</html>
- Project 안에서 ./gradlew build
- build/libs 폴더로 이동 cd build/libs
- java -jar hello-spring-0.0.1-SNAPSHOT.jar 실행
이후 서버가 실행된다.