강의 기록/스프링 입문(김영한)
[스프링 입문] Section 1.프로젝트 환경설정: View 환경설정 (이클립스)
momong'-'
2021. 3. 15. 16:35
Section 1
- 프로젝트 생성
- 라이브러리 살펴보기
- View 환경설정
- 빌드하고 실행하기
View 환경설정
인텔리제이를 안쓰므로 이클립스를 사용함
src > main > resources > new > File 클릭> 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>
8080 server port가 사용중이라 server 포트를 변경해주었다. 서버 포트 변경바방법
스프링부트가 제공하는 welcome page 기능
spring.io 들어가서 projrct > spring boot > Learn > current의 Refernce Doc.
7.1.6 Welcome Page 정보
7.1.10 Template Engines 정보
controller 생성
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")
public String hello(Model model) {
model.addAttribute("data", "Hello!!");
// attributeName: data
// attributeValue: Hello!!
return "hello";
}
}
<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>
<!-- th: 타임리프 문법 -->
</body>
</html>
컨트롤러에서 리턴 값으로 문자를 반환하면 ViewResolver가 화면을 찾아 처리
- 스프링 부트 템플릿엔진 기본 viewName 매핑
- resources:templates/ +{ViewName}+ .html
=> 참고: spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능