Thymeleaf模板引擎常用总结

本文详细介绍了Thymeleaf模板引擎在SpringBoot框架中的整合与使用方法,包括依赖配置、属性注入、静态资源引入、动态数据访问及迭代、JavaScript中model数据访问、HTML标签解析、数据格式化等核心内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一:语法简单总结

          Thymeleaf是一个Java类库,是xml/html/html5的模板引擎,SpringBoot框架推荐在MVC的Web应用做做View层使用.

          SpringBoot中整合Thymeleaf依赖如下.

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
	<properties>
		<!-- set thymeleaf version SpringBoot默认使用的是Thymeleaf的2.0的版本.-->
		<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
		<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

       

  

                                               ThymeleafProperties的源码如下.

将application.yml中的spring.thymeleaf的配置注入下面对应的属性中,有几个是默认的,剩下的都是根据使用情况配置的,然后注入到相应的值中.

@ConfigurationProperties:批量注入属性值.

@Value:单个注入属性值.

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
    private boolean cache;
    private Integer templateResolverOrder;
    private String[] viewNames;
    private String[] excludedViewNames;
    private boolean enableSpringElCompiler;
    private boolean enabled;
    private final ThymeleafProperties.Servlet servlet;
    private final ThymeleafProperties.Reactive reactive;

    public ThymeleafProperties() {
        this.encoding = DEFAULT_ENCODING;
        this.cache = true;
        this.enabled = true;
        this.servlet = new ThymeleafProperties.Servlet();
        this.reactive = new ThymeleafProperties.Reactive();
    }

   命名空间如下,静态页面转换为动态视图.

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

    1. Thymeleaf引入css资源.

          th:href="@{path}"

<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"  media="all">

    2. Thymeleaf引入JavaScript资源.

         th:src="@{path}"

<script th:src="@{js/jquery-3.1.1.min.js}"></script>

        通过 "@{}"引用静态资源.

   3. 访问Model中的数据.

        ${}访问model中的属性.

<span th:text="${singlePerson.name}"></span>

        动态处理的元素使用"th:"为前缀.

  4. model中的数据迭代.

     判断list集合不为空.

<div th:if="${not #lists.isEmpty(person)}">
</div>

    迭代list集合数据.list中保存实体对象.

<div class="panel-body">
                 <ul class="list-group">
                     <li class="list-group-item" th:each="person:${person}">
                         <span th:text="${person.name}"></span>
                         <span th:text="${person.age}"></span>
                         <button class="btn" th:onclick=" 'getPersonName(\'' +${person.name}+ '\'); '">获得名字</button>
                     </li>
                 </ul>
 </div>

如何获取遍历列表的序号,方式一:${stat.count}是从1开始的,方式  二:${stat.index}是从0开始的,如果从1开始就${stat.index+1}.

 <tr th:each="novel,stat:${resultlist}">
 <th style="text-align:center;" th:text="${stat.count}"></th>

  迭代list,里面存放map对象.

  th:text=${novel['Map Key']}

 <tr th:each="novel,stat:${resultlist}">
     <th style="text-align:center;" th:text="${stat.count}"></th>
     <th style="text-align:center;" th:text="${novel['Index']}"></th>
     <th style="text-align:center;" th:text="${novel['Type']}"></th>
     <th style="text-align:center;" th:text="${novel['Id']}"></th>
     <th style="text-align:center;" th:utext="${novel['Highlight']}"></th>
     <th style="text-align:center;" th:text="${novel['Scope']}"></th>
</tr>

5. 在JavaScript中访问model中的数据.

     通过th:inline=“javascript”添加到script标签,这样Javascript就可以访问model中的对象属性了.

    通过“[[${}]]”访问实际的值.

<script th:inline="javascript">
           var single=[[${singlePerson}]];
           console.log(single.name+"/"+single.age);
           function getPersonName(name){
               console.log(name);
           }
 </script>

6. 解析model属性值中的html标签.

<th style="text-align:center;" th:utext="${novel['Highlight']}"></th>

7. 格式化.(表达式对象)

    7.1格式化金额.小数位为2为.

<dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}">350</dd>

    7.2 格式化日期

<dd th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">28-Jun-2018</dd>

    7.3 字符串连接

<span th:text="${'Hi'+person.name}"></span>

 

