spring boot 之Thymleaf模板引擎

ssm中使用jsp作为前端页面
springboot不支持jsp但是支持模板引擎+html

缓存

在application.properties配置文件中关闭模板的缓存 避免开发的过程中有了变更但是刷新不出来 等真正上线的时候再打开

# ThymeleafProperties
spring.thymeleaf.cache=false

配置

在这里插入图片描述
将模板和数据结合一起 替换变量显示出来页面
springboot2.x只需要导入依赖

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

使用

  • 控制层直接拼接 return转到classpath:/templates/路径下的 html前端页面
    在这里插入图片描述
  • html页面当中的使用:
  1. 需要导入语法提示
<html lang="en" xmlns:th="http://www.thymeleaf.org">

在这里插入图片描述
2. 用法

属性

在这里插入图片描述

5种表达式

在这里插入图片描述

  • $()
    比如获取model and view中的参数
    在这里插入图片描述
  • #()
<b th:text="${#dates.format(map.post.createTime,'yyyy-MM-dd HH:mm:ss')}">2019-04-15 15:32:18</b>

模板内置对象 通过#来调用 接着用${#内置对象.属性}来使用属性
在这里插入图片描述

  • *{}
    和${}类似 但是有优化的一点
    在这里插入图片描述
    优秀于
    在这里插入图片描述
  • @{}定义url链接
    在这里插入图片描述
  • ~{} 引用
    在这里插入图片描述
    在这里插入图片描述

一个分页的模板实例

里面包含了链接 获取参数 使用封装工具还有判断等

<!-- 分页 -->
				<nav class="mt-5" th:if="${page.rows>0}">
					<ul class="pagination justify-content-center">
						<li class="page-item">
							<a class="page-link" th:href="@{${page.path}(current=1)}">首页</a>
						</li>
						<li th:class="|page-item ${page.current==1?'disabled':''}|">
							<a class="page-link" th:href="@{${page.path}(current=${page.current-1})}">上一页</a></li>
						<li th:class="|page-item ${i==page.current?'active':''}|" th:each="i:${#numbers.sequence(page.from,page.to)}">
							<a class="page-link" href="#" th:text="${i}">1</a>
						</li>
						<li th:class="|page-item ${page.current==page.total?'disabled':''}|">
							<a class="page-link" th:href="@{${page.path}(current=${page.current+1})}">下一页</a>
						</li>
						<li class="page-item">
							<a class="page-link" th:href="@{${page.path}(current=${page.total})}">末页</a>
						</li>
					</ul>
				</nav>
  • 可以使用${参数}(参数后面要拼接的)来表示正常的拼接 这是 ${}的一个功能
    在这里插入图片描述
    使用工具类
    在这里插入图片描述
    th 和class 放在一起
    在这里插入图片描述
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值