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>
相关文章
相关标签/搜索