프로젝트/Spring Boot
Spring Boot Thymeleaf 이용하여 간단한 예제 만들기
라임07
2022. 10. 2. 18:17
실습환경
인텔리제이(Intellij), VS Code, Spring Boot
실습하기
프로젝트를 새로만들어 위와 같이 세팅합니다.
- 의존성(Dependency)
- Spring Web
- Spring Boot DevTools
- Thymeleaf
- com.example.thymeleaf.controller 안에 GreetingController 파일
@Controller
public class GreetingController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model){
model.addAttribute("name", name);
return "greeting";
}
}
- templates 안에 Greeting.html 파일
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" /></p>
</body>
</html>
정상적으로 실행되었음을 확인할 수 있습니다.
※ 파라미터로 ?name=(텍스트)를 붙이면 (텍스트)가 출력됩니다.
팁(Tip)
VS Code 들어가서 Live Server 확장 프로그램을 설치합니다.
[프로젝트 설치 경로] : thymeleaf → src → main → resources 폴더선택하여 폴더를 엽니다.
<p></p> 사이에 "테스트" 글자를 작성하고, 우클릭 후 Open with Live Server 클릭합니다.
이제 테스트2로 고친 후 저장(Ctrl + S)하면 화면에 바로 테스트2로 바뀌었음을 알 수 있습니다.
정상적으로 실행되었음을 알 수 있습니다.
참고자료
https://spring.io/guides/gs/serving-web-content/
Serving Web Content with Spring MVC
this guide is designed to get you productive as quickly as possible and using the latest Spring project releases and techniques as recommended by the Spring team
spring.io