iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot公共页面抽取方法实现过程介绍
  • 320
分享到

SpringBoot公共页面抽取方法实现过程介绍

2024-04-02 19:04:59 320人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录1. 需求描述2. 使用 thymeleaf 抽取公共页面方法3. insert与replace及include抽取公共页面的区别1. 需求描述 我们有这样一个页面,其具有左侧导

1. 需求描述

我们有这样一个页面,其具有左侧导航和上侧导航,在切换不同内容过程中,左侧导航和上册导航不变,也就是说我们想将左侧导航和上侧导航这个公共部分抽取出来。

注意:使用 thymeleft 必须引入 <html lang="en" xmlns:th="Http://www.thymeleaf.org">

2. 使用 thymeleaf 抽取公共页面方法

利用 thymeleaf 的 insert、replace 及 include 方法。

首先创建一公共页面 html,叫做 common.html,把公共的内容放进去。并通过 id="leftmenu"th:fragment="headermenu" 标注需要抽取的内容。

方法一 id="leftmenu"

<!-- left side start-->
<div id="leftmenu" class="left-side sticky-left-side"> 内容 </div>
<!-- left side end-->

方法二th:fragment="headermenu"

<!-- header section start-->
<div th:fragment="headermenu" class="header-section">内容 </div>
<!-- header section end-->

在需要引入的位置,通过insert、replace 及 include 方法引入 引入 id="leftmenu" 标识的公共部分 (要加 # 号):

<div th:replace="common :: #leftmenu"></div>

引入通过 th:fragment="headermenu" 标识的公共部分:

<div th:replace="common :: commonheader"> </div>

3. insert与replace及include抽取公共页面的区别

  • insert: 保留原来大标签,也保留引入部分的标签
  • replace: 保留原来的大标签,不保留引入部分的标签
  • include: 不保留原来的大标签,保留引入部分的标签

以插入下列 commonheader 为例:

commonheader 在 common.html 的 header 中,是每个页面都要引入的 CSSjs 文件

<head th:fragment="commonheader">
    <!--common-->
    <link href="css/style.css" rel="external nofollow"  th:href="@{/css/style.css}" rel="external nofollow"  rel="stylesheet">
    <link href="css/style-responsive.css" rel="external nofollow"   th:href="@{/css/style-responsive.css}" rel="external nofollow"  rel="stylesheet">
    <script src="js/HTML5shiv.js" th:src="@{/js/html5shiv.js}"></script>
    <script src="js/respond.min.js"th:src="@{/js/respond.min.js}" ></script>
</head>

1. 使用 include

<div th:include="common :: commonheader"> </div>

结果引入部分无大标签 header(检查页面源代码功能),但是 div 还在:

2. 使用 replace:

<div th:replace="common :: commonheader"> </div>

结果引入部分含大标签 header,浏览器语法检测处理掉了,但是没有 div(检查页面源代码功能):

2. 使用 insert:

<div th:insert="common :: commonheader"> </div>

结果引入部分含大标签 header,也含 div(检查页面源代码功能):

到此这篇关于SpringBoot公共页面抽取方法实现过程介绍的文章就介绍到这了,更多相关springBoot公共页面内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot公共页面抽取方法实现过程介绍

本文链接: https://www.lsjlt.com/news/168886.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringBoot公共页面抽取方法实现过程介绍
    目录1. 需求描述2. 使用 thymeleaf 抽取公共页面方法3. insert与replace及include抽取公共页面的区别1. 需求描述 我们有这样一个页面,其具有左侧导...
    99+
    2022-11-13
  • SpringBoot异常错误页面实现方法介绍
    目录导言实现步骤加入thymeleaf依赖然后创建controller使用X进行模糊匹配统一错误页面显示导言 默认情况下,SpringBoot 项目错误页面如下: 直接这样太丑了...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作