SpringBoot利用thymeleaf设置公共页面

SpringBoot利用thymeleaf设置公共页面

步骤:

1. 在templates下创建一个新的html页面,用于存放公共部分
在这里插入图片描述

2.将公共部分用div标签或是别的标签包裹起来(如下图中head标签中的内容)
th:fragment="my_css"此项就是给该标签命名,用于后续的使用

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="my_css">
    <link rel="stylesheet" href="assets/css/bootstrap.min.css" th:href="@{/assets/css/bootstrap.min.css}">
    <link rel="stylesheet" href="assets/css/owl.carousel.min.css"th:href="@{/assets/css/owl.carousel.min.css}">
    <link rel="stylesheet" href="assets/css/flaticon.css"th:href="@{/assets/css/flaticon.css}">
    <link rel="stylesheet" href="assets/css/slicknav.css"th:href="@{/assets/css/slicknav.css}">
    <link rel="stylesheet" href="assets/css/animate.min.css"th:href="@{/assets/css/animate.min.css}">
    <link rel="stylesheet" href="assets/css/magnific-popup.css"th:href="@{/assets/css/magnific-popup.css}">
    <link rel="stylesheet" href="assets/css/fontawesome-all.min.css"th:href="@{/assets/css/fontawesome-all.min.css}">
    <link rel="stylesheet" href="assets/css/themify-icons.css"th:href="@{/assets/css/themify-icons.css}">
    <link rel="stylesheet" href="assets/css/slick.css"th:href="@{/assets/css/slick.css}">
    <link rel="stylesheet" href="assets/css/nice-select.css"th:href="@{/assets/css/nice-select.css}">
    <link rel="stylesheet" href="assets/css/style.css"th:href="@{/assets/css/style.css}">
</head>

3.在需要的地方使用 th:include 或 th:replace 或 th:insert
语法:th:insert="公共页面名 :: 要引用的代码块名字"
如:<div th:insert="common:: test"></div> common就是公共页面名,test就是要引用的代码块名字

下例是引用步骤2中的代码:

<!doctype html>
<html lang="zxx" xmlns:th="http://www.thymeleaf.org">
<head>
  <!-- CSS here -->
   <link th:include="common::my_css">
</head>

三者的区别:如公共页面代码中
假设公共页面的名字就是common.html

<span th:fragment="test">
    this is content!
</span>

1.th:insert (保留自己的标签,并将引用的标签内容及其标签都嵌入自己的标签内)

编译前:
<div th:insert="common:: test"></div>
编译后:
<div><span> this is content!</span></div>

2.th:replace(替换自己的标签)

编译前:
<div th:replace="common:: test"></div>
编译后:
<span> this is content!</span>

3.th:include(将被引用的标签中的内容嵌入自己的标签中)

编译前:
<div th:include="common :: test"></div>
编译后:
<div> this is content!</div>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值