您的位置:首页技术文章
文章详情页

Java基础总结之Thymeleaf详解

【字号: 日期:2022-08-12 16:46:23浏览:4作者:猪猪
目录一、Thymeleaf语法1.1 变量表达式${…}1.2 选择变量表达式*{…}1.3 消息表达式 #{…}1.4 链接表达式 @{…}1.5 片段表达式 ~{…}二、基本使用2.1 Thymeleaf模板基本配置一、Thymeleaf语法

标签

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

<!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='https://www.haobala.com/css/gtvg.css' rel='external nofollow' th:href='https://www.haobala.com/bcjs/@{/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 变量表达式${…}

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

<p th:text='${title}'>这是标题</p>

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

# ctx:上下文对象# vars:上下文变量# locale:上下文区域设置# request:(仅限Web Context)HttpServletRequest对象# response:(仅限Web Context)HttpServletResponse对象# session:(仅限Web Context)HttpSession对象# servletContext:(仅限Web Context)ServletContext对象

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

The locale country is: <span th:text='${#locale.country}'>US</span>1.2 选择变量表达式*{…}

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

<div th:object='${book}'><p>titile: <span th:text='*{title}'>标题</span>.</p></div>

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

1.3 消息表达式 #{…}

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

1.4 链接表达式 @{…}

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

<a th:href='https://www.haobala.com/bcjs/@{http://localhost:8080/order/details(orderId=${o.id})}' rel='external nofollow' >view</a><a th:href='https://www.haobala.com/bcjs/@{/order/details(orderId=${o.id},pid=${p.id})}' rel='external nofollow' >view</a>

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

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

1.5 片段表达式 ~{…}

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

<div th:insert='~{thymeleafDemo::title}'></div>

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

二、基本使用2.1 Thymeleaf模板基本配置

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

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

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

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
相关文章: