1、添加起步依赖:

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

2、添加配置(application.properties):

#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

参考ThymeleafProperties.java类,cache默认为true,其它属性都有默认值,一般不需要修改。

3、写测试controller返回页面并携带数据:

package cn.mmweikt.es.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; @Controller
public class IndexController {
@GetMapping("/index")
public String indexPage(Model model) {
model.addAttribute("name", "es_project.");
return "index";
}
}

4、在resources/templates下放.html页面,在resources/static下放静态文件。templates和static下都可以再放文件夹:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${name}"></span>
</body>
</html>

注意,在html标签里一定要引入 xmlns:th="http://www.thymeleaf.org" ,这样thymeleaf模板才会启用,才能使用th:*这样的语法。

语法可参考:

https://www.cnblogs.com/jin-zhe/p/8202885.html

https://www.cnblogs.com/nfcm/p/7843935.html

补充:

网上看到说,在spring-boot1.4之后,支持thymeleaf3,可以更改版本号来进行修改支持。3相比2极大的提高了效率,并且不需要标签闭合,类似的link,img等都有了很好的支持,按照如下配置即可:

 <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- set thymeleaf version -->
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
<!--set java version-->
<java.version>1.8</java.version>
</properties>

SpringBoot集成Thymeleaf模板的更多相关文章

  1. 九、SpringBoot集成Thymeleaf模板引擎

    Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:“赛母李府”,大部分中国人是听不出破绽的.. ...

  2. SpringBoot集成Thymeleaf模板引擎

    简单介绍 目前在JavaEE领域有几中比较常用的模板引擎,分别是Jsp.Velocity.Freemarker.Thymeleaf,对Freemark语法不是特别熟悉,不过对于前端页面渲染效率来说,j ...

  3. SpringBoot 之Thymeleaf模板.

    一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...

  4. Springboot 集成 Thymeleaf 及常见错误

    Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...

  5. springboot整合Thymeleaf模板引擎

    引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</ ...

  6. Springboot整合thymeleaf模板

    Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...

  7. SpringBoot集成beetl模板快速入门

    SpringBoot集成beetl模板快速入门 首次探索 beetl官方网址:http://ibeetl.com/ 创建SpringBoot工程(idea) 新建工程 选择创建Spring工程 书写包 ...

  8. SpringBoot集成thymeleaf(自定义)模板中文乱码的解决办法

    楼主今天在学习SpringBoot集成thymelaf的时候报了中文乱码的错误,经过网上的搜索,现在得到解决的办法,分享给大家: package com.imooc.config; import or ...

  9. springboot用thymeleaf模板的paginate分页

    本文根据一个简单的user表为例,展示 springboot集成mybatis,再到前端分页完整代码(新手自学,不足之处欢迎纠正): 先看java部分 pom.xml 加入 <!--支持 Web ...

随机推荐

  1. 基于虚拟用户登录的ftp服务配置

    文章结构:             一.使用逻辑卷配置ftp数据存放目录             二.安装和配置vsftpd服务             三.使用不通权限的用户访问ftp服务器 系统环 ...

  2. redis缓存架构-02-两种持久化机制(RDB和AOF)

    1.两种持久化机制的介绍 1.1 RDB 周期性的生成redis内存数据的一份完整的快照 1)根据配置的检查点,生产rdb快照文件,fork一个子线程,将数据dump到rdb快照文件中,完成rdb文件 ...

  3. go module管理依赖包

    go mod 最大的好处就是摆脱了GOPATH这个限制,在除了GOPATH以外的目录下也能开展你的项目 go mod使用: 1,确保你的go版本是1.1以上 2,创建一个项目目录example,并添加 ...

  4. C#隐式类型和显示类型

    一,在程序中我们经常会遇到:无法将类型“XXX”隐式装换为“XXX”,如下例子: static void Main(string[] args) { int i; i = "Hello Wo ...

  5. bootstrap-thymeleaf-分页

    1.HTML代码 <div th:fragment="paginater"> <ul th:id="paginaterUlID" th:if= ...

  6. Primeng UI框架ionic3 中下拉选择插件p-dropdown 插件的使用方法

    1.html引入: <p-dropdown float-right [options]="sortOption" [(ngModel)]="sortNow" ...

  7. ajax基本原理实现

    function ajax(method,url,data,success){ try{ var xhr=new XMLHttpRequest(); }catch(e){ xhr=new Active ...

  8. Linux: 给右键菜单加一个“转换图片为jpg格式”

    Linux上通常都会安装imagemagick这个小巧但又异常强大的工具.这个软件提供了一系列很好用的功能.这里说一说如何使用它的convert命令转换图片为jpg格式,以及如何把它添加到Thunar ...

  9. 74th Jupyter Notebook 插入图片的方法

    转载: https://account.cnblogs.com/signin?ReturnUrl=http%3A%2F%2Fhome.cnblogs.com%2Fu%2Ferdou%2F   插入本地 ...

  10. 【leetcode】207. Course Schedule

    题目如下: There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have ...