springboot使用thymeleaf

本文介绍了Thymeleaf模板引擎的配置和基本使用,包括在Spring Boot项目中引入依赖,创建测试HTML页面,并展示了如何使用th:text、th:href等属性进行数据渲染和条件判断。此外,还列举了Thymeleaf的常用语法,如th:id、th:object、th:each等,以及它们在实际应用中的功能和示例。
摘要由CSDN通过智能技术生成

1.引入依赖

<dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring‐boot‐starter‐thymeleaf</artifactId> 
</dependency> 
<properties> 
        <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
        <thymeleaf‐layout‐dialect.version>2.2.2</thymeleaf‐layout‐dialect.version> 
</properties> 

thymeleaf会自动渲染resource/templates下的html文件,静态资源存放在resource/static中

2.新建测试html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div th:text="${msg}">这是显示欢迎信息</div>
<br/>
id: <span th:text="${user.name}"></span>
name: <span th:text="${user.age}"></span>
age: <span th:text="${user.sex}"></span>
<br/>
<br/>
<a href="#" th:href="'http://localhost:8080/user?id=' + ${user.age}">a标签文本内容</a>
<br/>
<p th:each="user: ${userList}">
    <span th:text="${user.sex}" >xxx</span>
    <span th:text="${user.name}" >xxx</span>
    <span th:text="${user.age}" >xxx</span>
</p>
<span th:if="${user.sex eq '男'}">1</span>
<span th:if="${user.sex eq '女'}">2</span>
</div>
</body>
</html>

展示效果

 3.常用写法

关键字功能介绍案例
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:onclick="'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"> 1.all:删除包含标签和所有的孩子。
th:attr设置标签属性,多个属性可以用逗号分隔比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。

字符串拼接可用‘|’

 <span th:text="|hello, ${user.name}!|">

在Spring Boot中使用Thymeleaf是相对简单的,你只需要在你的项目中加入Thymeleaf的相关依赖即可。使用Thymeleaf步骤如下: 1. 在你的项目中添加Thymeleaf的依赖。可以通过Maven或者Gradle来添加依赖。例如,在Maven中,你可以在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在你的Spring Boot应用的配置文件中,配置Thymeleaf的一些属性。你可以在`application.properties`文件中添加下面的配置: ``` spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false ``` 在这个例子中,我们设置了模板文件的前缀为`classpath:/templates/`,后缀为`.html`,并且将缓存关闭。 3. 创建Thymeleaf模板文件。在`src/main/resources/templates/`目录下创建你的HTML模板文件。在这些模板文件中,你可以使用Thymeleaf的标记来进行动态数据绑定和渲染。 4. 在你的Spring Boot应用中使用Thymeleaf。你可以在Controller的方法中返回一个字符串,该字符串指定了你要渲染的Thymeleaf模板文件的名称。Spring Boot会自动根据配置的前缀和后缀来查找并渲染对应的模板文件。 以上就是在Spring Boot中使用Thymeleaf的简单步骤。通过这些步骤,你可以在你的Spring Boot应用中使用Thymeleaf来进行视图的渲染和展示。希望对你有帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值