SpringBoot整合thymeleaf及常用th:标签使用方法

  1. pom文件加入启动依赖
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. yml文件配置前缀 后缀 关闭缓存
spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
  1. 在resources目录下创建templates文件夹 创建thymeleaf.html
    在这里插入图片描述
<!DOCTYPE html>
<!-- 头部加入thymeleaf命名空间 th标签引入 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!-- 服务器处理thymeleaf页面时 会读取th:text指定的值替换原本标签体的值 -->
<p th:text="经过服务器处理后可以看见的内容">直接在浏览器打开可以看见的内容</p>
</body>
</html>
  1. 编写测试controller
@RequestMapping("/thymeleaf")
public String thymeleaf(){
    System.out.println(1111);
    return "thymeleaf";
}
  1. 启动项目 测试
    在这里插入图片描述
  2. 常用th标签使用
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    div{
        width: 100px;
        height: 50px;
    }
</style>
<body>
<!-- 服务器处理thymeleaf页面时 会读取th:text指定的值替换原本标签体的值 -->
<p th:text="经过服务器处理后可以看见的内容">直接在浏览器打开可以看见的内容</p>
<h3>替换原本标签体  th:text</h3>
<p th:text="新值">原始值</p>
<h3>替换原始属性值  th:value</h3>
<input value="old-value" th:value="new-value">
<br>
<br>
<div style="height: 50px;width: 100px; background-color: blue" th:style="'height: 50px;width: 100px; background-color: chartreuse'"></div>
<h3>替换文本域     th:text</h3>
<textarea th:text="文本域的新值">文本域的原始值</textarea>
<h3>获取后端请求域里面的数据</h3>
<h4>request</h4>
<p th:text="${param1}"></p>
<h4>session</h4>
<p th:text="${session.param2}"></p>
<h4>application</h4>
<p th:text="${application.param3}"></p>
<h3>解析URL地址 @{}相当于前置添加了 ${pageContext.request.contextPath}</h3>
<!-- img标签的src属性 a标案的href属性 其他标签的 th:text th:value 属性-->
<!-- 备注一下 才发现 th:text标签不能有空格 暂时没研究为什么怎么搞? -->
<a th:href="@{/thymeleaf}" th:text="轻轻地我走了正如我轻轻的来">轻轻地我走了 正如我轻轻的来</a>
<h3>直接执行表达式 在页面上 不是在th:标签上使用了</h3>
<p>有转义效果的: [[${param1}]]</p>
<p>无转义效果的: [(${param1})]</p>
<h3>if判断的使用</h3>
<p th:if="${#strings.isEmpty(param1)}"> request里面的 param1 为空</p>
<p th:if="${ not #strings.isEmpty(param1)}"> request里面的 param1 不为空</p>
<h3>for循环的使用</h3>
<table>
    <tr th:each="num: ${list}">
        <td th:text="${num}"></td>
    </tr>
</table>
<h3>页面包含</h3>
<p>先创建需要包含的页面在 include/part.html 内部</p>
<p>~{A::B}表达式 其中A为要被包含片段所在的页面 配合yml里面配置的前缀后缀 即为 /template/include/part.html B为被包含片段的名字 th:fragment指定</p>
<h4>th:insert 表示将 被包含片段直接插入 该标签内部</h4>
<div th:insert="~{include/part :: myfirst}"></div>
<h4>th:replace 表示将 被包含片段直接替换该标签 设置的背景颜色 被替换不存在了</h4>
<div style="background-color: chartreuse" th:replace="~{include/part :: mysecond}"></div>
<h4>th:include 表示将 被包含片段直接被包含进来而且不影响style设置</h4>
<div style="background-color: burlywood" th:include="~{include/part :: mythird}"></div>
</body>
</html>

页面显示效果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring BootThymeleaf 模板引擎集成了 Thymeleaf 模板引擎,可以轻松地与 Spring Boot 应用程序集成。Thymeleaf 是一个流行的模板引擎,它为 Web 应用程序提供了强大的视图层支持,允许您使用 HTML 模板来构建 Web 应用程序的视图层。 在 Spring Boot 中,您可以使用以下步骤来集成 Thymeleaf 模板引擎: 1. 在 pom.xml 文件中添加 Thymeleaf 依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 在 application.properties 文件中配置 Thymeleaf 模板引擎: ```properties spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` 3. 创建 Thymeleaf 模板文件: 在 src/main/resources/templates 文件夹下创建 HTML 文件,这些文件将作为视图层的模板文件。您可以使用 Thymeleaf 提供的标准语法来构建模板文件。 4. 创建控制器类: 创建控制器类来处理 Web 请求,并返回 Thymeleaf 模板文件作为响应。您可以使用 @Controller 注解将类标记为控制器类,并使用 @GetMapping 或 @PostMapping 注解将方法标记为处理 GET 或 POST 请求。 ```java @Controller public class MyController { @GetMapping("/") public String home(Model model) { model.addAttribute("message", "Hello, Thymeleaf!"); return "home"; } } ``` 在这个例子中,控制器类将一个名为 message 的属性添加到模型中,并返回名为 home 的 Thymeleaf 模板文件。 5. 在 Thymeleaf 模板文件中使用模型数据: 使用 Thymeleaf 标准语法在模板文件中使用模型数据。在本例中,您可以使用 ${message} 表达式来访问 message 属性的值。 ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Home</title> </head> <body> <h1 th:text="${message}"></h1> </body> </html> ``` 6. 运行应用程序: 使用 mvn spring-boot:run 命令来启动应用程序。访问 http://localhost:8080/ 将显示名为 home 的 Thymeleaf 模板文件,其中包含 message 属性的值。 上述是 Spring Boot 整合 Thymeleaf 的基本流程和步骤,您可以根据实际需求和业务场景进行适当的调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值