thymeleaf

一、引入场景

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

二、SpringBoot 自动配置原理中的 thymeleaf 的自动配置规则

只要我们把 HTM L页面放在 classpath:/templates/,thymeleaf 就能自动渲染;

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
  private static final Charset DEFAULT_ENCODING = Charset.forName("UTF‐8");
  private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
  //前缀
  public static final String DEFAULT_PREFIX = "classpath:/templates/";
  //后缀
  public static final String DEFAULT_SUFFIX = ".html";

三、thymeleaf 语法

1、导入thymeleaf的名称空间(导入该名称空间后,就会有 thymeleaf 语法提示了)

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2、helloworld

java 代码
@Controller
public class HelloController {

    @RequestMapping("/success")
    public String success(Model model){
        model.addAttribute("msg", "你好");
        return "success";
    }
    
}

html 代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--th:text 将div里面的文本内容设置为 ${msg}-->
    <div th:text="${msg}"></div>

</body>
</html>
View Code

猜你喜欢

转载自www.cnblogs.com/fangwu/p/8931547.html