Spring Boot整合 Thymeleaf 模板引擎
什么是Thymeleaf
Thymeleaf是一款用于渲染XML、XHTML、HTML5内容的模板引擎。类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。
Thymeleaf也是Spring Boot首要支持的模板引擎,并且在最新的Spring Boot版本中已经不再支持Velocity了。
引入依赖
需要引入Spring Boot的Thymeleaf启动器依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
引入该依赖后会自动引入web依赖,不需要再单独引入web依赖。
自动配置说明
下面是Thymeleaf的自动配置相关类。
Thymeleaf的自动配置类:
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration
Thymeleaf的自动配置参数类:
org.springframework.boot.autoconfigure.thymeleaf.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";
默认的编码是:UTF-8
默认的类型是:text/html
默认的模板文件目录是:classpath:/templates/
默认的模板文件后缀是:.html
这些参数都可以通过在application配置文件中指定spring.thymeleaf.xx进行更改,更多可参考该参数类。
实战
知道了自动配置的原理,所以我们可以知道怎么做了。
一、在resources目录下创建templates目录。
二、在templates目录下创建.html模板文件。
三、使用模板:
1、模板文件头部使用<html xmlns:th="http://www.thymeleaf.org">定义。
2、html标签上使用th:开头标识作为前缀。
3、通过@{}引入web静态文件。
<link rel="stylesheet" th:href="@{/css/jquery.min.css}"/>
4、访问数据
访问springmvc中的model数据:${user.name},访问更多不同对象的数据请点击参考官方定义。
Spring Boot整合 Thymeleaf 模板引擎的更多相关文章
- Spring Boot整合Thymeleaf模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结
一.Thymeleaf概述 一般来说,常用的模板引擎有JSP.Velocity.Freemarker.Thymeleaf . SpringBoot推荐的 Thymeleaf – 语法更简单,功能更强大 ...
- Spring Boot☞ 使用Thymeleaf模板引擎渲染web视图
静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /s ...
- spring boot 整合Thymeleaf模板
SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图
在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: ...
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- 从零开始的Spring Boot(5、Spring Boot整合Thymeleaf)
Spring Boot整合Thymeleaf 写在前面 从零开始的Spring Boot(4.Spring Boot整合JSP和Freemarker) https://www.cnblogs.com/ ...
- Spring Boot整合Thymeleaf视图层
目录 Spring Boot整合Thymeleaf Spring Boot整合Thymeleaf 的项目步骤 Thymeleaf 语法详解 Spring Boot整合Thymeleaf Spring ...
随机推荐
- App后台Keynote
[App后台Keynote] 一.基础. 1.一个 节省 流量 的 处理 方法 是 让 App 下载 经过 压缩 的 图片( 一般 是 几十 KB 以下), 当 用户 需要 查看 原图 时 才 下载 ...
- nginx命令启动及选项
[root@ke]# nginx -h #this help [root@ke]# nginx -t #检查配置文件的语法 [root@ke]# nginx -T #检查配置文件的语法并输出 [ ...
- python中获取python版本号的方法
import platform print platform.python_version()
- easyui 回车搜索
代码: js: // 搜索 $("#searchButton").click(function() { var ip = $("#searchIp").val( ...
- [leetcode]76. Minimum Window Substring最小字符串窗口
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 时间戳转中国人能看得懂的日期格式 yy-mm-dd
很多项目都会用到时间戳的转换 说实话 我现在的这家公司超级好 因为后太要求传数据的时候竟然可以是时间戳的格式 我觉得我好幸福 哈哈哈 不过 等后台转给你数据的时候很多时候都是时间戳 这时候就得前端转 ...
- DevExpress数据源 BindingList<FilesList> 与BindSource 使用区别
这两个都能实现对接girdcontrol.datasource . 我只是说我自己用的时候的差异 bingsource bs 添加了对象 之后成为一个对象集合 你是无法用foreach (var ...
- 选择困难症的福音——团队Scrum冲刺阶段-Day 7
选择困难症的福音--团队Scrum冲刺阶段-Day 7 今日进展 测试代码 将界面设计完后放app使用示意图于此 今日贡献量 严域俊 吴恒佚 曾程 刘辰 邓煜坤 3.5 3.5 3.3 3.6 3 贡 ...
- linux学习第八天 (Linux就该这么学)
今天学了,mount 挂载,umount撤销挂载,.fdisk 命令 管理硬盘 交换分区swap,硬盘配额 xfs_quota命令 今天工作,手机看了,看的不全,回头看录播了.
- Oracle使用JDBC进行增删改查 表是否存在
Oracle使用JDBC进行增删改查 数据库和表 table USERS ( USERNAME VARCHAR2(20) not null, PASSWORD VARCHAR2(20) ) a ...