728x90
1. 프로젝트 생성
- Gradle / Java11 / Spring Boot 2.3.x / Spring Boot web, Thymelaf
2. 라이브러리 살펴보기
의존관계가 있는 라이브러리를 함께 다운로드 한다.
스프링 부트 라이브러리
- 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
- spring-boot
테스트 라이브러리
- spring-boot-starter-test
- junit: 테스트 프레임워크
- mockito: mock 라이브러리
- assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
- spring-test: 스프링 통합 테스트 지원
3. View 환경설정
Welcome Page 만들기
thymeleaf 템플릿 엔진
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("/hello")
public String hello(Model model) {
model.addAttribute("data", "hello");
return "hello";
}
}
- resources/templates/hello.html
<!doctype html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>
<title>Hello</title>
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}">안녕하세요.손님</p>
</body>
</html>
Thymeleaf 템플릿엔진 동작 확인
- 컨트롤러에서 리턴 값으로 문자를 반환하면 viewResolver가 화면을 찾아서 처리한다.
- 스프링 부트 템플릿엔진 기본 viewName 매핑
- resources: templates/ +{ViewName} + .html
4. 빌드하고 실행하기
- 콘솔로 이동
- ./gradlew build
- cd build/libs
- java -jar hello-spring-0.0.1-SNAPSHOT.jar
- 실행 확인
강의자료
728x90
728x90
'Lecture' 카테고리의 다른 글
[프로그래머스-단순 CRUD는 그만! 웹 백엔드 시스템 구현(Sring Boot) 12기] 2주차 세션 정리 (0) | 2022.03.16 |
---|---|
[프로그래머스-단순 CRUD는 그만! 웹 백엔드 시스템 구현(Sring Boot) 12기] 1주차 미션 정리 (0) | 2022.03.11 |
[프로그래머스-단순 CRUD는 그만! 웹 백엔드 시스템 구현(Sring Boot) 12기] 1주차 세션 정리 (0) | 2022.03.01 |