springboot thymeleaf的使用

本文介绍了如何在SpringBoot项目中引入并使用Thymeleaf模板引擎,包括添加依赖、理解Thymeleaf的语法特性,如标准表达式、属性优先级,并通过具体案例展示了Thymeleaf在实际页面渲染中的应用。
摘要由CSDN通过智能技术生成

thymeleaf 是 springboot 中流行使用的模板引擎,语法简单,功能强大。
thymeleaf 官方帮助文档(英文版)thymeleaf.pdf 提取密码: zev2

引入 thymeleaf

pom 文件引入 thymeleaf 依赖

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

如果想要更改默认的版本,可加入如下代码更改 thymeleaf 版本
注意:thymeleaf-layout-dialect 的 2.0 及以上版本才开始支持 thymeleaf 的 3.0 以上版本

  <properties>
  	<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
  	<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
  </properties>

thymeleaf 的语法

引入名称空间

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

如果不引入名称空间,可以使用 data-th-属性 使用 thymeleaf 功能,如下

<p data-th-text="#{home.welcome}">Welcome to our grocery store!</p>
标准表达式语法
Simple expressions: 简单表达式
	Variable Expressions: ${...}    变量表达式,获取变量的值,OGNL实现
		1) 获取对象的属性、调用方法
		例如:
			${person.father.name}
			${personsByName['Stephen Zucchini'].age}
			${person.createCompleteName()}
			
		2) 使用内置的基本对象
			#ctx : the context object.
			#vars: the context variables.
			#locale : the context locale.
			#request : (only in Web Contexts) the HttpServletRequest object.
			#response : (only in Web Contexts) the HttpServletResponse object.
			#s
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值