스프링 부트와 JPA 활용1 - 프로젝트 환경설정 4
9968 단어 SpringbootJPAJPA
📌 스프링 부트와 JPA 활용1 - 프로젝트 환경설정 4
인프런 - 스프링 부트와 JPA 활용1 by 김영한 을 기반으로 작성된 글입니다.
실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발
프로젝트 환경설정 목차
1. 프로젝트 생성
2. 라이브러리 살펴보기
3. View 환경설정
4. H2 데이터베이스 설치
5. JPA와 DB 설정, 동작 확인
프로젝트 환경설정
3. View 환경설정
1) thymeleaf 템플릿 엔진
- thymeleaf 공식 사이트 : https://www.thymeleaf.org/
Springboot와 thymeleaf 매핑 (HelloController.java)
@Controller
public class HelloController {
//url:hello는 해당 컨트롤러 호출
@GetMapping("hello")
public String Hello(Model model){
model.addAttribute("data", "hello!!!");
return "hello"; //hello.html
}
}
- Model : 컨트롤러에서 데이터를 담아 view로 넘김
- attributeName : data
- attributeValue : hello!!!
- return : ViewName (화면 이름)
resources:templates/ +{ViewName}+ .html
thymeleaf 템플릿엔진 동작 확인 (hello.html)
templates/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>
출력
정적페이지 동작 확인 (index.html)
static/index.html
생성
<!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>
restart 출력
- 정적 콘텐츠는 static에 생성
- 템플릿 엔진을 이용해 렌더링을 이용할 콘텐츠는 templates 에 생성
코드 수정후 빠르게 실행 하는 법!
라이브러리에 아래 코드 추가
'org.springframework.boot:spring-boot-devtools'
gradle import
후 서버 재실행
log에 restartedMain
이라고 떠야 제대로 devtools 세팅 완료!
❗ 혹시 안된다면
우측 상단에 Gradle 메뉴에서 Reload Gradle Project
버튼을 눌러 새로고침 후 재실행
devtools 세팅 이후에는 서버 재실행 대신 Build >Recompile + 파일명
선택하면 완료!
ex) hello.html 수정 후 Build >Recompile hello.html
선택
<p th:text="'안녕!반가워요 ' + ${data}"> 안녕하세요 </p>
새로고침 ( 서버 재실행 보다 빠르게 확인이 가능하다 )
Author And Source
이 문제에 관하여(스프링 부트와 JPA 활용1 - 프로젝트 환경설정 4), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@joajoa/스프링-부트와-JPA-활용1-프로젝트-생성-4저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)