Notice
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
관리 메뉴

Jade_o.o

[Spring] Spring Thymeleaf 템플릿 정의 본문

Spring

[Spring] Spring Thymeleaf 템플릿 정의

by jade 2024. 5. 22. 19:20

Thymeleaf 란?
• 템플릿 엔진
• ex. JSP, Thymeleaf, FreeMarker, …
• HTLM 태그에 속성을 추가해 페이지에 동적으로 값을 추가하거나 처리할 수 있게 도와주는 것
• Spring Boot 사용시 권장되는 템플릿 엔진

 


Controller 만들기
package springstudy.javaspringstudy.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {
	@GetMapping("/hi")
    public String getHi(Model model) {
	  model.addAttribute("msg", "Hi~");
 	  return "hi";
	}
}
@Controller
: 해당 클래스가 Controller 클래스라는 것 을 Spring Container에게 알려준다.
@GetMapping
: URL을 매핑시켜주는 것으로 get method 로 해당 경로로 들어올 시 getHi라는 함수를 실행시킨다.

 


Template view 만들기

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
	<p th:text="'안녕하세요. ' + ${msg}">안녕 반가워</p>
</body>
</html>
xmlns:th="http://www.thymeleaf.org"


Spring Boot 동작 환경
스프링 부트는 WAS로 Tomcat을 내장
1. localhost:8080/hi 요청 시, 톰캣이 스 프링한테 넘겨줌
2. 컨트롤러(HelloController.java)에서 매칭되는 URL 을 찾음
3. 해당 메소드(getHi) 에서 반환하는 문자 열을 ViewResolver 가 화면을 찾아서 처리 (templates/hi.html)

'Spring' 카테고리의 다른 글

[Spring] Thymeleaf 표현식과 문법  (0) 2024.05.22
[Spring] Spring MVC  (0) 2024.05.22
[Spring] Spring Boot 개념과 특징  (0) 2024.05.22
[Spring] Spring 개념과 특징  (0) 2024.05.22