Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎,官方推荐使用Thymeleaf。

spring-boot-starter-thymeleaf

在Spring Boot中使用Thymeleaf只需在pom中加入Thymeleaf的starter即可:

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在Spring Boot 1.5.9.RELEASE版本中,默认的Thymeleaf版本为2.1.6.RELEASE版本,这里推荐使用3.0以上版本。在pom中将Thymeleaf的版本修改为3.0.2.RELEASE:

1
2
3
4
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version>
</properties>

在Spring Boot中,默认的html页面地址为src/main/resources/templates,默认的静态资源地址为src/main/resources/static。

Thymeleaf默认配置

在Spring Boot配置文件中可对Thymeleaf的默认配置进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#开启模板缓存(默认值:true)
spring.thymeleaf.cache=true
#Check that the template exists before rendering it.
spring.thymeleaf.check-template=true
#检查模板位置是否正确(默认值:true)
spring.thymeleaf.check-template-location=true
#Content-Type的值(默认值:text/html)
spring.thymeleaf.content-type=text/html
#开启MVC Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enabled=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.excluded-view-names=
#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)
spring.thymeleaf.mode=HTML5
#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix=.html
#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。
spring.thymeleaf.template-resolver-order=
#可解析的视图名称列表,用逗号分隔
spring.thymeleaf.view-names=

一般开发中将spring.thymeleaf.cache设置为false,其他保持默认值即可。

springboot 中使用thymeleaf的更多相关文章

  1. 7 — 简单了解springboot中的thymeleaf

    1.官网学习地址 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html 2.什么是thymeleaf? 一张图看明白: 解读: ...

  2. springboot 中的 thymeleaf 页面进阶小知识

    我们做好了登录页,那设计一个场景吧,比如我们登录后跳转到公司主页,想从公司主页再跳转到员工列表页: 这样的场景我们该如何实现,首先要知道一些基础知识,就是SpringBoot的一些关于请求的架构知识: ...

  3. SpringBoot中的thymeleaf引擎报错

    关于:thymeleaf报错: An error happened during template parsing (template: "class path resource [temp ...

  4. SpringBoot中使用Thymeleaf模板

    1.引入pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  5. SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用

    1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...

  6. thymeleaf教程-springboot项目中实现thymeleaf自定义标签

    转载: http://www.9191boke.com/466119140.html    91博客网 开始: 在使用thymeleaf的过程中有时候需要公共部分渲染页面,这个时候使用自定义标签实现自 ...

  7. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

  8. [读书笔记] 四、SpringBoot中使用JPA 进行快速CRUD操作

    通过Spring提供的JPA Hibernate实现,进行快速CRUD操作的一个栗子~. 视图用到了SpringBoot推荐的thymeleaf来解析,数据库使用的Mysql,代码详细我会贴在下面文章 ...

  9. 8.SpringBoot 模板引擎 Thymeleaf

    1.模板引擎原理 JSP.Velocity.Freemarker.Thymeleaf 都是模板引擎.SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: Thymeleaf模板引擎 ...

随机推荐

  1. flask第十八篇——模板【2】

    请关注公众号:自动化测试实战 上一节我们介绍了模板的基本使用方法,现在我们想一个问题,如果把index.html放到template文件夹下面的文件夹该怎么办呢?其实很容易,当文件夹结构如下图所示时: ...

  2. python---time 相关, str 转timestamp

    df['col'] = pd.to_datetime(df['col']) from datetime import date from datetime import datetime d = da ...

  3. sailsjs 不用写代码就能生成rest api 代码

    1. 脚手架安装 npm install sails -g     2. 生成基本项目 a. 项目 sails new appdemo b. 创建api sails new api demoapi a ...

  4. saas 系统租户个性化域名&&租户绑定自己域名的解决方案

       实际的需求就类似github 的自定义page 1. 个性化域名    github 实现原理就是用户个性化域名使用泛域名解析,这个比较简单,大部分域名提供商都可以解决    具体操作不用赘述 ...

  5. 7.Python使用pandans遇到的坑

    1.开始入门Pandas,然后跟着网上的例子,编写以下代码: import pandas as pd import datetime import pandas.io.data as web star ...

  6. 回文字符串的变形——poj1159

    回文字符串 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如"aba".当然, ...

  7. python中format函数学习笔记

    简而言之,format函数就是用{}来代替之前的输出字符时使用的% print('my name is %s and I am %d years old' % ('porsche',23)) 下面详细 ...

  8. Js、jquery学习笔记

    end() 方法 重新定位到上次操作的元素,一般与siblings()一起使用,操作其兄弟元素.如:$(this).addClass("highlight").children(& ...

  9. 给DB2增加删除字段二三事

    加字段用这个,CS.TG表名,LINE2_TYPE字段名,CHAR(1)字段类型 ALTER TABLE CS.TG ADD COLUMN LINE2_TYPE CHAR(1); 要是加错了用以下语句 ...

  10. Unit02: 参数值注入 、 基于注解的组件扫描

    Unit02: 参数值注入 . 基于注解的组件扫描 (4)IOC (Inversion Of Controll 控制反转) 什么是IOC? 对象之间的依赖关系由容器来建立. 什么是DI? (Depen ...