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. KeyDown/PreviewKeyDown事件中监听Alt键按下

    一个坑 在WPF应用程序(或者其他Windows应用程序中),为了监听Alt键按下,我们可以尝试写出这样的代码: PreviewKeyDown += (s, e) => { if (e.Key ...

  2. pip 在windows下的更新升级

    1. pip 在 PyCharm 无法自动更新 2.   https://pip.pypa.io/en/latest/installing.html 官方网页要求在 cmd中输入以下命令进行 pip的 ...

  3. "http://127.0.0.1:4723/wd/hub"的解释

    先补充一个内容,就是appium安装时候的环境变量配置,必须要配ANDROID_HOME这个变量,不是“要配置”,是“必须配置”,其他的那些放到系统变量的path里就可以了: # coding: ut ...

  4. elixir 集成ejabberd

    备注: 我开发测试的环境时centos 1. 预备环境 1. openssl yum install -y  openssl-devel 2. xml yum install -y expat-dev ...

  5. bzoj 1670 [Usaco2006 Oct]Building the Moat护城河的挖掘——凸包

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1670 用叉积判断.注意两端的平行于 y 轴的. #include<cstdio> ...

  6. java少包汇总

    1.在下载使用javax.mail的jar包时候,注意: 有的jar没有包含sun的实现,只包含了api,这类jar名称通常为javax.mail-api-x.x.x.jar,在使用smtp协议发邮件 ...

  7. java.lang.NoSuchFieldError: TRACE

    Exception in thread "main" java.lang.NoSuchFieldError: TRACE    at org.jboss.logging.Log4j ...

  8. mysql存储引擎之myisam学习

    myisam存储引擎特点:1.不支持事务2.表级锁定(更新时锁整个表,其索引机制是表级索引,这虽然可以让锁定的实现成本很小,但是也同时大大降低 了其并发性能) 3.读写互相阻塞:不仅会在写入的时候阻塞 ...

  9. 简单服务端缓存API设计

    Want 我们希望设计一套缓存API,适应不同的缓存产品,并且基于Spring框架完美集成应用开发. 本文旨在针对缓存产品定义一个轻量级的客户端访问框架,目标支持多种缓存产品,面向接口编程,目前支持简 ...

  10. MQTT 协议 Client ID 长度不能超过23个字符

    今天遇到一个MQTT的问题,MqttException: MQIsdp ClientId > 23 bytes ClientId的长度大于23时,无法链接MQTT服务器. 经过查看协议发现:客户 ...