SpringBoot——》Thymeleaf

一、項目依賴配置

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

二、thymeleaf參數配置

參數對應jar包(版本可變動):spring-boot-autoconfigure-2.1.1.RELEASE.jar
參數對應java類:org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
注:在 application.ymlapplication.property 中配置
在這裏插入圖片描述

spring:
  # 模板引擎
  thymeleaf:
    # Thymeleaf3.0模板:HTML (默認)、XML、TEXT、JAVASCRIPT、CSS、RAW
    mode: HTML
    encoding: utf-8
    # 禁用緩存:避免改了模板還要重啓服務器
    cache: false
  # 資源信息
  messages:
    # 國際化資源文件路徑
    basename: static/i18n/messages

1、默認模板存放路徑

默認值:classpath:/templates/
可修改:

spring:
  thymeleaf:
    prefix:/templates/  

2、默認模板後綴

默認值:.html
可修改:

spring:
  thymeleaf:
    suffix:.html

3、默認靜態資源路徑 (4種)

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/
    在這裏插入圖片描述

三、示例:Controller和index.html

src/main/java/com/controller下新建 WebController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
 
@Controller
public class WebController {
 
    @GetMapping("/index")
    public String index(){
        return "index";
    }
}

classpath:/resources/templates下新建 index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>我是小仙</h1>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章