引言

Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。也是Spring Boot官方推荐的模板引擎

引用 依赖

 <parent>
        <groupId>org.spring work.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.1.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-themleaf</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.spring work.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.spring work.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

在Spring Boot 2.0 以上版本,一定要引用 thymeleaf-layout-dialect !!!,不然你得layout就无法使用

application.yml

spring:
  thymeleaf:
    cache: false
    mode: LEGACYHTML5
    check-template: true

默认配置下,thymeleaf对.html的内容要求很严格,比如 < charset=“UTF-8” />,如果少最后的标签封闭符号/,就会报错而转到错误页。

Controller

package top.yuyufeng.demo.action;

import org.spring work.stereotype.Controller;
import org.spring work.ui.Model;
import org.spring work.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author yuyufeng
 * @date 2018/12/21.
 */
@Controller
public class IndexController {

    @RequestMapping(value = \"index\")
    private String index(Model model) {
        model.addAttribute(\"myDate\", new Date());
        return \"index\";
    }

    @RequestMapping(value = \"hello\")
    private String hello(Model model) {
        model.addAttribute(\"userName\",\"用户1\" );
        return \"hello\";
    }

}

Application

package top.yuyufeng.demo;

import org.spring work.boot.SpringApplication;
import org.spring work.boot.autoconfigure.SpringBootApplication;

/**
 * @author yuyufeng
 * @date 2018/12/21.
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

layout页面

<!DOCTYPE html>
<html lang=\"zh\"  ns:th=\"http://www.thymeleaf.org\"
       ns:layout=\"http://www.ultraq.net.nz/web/thymeleaf/layout\">
<head>
    <  charset=\"UTF-8\">
    < >layout</ >
</head>
<body>
<h1>--------------------这是layout内容------------------------</h1>
<div layout:fragment=\"content\"></div>
<h1>---------------------这是layout内容-----------------------</h1>
</body>
</html>

首页

<!DOCTYPE html>
<html lang=\"en\"  ns:th=\"http://www.thymeleaf.org\"  ns:layout=\"http://www.ultraq.net.nz/web/thymeleaf/layout\"
      layout:decorator=\"include/layout\">
<head>
    <  charset=\"UTF-8\">
    < >首页</ >
</head>
<body>
<div layout:fragment=\"content\">
    <h1>这是首页内容</h1>
    <p>时间:<b th:text=\"${myDate}\"></b></p>
    <p><b  th:text=\"${\'时间:\'+#dates.format(myDate, \'yyyy年MM月dd日 HH:mm:ss\')}\"></b></p>
</div>
</body>
</html>

文件目录结构与页面效果展示

\"在这里插入图片描述\"

\"在这里插入图片描述\"

themleaf 的常用标签参考

待整理
https://www.thymeleaf.org/

收藏 打印