SpringBoot学习记录(四):SpringBoot整合Thymeleaf

本文详细介绍了Thymeleaf模板引擎的功能和使用方法,包括如何替代JSP实现前后端分离,支持静态和动态页面展示,以及如何通过在HTML标签中添加额外属性来结合模板和数据。文中还提供了具体的用例测试,包括引入依赖、配置文件、Controller类和页面代码,展示了如何在Spring Boot框架下使用Thymeleaf。

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

Thymeleaf简介

Thymeleaf是什么? 简单的说它是一个类似于Freemarker和Velocity的模板,它可以完全的替代JSP。
Thymeleaf在有网络和无网络的环境下皆可运行,它可以让前端工程师在浏览器查看页面的静态效果,也可以让后端工程师在服务器查看带数据的动态页面效果,这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

用例测试

1、引入依赖

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

2、配置文件

#设置后缀以及页面存放位置
spring:
  thymeleaf:
    prefix: classpath:/web/			
    suffix: .html

3、Controller类

@Controller
@RequestMapping("/index/")
public class StudentController {

	@RequestMapping("test")
	public String test(Model model) {
		
		//传入字符串
		model.addAttribute("test","Hellow Spring boot Freemarker!!");
		
		//传入对象
		Student stu = new Student(101, "张三");
		model.addAttribute("stu",stu);
		
		//传入集合
		List<Object> list = new ArrayList<Object>();
		list.add(new Student(102, "李四"));
		list.add(new Student(103,"王五"));
		model.addAttribute("list",list);
		
		
		return "student";
	}
}

4、页面

<body>
	<h2>thymeleaf</h2><br>
	
	<!-- 渲染文本 -->
	<span th:text="${test}"></span><br>
	
	<!-- 对象 -->
	<span th:text="${stu.id}"></span><br>
	<span th:text="${stu.name}"></span><br>
	
	<!-- 替换超链接 (带参数) -->
	<a href="https://www.zy.com" th:href="@{https://www.baidu.com(id=${stu.id})}">带参链接</a><br>
	
	<!-- 替换超链接 (无参数) -->
	<a href="https://www.zy.com" th:href="@{https://www.baidu.com}">无参链接</a><br>
	
	<!-- 循环 -->
	<ul>
		<li th:each="s:${list}" th:text="${s.name}"></li>
	</ul>
</body>

项目结构
在这里插入图片描述

浏览器输入:http://localhost:8080/index/test

测试结果

在这里插入图片描述

在这里插入图片描述

常用的th标签

关键字

功能介绍

案例

th:id

替换id

<input th:id="'xxx' + ${collect.id}"/>

th:text文本替换

<p th:text="${collect.description}">description</p>

th:utext

支html的文本替换<p th:utext="${htmlcontent}">conten</p>
th:object替换对象

<div th:object="${session.user}">

th:value属性赋值

<input th:value="${user.name}" />

th:with变量赋值运算

<div th:with="isEven=${prodStat.count}%2==0"></div>

th:style设置样式

th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"

th:onclick点击事件

th:οnclick="'getCollect()'"

th:each属性赋值

tr th:each="user,userStat:${users}">

th:if判断条件

<a th:if="${userId == collect.userId}" >

th:unless和th:if判断相反

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>

th:href链接地址

<a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />

th:switch多路选择 配合th:case 使用

<div th:switch="${user.role}">

th:caseth:switch的一个分支

<p th:case="'admin'">User is an administrator</p>

th:fragment布局标签,定义一个代码片段,方便其它地方引用

<div th:fragment="alert">

th:include布局标签,替换内容到引入的文件

<head th:include="layout :: htmlhead" th:with="title='xx'"></head> />

th:replace布局标签,替换整个标签到引入的文件

<div th:replace="fragments/header :: title"></div>

th:selectedselected选择框 选中

th:selected="(${xxx.id} == ${configObj.dd})"

th:src图片类地址引入

<img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />

th:inline定义js脚本可以使用变量

<script type="text/javascript" th:inline="javascript">

th:action表单提交的地址

<form action="subscribe.html" th:action="@{/subscribe}">

th:remove删除某个属性

<tr th:remove="all">

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值