Server/Spring
[Spring] 인프런 강의 입문- 1 프로젝트 환경설정
댕경
2021. 9. 24. 03:07
728x90
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8%EF%BB%BF
www.inflearn.com
2. 라이브러리 살펴보기
프로젝트 생성시 추가한 'web' 'thymeleaf' 에서 의존된 것들 다 불러온 것
Gradle → Dependencies : Gradel 은 의존관계가 있는 라이브러리를 함께 다운로드!
3. View 환경설정
index.html : http://localhost:8080
hello.html : http://localhost:8080/hello
HelloController
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello") // "/hello" 로 집입 시 여기로 Mapping!!!
public String hello(Model model){
model.addAttribute("data","hello!!!"); // 1)
return "hello"; // 2)
// data 는 모델을 넘기면서, "hello"로 리턴
}
}
1) model.addAttribute("data","hello!!!");
- model 의 "data"에 키값으로 "hello!!!"를 넘겨주자.
2) return "hello";
- template->hello 를 찾아간다.
hello.html
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"> <!--thymeleaf 사용가능 -->
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello</title>
</head>
<body>
<p th:text="'안녕하세요. '+ ${data}"> <!--${data} : 모델의 키값 가져옴 -->
안녕하세요. 손님
</p>
</body>
</html>
컨트롤러에서 리턴 값으로 문자 "hello!!!" 를 반환하면 ViewResolver 가 화면 "hello" 를 찾아서 처리한다.
스프링 부트 템플릿엔진 기본 viewName 매핑
resources:templates/ +{ViewName}+ .html