Spring 라이브러리
Spring Boot Library
- 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-starter-logging
-
-
- logback, slf4j
-
Test Library
- junit : 테스트 프레임워크
- mockito : 목라이브러리
- assertj : 테스트코드를 좀 더 편하게 작성하게 도와주는 라이브러리
- spring-test : 스프링 통합 테스트 지원
Controller
src/main/java/packageName/HelloController.java
에서 아래처럼 코딩후
@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>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p th:text="'안녕하세요.' + ${data}">안녕하세요 고객님 </p>
</body>
</html>
위와같이 입력하면, ${data}
위치에 hello!!
가 치환되어서 들어가게된다.
이는,
thymeleaf
템플릿엔진에서 WebBrower 가 local:8080/hello 를 spring boot 에 있는 내장 Tomcat server에 보내고 Tomcat server가 스프링컨테이너에서 hello Controller의 return이 문자(hello) 로 반환되어 있어 View Resolver가 화면을 찾아서 처리해준 결과이다.
나는 thymeleaf
템플릿엔진이 addAttribute 의 첫번재 인자를 두번째 인자로 데이터바인딩 해주었다고 이해하고 넘어갔다.
빌드하고 실행하기
쉘에서 해당 폴더에 들어간 후 ./gradlew build
를 통해 바로 실행시켜준다.
그럼 build
디렉토리가 생긴다.
build
디렉토리에 있는 libs
에 들어가서 java -jar hello-0.0.1-SNAPSHOT.jar
로 .jar 파일을 바로 실행할 수 있다.
.jar
파일을 실행하면 Eclipse 또는 intellij 내에서 Run을 시키지 않아도 바로 서버를 구동시킬 수 있다.