GENERATE - 압축 풀기 - IntelliJ에서 build.gradle를 프로젝트로 열기
build.gradle
testImplementation 'org.springframework.boot:spring-boot-starter-test'
↓ 수정
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
HelloSpringApplication.java 실행
에러 페이지가 뜨지만 성공한 거임!
IntelliJ Gradle 대신에 자바 직접 실행
IntelliJ에서 단축키를 확실하게 검색하는 방법
spring-boot-starter-test
junit: 테스트 프레임워크
mockito: 목 라이브러리
assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
spring-test: 스프링 통합 테스트 지원
resources/static/index.html 생성
static/index.html을 올려두면 Welcome page 기능을 제공한다.
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>
7.1.6. Welcome Page
Spring Boot supports both static and templated welcome pages. It first looks for an index.html file in the configured static content locations. If one is not found, it then looks for an index template. If either is found, it is automatically used as the welcome page of the application.
1) static 영역에서 index.html 찾기
2) 없으면 index 템플릿 찾기
hello.hellospring/controller 패키지 생성/HelloController 클래스 생성
hello.hellospring/controller/HelloController
package helllo.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
// 웹 어플리케이션에서 /hello로 접속하면 아래 메소드 호출
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
// return "hello"를 하면 스프링은 resources/templates 아래에 있는 hello를 찾아 렌더링
}
}
웹 어플리케이션에서 /hello로 접속하면 아래 메소드 호출
@GetMapping("hello")
public String hello(Model model) {
}
templates/hello.html 생성
hello.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<!--thymeleaf 템플릿 엔진 선언-->
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
<!--data가 hello!!로 치환-->
</body>
</html>
http://localhost:8080/hello 접속
참고
spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능하다.
인텔리J 컴파일 방법: 메뉴 build Recompile
윈도우 사용자를 위한 팁
hello-spring>gradlew.bat build
hello-spring\build\libs>dir
hello-spring\build\libs>java -jar hello-spring-0.0.1-SNAPSHOT.jar