정적 페이지
정적페이지는 말그대로, 파일 그 자체를 받아오는 것입니다.
resources/static에 있는 hello-static.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
static content page.
</body>
</html>
해당 페이지를 불러올려면, "localhost:8080/hello-static.html" URL로 요청을하면, 해당 HTML 파일 자체를 불러옵니다.
동적 페이지
Controller생성
스프링은 MVC구조이기 때문에, 우선 Controller를 생성합니다.
helloController를 생성하고
@Controller
public class helloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello");
return "hello";
}
}
위의 코드를 추가합니다.
@GetMapping("hello")가 의미하는 것은 GET방식으로 "hello"로 요청이 왔을 경우
즉 "localholst:8080/hello"의 URL로 요청이 왔을 경우 해당 함수를 실행하라는 의미입니다.
Model은 view의 html에서 변수내용으 랜더링 해주기 위해서 사용합니다.
model.addAttribute("data", "hello"); 에서 data는 변수명(key값), hello는 value값에 해당합니다.
template파일 생성
templates폴더 안에 hello.html 파일을 생성합니다.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'안녕하세요 . ' + ${data}" ></p>
</body>
</html>
thymeleaf를 이용하기 위해서, 위의 선언을 해주고, th:text를 이용하면, 해당 태그안에 내용을 삽입해줄 수 있습니다.
${data}는 위의 controller에서 만든 key값을 의미하고, value값인 hello가 여기에 대입되게 됩니다.