SpringBoot中thymeleaf的使用

本文详细介绍了在SpringBoot中如何使用Thymeleaf模板引擎,从配置pom.xml,设置html页面路径,导入名称空间,引用css样式,到发送请求,抽取公共页面,传递参数,以及进行CRUD操作,包括查询员工列表、添加和修改页面的设计以及删除员工的方法。
摘要由CSDN通过智能技术生成

 

1、在pom.xml中导入thymeleaf

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

   需要将html页面放在templates下面,thymeleaf模板引擎就会自动渲染。

2、在html中使用thymeleaf需要导入名称空间

<html lang="en"  xmlns:th="http://www.thymeleaf.org">

3、导入webjars中的css样式

<link href="asserts/css/bootstrap.min.css" th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">

4、导入springboot中static目录下的自己的css样式

<link href="asserts/css/signin.css" th:href="@{/asserts/css/signin.css}" rel="stylesheet">

5、在thymeleaf中发送请求

//发送http://localhost:8080/user/login请求
<form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
    ...
</form>

6、thymeleaf公共页面的抽取

将工具的html代码片段写在common目录的 bar.html中。

第一种使用th:fragment
<div th:fragment="topbar">  topbar为起的片段名称
	....
</div> 

引用方法:
<div th:replace="common/bar::topbar"></div>

第二种:使用id选择器
<div id="sidebar">
	....
</div> 

引用方法:
<div th:replace="common/bar::#sidebar"></div>

th:insert:将公共片段整个插入到声明引入的元素中
th:replace:将声明引入的元素替换为公共片段 
th:include:将被引入的片段的内容包含进这个标签中 

7、thymeleaf引入片段时传递参数


                
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值