Java基础总结之Thymeleaf详解

Posted 虎子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java基础总结之Thymeleaf详解相关的知识,希望对你有一定的参考价值。

一、Thymeleaf语法

标签

html页面上使用Thymeleaf标签,Thymeleaf 标签能够动态地替换掉静态内容,使页面动态展示。为了大家更直观的认识Thymeleaf,下面展示一个在HTML文件中嵌入了Thymeleaf的页面文件,示例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

<!DOCTYPE html>

<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>

    <meta charset="UTF-8">

        <link rel="stylesheet" type="text/css" media="all"

        href="../../css/gtvg.css" rel="external nofollow"  th:href="@/css/gtvg.css" rel="external nofollow"  />

    <title>Title</title>

</head>

<body>

    <p th:text="$hello">欢迎进入Thymeleaf的学习</p>

</body>

</html>

thymelef常用标签

标签说明
th:insert布局标签,替换内容到引入的文件
th:replace页面片段包含(类似JSP中的include标签)
th:each元素遍历(类似JSP中的c:forEach标签)
th:if条件判断,如果为真
th:unless条件判断,如果为假
th:switch条件判断,进行选择性匹配
th:case条件判断,进行选择性匹配
th:value属性值修改,指定标签属性值
th:href用于设定链接地址
th:src用于设定链接地址
th:text用于指定标签显示的文本内容

标准表达式

说明表达式语法
变量表达式$…
选择变量表达式*…
消息表达式#…
链接URL表达式@…
片段表达式~…

1.1 变量表达式$…

主要用于获取上下文中的变量值,示例代码如下:

1

<p th:text="$title">这是标题</p>

Thymeleaf为变量所在域提供了一些内置对象,具体如下所示

1

2

3

4

5

6

7

# ctx:上下文对象

# vars:上下文变量

# locale:上下文区域设置

# request:(仅限Web Context)HttpServletRequest对象

# response:(仅限Web Context)HttpServletResponse对象

# session:(仅限Web Context)HttpSession对象

# servletContext:(仅限Web Context)ServletContext对象

假设要在Thymeleaf模板引擎页面中动态获取当前国家信息,可以使用
#locale内置对象,示例代码如下

1

The locale country is: <span th:text="$#locale.country">US</span>

1.2 选择变量表达式*…

和变量表达式用法类似,一般用于从被选定对象而不是上下文中获取属性值,如果没有选定对象,则和变量表达式一样,示例代码如下

1

2

3

<div th:object="$book">

<p>titile: <span th:text="*title">标题</span>.</p>

</div>

*title 选择变量表达式获取当前指定对象book的title属性值。

1.3 消息表达式 #…

消息表达式#…主要用于Thymeleaf模板页面国际化内容的动态替换和展示,使用消息表达式#…进行国际化设置时,还需要提供一些国际化配置文件。

.4 链接表达式 @…

链接表达式@…一般用于页面跳转或者资源的引入,在Web开发中占据着非常重要的地位,并且使用也非常频繁

1

2

<a th:href="@http://localhost:8080/order/details(orderId=$o.id)" rel="external nofollow" >view</a>

<a th:href="@/order/details(orderId=$o.id,pid=$p.id)" rel="external nofollow" >view</a>

链接表达式@…分别编写了绝对链接地址和相对链接地址。

在有参表达式中,需要按照@路径(参数名称=参数值,参数名称=参数值…)的形式编写,同时该参数的值可以使用变量表达式来传递动态参数值

1.5 片段表达式 ~…

片段表达式~…用来标记一个片段模板,并根据需要移动或传递给其他模板。其中,最常见的用法是使用th:insert或th:replace属性插入片段

1

<div th:insert="~thymeleafDemo::title"></div>

thymeleafDemo为模板名称,Thymeleaf会自动查找“/resources/templates/”目录下的thymeleafDemo模板,title为片段名称

二、基本使用

2.1 Thymeleaf模板基本配置

首先 在Spring Boot项目中使用Thymeleaf模板,首先必须保证引入Thymeleaf依赖

1

2

3

4

<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

其次,在全局配置文件中配置Thymeleaf模板的一些参数。一般Web项目都会使用下列配置

1

2

3

4

5

spring.thymeleaf.cache = true #启用模板缓存

spring.thymeleaf.encoding = UTF_8 #模板编码

spring.thymeleaf.mode = HTML5 #应用于模板的模板模式

spring.thymeleaf.prefix = classpath:/templates/ #指定模板页面存放路径

spring.thymeleaf.suffix = .html #指定模板页面名称的后缀

上述配置中:

spring.thymeleaf.cache表示是否开启Thymeleaf模板缓存,默认为true,在开发过程中通常会关闭缓存,保证项目调试过程中数据能够及时响应;

spring.thymeleaf.prefix指定了Thymeleaf模板页面的存放路径,默认为classpath:/templates/;

spring.thymeleaf.suffix指定了Thymeleaf模板页面的名称后缀,默认为.html

到此这篇关于Java基础总结之Thymeleaf模板的文章就介绍到这了,更多相关Java Thymeleaf模板内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

 

以上是关于Java基础总结之Thymeleaf详解的主要内容,如果未能解决你的问题,请参考以下文章

java基础之变量详解

不惑JAVA之JAVA基础 - NIO

关于Themleaf学习总结

《C#零基础入门之百识百例》(八十三)ArrayList数组列表详解 -- 代码示例

《C#零基础入门之百识百例》(八十三)系统类ArrayList数组列表详解 -- 代码示例

Java最强最新知识体系总结(2021版)