springboot整合thymeleaf

简介: springboot整合thymeleaf

springboot整合thymeleaf

1. 导入起步依赖

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

2. 更改引入版本

<properties>
    <springboot-thymeleaf.version>3.0.2.RELEASE</springboot-thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。

使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。

锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。

3. 配置文件配置

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false


spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false


语法

1. 替换标签体内容

<!--渲染效果-->
<div  th:text="${Hello}">显示欢迎</div>
<!--不渲染效果-->
<div  th:utext="${Hello}">显示欢迎</div>

2. 替换属性

<div id="div01" th:id="${Hello}" th:class="${Hello}" th:text="${Hello}">显示欢迎</div>
<div style="background-color: yellow" th:style="'background-color:green'"></div>

3. 在表达式中访问属性域

<h3>访问属性域</h3>
<p th:text="${attrRequestScope}">访问请求域 方式一</p>  <br>
<p th:text="${#httpServletRequest.getAttribute('attrRequestScope')}">访问请求域 方式二</p><br/>
<h3>访问Session域</h3>
<p th:text="${session.atttrSessionScope}">访Session域 方式一</p><br/>
<p th:text="${application.attrAppSocpe}">访问Application域 方式一</p>

4. 解析url地址

<h3>解析URL地址,获取ContextPath的值</h3>
<p th:text="@{/success}">@{}是把ContextPath的值附加到指定的地址前</p>
<a th:href="@{/test}">@{}是把ContextPath的值附加到指定的地址前</a>

5. 直接执行表达式

<h3>直接执行表达式</h3>
<p>无转义效果 : [[${attrRequestScope}]] </p>
<p>有转义效果 : [(${attrRequestScope})] </p>

6. 分支与迭代

1. if 判断

<h3>if判断字符串是否为空</h3>
<p th:if="${not #strings.isEmpty(attrRequestScope)}">attrRequestScope不为空显示</p>
~~~<p th:if="${#strings.isEmpty(attrRequestScope)}">attrRequestScope为空显示</p>~~~~

2. 测试循环

<h3>测试循环</h3>
<div>
    <p th:text="${str}" th:each="str :${list}"></p>
</div>

使用th:each进行集合数据迭代

th:each=“声明变量:${集合}”

th:each 用在哪个标签上,哪个标签就会出现多次

7. 引入外部代码

要被引入的代码 include/part.html

<html xmlns:th="http://www.thymeleaf.org">
<div th:fragment="myFirstPart">
<p>
    被包含的内容1111
</p>
</div>
<div th:fragment="mySecondPart">
    <p>
        被包含的内容2222
    </p>
</div>
<div th:fragment="myThirdPart">
    <p>
        被包含的内容3333
    </p>
</div>

需要引入的页面

<div th:insert="~{include/part :: myFirstPart }"></div>
<div th:replace="~{include/part :: mySecondPart }" style="background-color: yellow"></div>
<div th:include="~{include/part ::myThirdPart }" style="background-color: red"></div>

渲染后的页面源码

<div>
    <div>
        <p>
            被包含的内容1111
        </p>
    </div>
</div>
<div>
    <p>
        被包含的内容2222
    </p>
</div>
<div style="background-color: red">
    <p>
        被包含的内容3333
    </p>
</div>

" :: "左边的值拼前后缀后必须能够找到要包含的文件。

" :: " 右边的值是代码片段的名字 ,就是th:fragment的值。

insert将代码原样引入。

replace使用被引入的代码和属性替换原有的。

include使用被引入的代码div内部的代码。


相关文章
|
数据采集 SQL 分布式计算
数据处理 、大数据、数据抽取 ETL 工具 DataX 、Kettle、Sqoop
数据处理 、大数据、数据抽取 ETL 工具 DataX 、Kettle、Sqoop
4169 0
|
前端开发 Java 应用服务中间件
ruoyi-vue前后端分离项目实现一体化打包(前后端合并打包)
ruoyi-vue前后端分离项目实现一体化打包(前后端合并打包)
3488 0
|
存储 SQL 运维
Apache Doris 在小米统一 OLAP 和湖仓一体的实践
小米早在 2019 年便引入 Apache Doris 作为 OLAP 分析型数据库之一,经过五年的技术沉淀,已形成以 Doris 为核心的分析体系,并基于 2.1 版本异步物化视图、3.0 版本湖仓一体与存算分离等核心能力优化数据架构。本文将详细介绍小米数据中台基于 Apache Doris 3.0 的查询链路优化、性能提升、资源管理、自动化运维、可观测等一系列应用实践。
566 3
Apache Doris 在小米统一 OLAP 和湖仓一体的实践
|
12月前
|
Java 测试技术 数据库
Spring Boot中的项目属性配置
本文介绍Spring Boot中配置管理的常用方法:通过`@Value`读取单个配置,使用`@ConfigurationProperties`封装多个配置项,并实现开发与生产环境配置文件(如application-dev.yml和application-pro.yml)的灵活切换,提升项目可维护性。
|
12月前
|
缓存 JSON 前端开发
Spring Boot集成Thymeleaf模板引擎
Thymeleaf 是现代Java服务端模板引擎,支持HTML静态原型与动态数据融合,前后端可协作开发。通过 `th:*` 标签动态渲染数据,浏览器可直接预览页面,无需启动服务。Spring Boot 中集成简单,只需引入依赖并配置关闭缓存即可。支持对象、List 数据展示,常用标签如 th:value、th:each、th:if 等,便于构建动态页面。适用于错误页、表单等场景,提升开发效率。
|
弹性计算 测试技术
GitHub宝藏工具:融合怪测评脚本,云服务器实测演示
本文介绍了一款GitHub上的“融合怪”服务器测评脚本,集CPU、内存、磁盘IO、网络线路及流媒体解锁检测于一体,操作简单,适合新手快速掌握服务器真实性能,助你充分发挥服务器价值。
1217 5
|
缓存 JSON 前端开发
第07课:Spring Boot集成Thymeleaf模板引擎
第07课:Spring Boot集成Thymeleaf模板引擎
1193 0
第07课:Spring Boot集成Thymeleaf模板引擎
|
存储 监控 测试技术
三大工作流引擎技术Activiti、Camunda、Flowable选型指南
三大工作流引擎技术Activiti、Camunda、Flowable选型指南
|
druid 前端开发 Java
|
存储 大数据 数据安全/隐私保护
【专栏】带你了解 S3 背后的故事,为我们的数字世界提供坚实的存储基础
【4月更文挑战第28天】S3,亚马逊的简单存储服务协议,是云存储的事实标准,以其易用、高效和可靠的对象存储闻名。核心概念包括桶(存储单元)、对象(独立数据实体)和区域(地理分布)。S3 提供弹性存储、高可用性、数据安全和成本效益,广泛应用于备份、大数据、网站存储等。其成功推动了云存储市场发展,促进了数据存储创新,成为云存储领域不可或缺的部分。
4275 0