Spring Boot入門初體驗(4) --配置thymeleaf模板引擎

thymeleaf源碼解析

package org.springframework.boot.autoconfigure.thymeleaf;//源碼路徑

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.MediaType;
import org.springframework.util.MimeType;

@ConfigurationProperties(
    prefix = "spring.thymeleaf"//在yml或properties文件從新配置
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/"; //默認路徑
    public static final String DEFAULT_SUFFIX = ".html";//加載頁面格式
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
    private boolean cache;
    private Integer templateResolverOrder;
    private String[] viewNames;
    private String[] excludedViewNames;
    private boolean enableSpringElCompiler;
    private boolean enabled;
    private final ThymeleafProperties.Servlet servlet;
    private final ThymeleafProperties.Reactive reactive;

導入thymeleaf模塊

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

yml、properties配置thymeleaf

yml配置:css

spring:
  thymeleaf:
   #開發模式下禁用緩存
   cache: false
   prefix: classpath:/xxxx/

properties配置:html

spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/xxxx/

** Controller編寫 **

@Controller //使用controller跳轉頁面
// 使用 @RestController 會直接返回字符串
public class helloController{
	@GetMapping("sayhello")
	public String getHello(Map<String,Object> map){
		map.put("msg","hello");
		return "sueecss";
	}
}

Html編寫

<!DOCTYPE html>
<!-- 開啓thymeleaf解析器 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org"
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>index</h1>
<div th:text="${msg}">sss</div>
</body>
</html>
相關文章
相關標籤/搜索