<think>好的,我现在需要帮助用户寻找关于Thymeleaf模板引擎的组件库、使用教程和示例。根据用户提供的引用内容,首先我需要整理现有的信息,然后补充可能的组件库和教程资源。 首先,用户引用了四个关于Thymeleaf在Spring Boot中的基本用法,包括依赖配置、模板文件的位置和示例代码。这些内容已经涵盖了基础的集成步骤和简单的模板编写方法。用户现在想进一步了解组件库,也就是Thymeleaf的扩展库或者相关工具,可能用于增强模板功能,比如布局管理、表单处理等。 Thymeleaf本身有一些官方和社区支持的扩展,比如Thymeleaf Layout Dialect用于布局管理,Thymeleaf Extras Spring Security用于集成Spring Security,Thymeleaf Extras Java8Time处理日期时间。这些都是常见的组件库。需要检查这些组件是否有对应的教程或示例,特别是结合Spring Boot的情况。 接下来,关于使用教程和示例,用户提供的引用已经包含了基础的配置和模板示例,但可能需要更详细的步骤说明,比如如何添加布局方言,或者如何与Spring Security集成。此外,Thymeleaf的官方文档和社区资源也是重要的参考来源。 需要确保回答结构清晰,分步骤说明组件库的集成方法,并提供代码示例。同时,生成的相关问题要围绕用户可能感兴趣的扩展应用和常见问题展开。 现在开始组织回答内容: 1. **常用组件库**:列出Thymeleaf常用的扩展库,每个库的用途和Maven依赖配置。 2. **使用教程与示例**:分步骤说明如何集成这些组件库,并给出模板示例代码,比如布局方言的使用方法。 3. **参考引用**:确保引用用户提供的资料中的相关内容,如依赖配置和模板示例。 4. **相关问题**:生成与组件库和高级使用相关的问题,帮助用户深入探索。 需要验证每个组件库的依赖是否正确,特别是版本号是否最新。例如,Thymeleaf Layout Dialect的最新版本可能需要查看官方仓库。同时,示例代码需要符合Thymeleaf的语法,并确保在Spring Boot环境中有效。 可能的问题点:用户提到的组件库是否有对应的Spring Boot Starter,或者是否需要手动配置。例如,Thymeleaf Extras Spring Security可能需要额外的配置步骤,而Layout Dialect可能只需添加依赖即可。 总结回答的结构: - Thymeleaf常用组件库介绍 - 布局管理:Layout Dialect - Spring Security集成 - Java 8时间支持 - 其他扩展 - 使用教程与示例(以布局方言为例) - 添加依赖 - 创建布局模板 - 子页面继承布局 - 示例代码 - 相关资源推荐 - 生成的问题列表 确保每个部分都引用用户提供的引用内容,比如依赖配置部分引用[^3],模板示例引用[^2][^4]。同时,补充新的组件库信息时,要确保正确性和实用性。</think>### Thymeleaf模板引擎组件库使用指南 #### 一、常用组件库介绍 1. **Thymeleaf Layout Dialect** 用于实现页面布局复用,支持创建公共模板和动态内容替换。 Maven依赖配置: ```xml <dependency> <groupId>nz.net.ultraq.thymeleaf</groupId> <artifactId>thymeleaf-layout-dialect</artifactId> <version>3.1.0</version> </dependency> ``` 2. **Thymeleaf Extras Spring Security** 集成Spring Security,支持在模板中根据权限动态显示内容。 Maven依赖: ```xml <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity6</artifactId> <version>3.1.2.RELEASE</version> </dependency> ``` 3. **Thymeleaf Java8Time 扩展** 提供对Java 8时间API的支持,简化日期格式化操作。 自动集成于Thymeleaf 3.0+,无需额外依赖[^3]。 --- #### 二、使用教程与示例(以Layout Dialect为例) **步骤1:添加依赖** 在Spring Boot项目的`pom.xml`中添加Layout Dialect依赖(见上文)。 **步骤2:创建布局模板** 在`resources/templates/layout`目录下创建`base.html`: ```html <!DOCTYPE html> <html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> <head> <title layout:title-pattern="$DECORATOR_TITLE - $CONTENT_TITLE">默认标题</title> </head> <body> <header>公共头部</header> <div layout:fragment="content"></div> <!-- 内容占位符 --> <footer>公共底部</footer> </body> </html> ``` **步骤3:子页面继承布局** 创建`hello.html`,继承并填充内容: ```html <!DOCTYPE html> <html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{layout/base}"> <head> <title>子页面标题</title> </head> <body> <div layout:fragment="content"> <h1 th:text="${name}">Hello Thymeleaf</h1> <!-- 动态内容填充 --> </div> </body> </html> ``` --- #### 三、相关资源推荐 - **官方文档**:[Thymeleaf Layout Dialect](https://github.com/ultraq/thymeleaf-layout-dialect) - **Spring Boot集成示例**:参考引用中的模板配置方法 - **社区教程**:Spring Boot官网推荐的Thymeleaf实践指南 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大道之简

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值