【实验】-员工列表-公共页抽取|学习笔记

简介: 快速学习【实验】-员工列表-公共页抽取

开发者学堂课程【SpringBoot快速掌握 - 核心技术:【实验】-员工列表-公共页抽取 】学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/612/detail/9250


【实验】-员工列表-公共页抽取

内容介绍

一、员工列表

二、三种引入功能片段的 th 属性

三、公共页抽取示例

 

一、员工列表

前边说了实验的一些要求,现在就根据这些要求来完成第一个功能,员工列表。先登录来到后台的主面板界面,希望点击 Customers 来查询所有员工的信息,点击  Customers 后,查出所有员工来到列表页面。列表页面在 restful-crud- 实验 /list 里边,可以进行增量检查。

来到主页面的代码编辑对话框 dashboard.html 里。找到 Customers 发请求。

Ctrl+f 查找,Customers ,将其改成中文:员工管理,修改超链接。

……

</1i>

<li class="nav-item" >

<a class="nav-link"href="#" th : href="@{/emps}">

……

</svg>

员工管理

</a>

</1i>

上节课,已经修改成@{/}的方式进行路径引入,引入名称空间。发送 emps 请求,查询所有员工用 get 方式。

在 controller 文件下新建 EmployeeController 文件,用它来处理和员工相关的请求。(实例操作看内容:三、公共页抽取示例)

thymeleaf 公共页面元素抽取

1、抽取公共片段

<div th:fragment="copy">

© 2011 The Good Thymes Virtual Grocery

</div>

2、引入公共片段

<div th:insert="~{footer :: copy}"></div>

~{templatename::selector}:模板名::选择器

~{templatename::fragmentname}:模板名::片段名

3、默认效果

insert 的功能片段在 div 标签中

如果使用 th:insert 等属性进行引入,可以不用写~{} :

行内写法可以加上: [[~{}]];[(~{})];

 

二、三种引入功能片段的th属性

th:insert  :将公共片段整个插入到声明引入的元素中

th:replace  :将声明引入的元素替换为公共片段

th:include  :将被引入的片段的内容包含进这个标签中

例:

<footer th:fragment-"copy">

© 2011 The Good Thymes Virtual Grocery

</footer>

引入方式

<div th:insert= "footer :: copy"></div>

<div th:replace-"footer :: copy"></div>

<div th:include-"footer :: copy"></div>

效果

<div>

<footer>

© 2011 The Good Thymes Virtual Grocery

</footer>

</div>

<footer>

© 2011 The Good Thymes Virtual Grocery

</footer>

<div>

© 2011 The Good Thymes Virtual Grocery

</div>

三、公共页抽取示例

1、发出“查询所有员工”请求

先登录到 Dashboard 后台主页面。点击 Cuctomers 查询所有员工,来到所有员工的列表页面,restul-crud-  实验里边的 list.html 文件,打开,在 Section title 列表页面里可以对它进行检查。

回到 main.html 主页面。点击 Cuctomers 来到 dashboard.html ,发出查询所有员工的 emps 请求是 GET 方式。编辑:

</1i>

<1i class="nav-item">

<a class=" nav- link" href= "#" th:href="@{/emps}">

<svg ……

</svg>

员工管理

</a>

</1i>

2、更改主页侧边栏的 Cuctomers 为“员工管理”

在 controlle 里新建 Create New Class ,名为 EmployeeController 。

当然,所有与员工有关的页面,都放在 templates 下的 emp 文件夹里,新建 emp文件,将 list.html 放入到 emp 里。

在 EmployeeControllerjava 里编辑:

package com. atguigu.springboot.controller;import org.springframework.stereotype.Controller;import com. atguigu. springboot . dao. EmployeeDao;import com. atguigu. springboot . entities . Employee;import org . springframework . beans . factory . annotat ion . Autowired;import org. springframework. stereotype .Controller; import org . springframework . ui .Model;import org . springframework . web . bind. annotation . GetMapping;import java. util.Collection;@Controllerpublic class EmployeeController {@AutowiredEmployeeDao employeeDao;

//查询所有员工返回列表页面

@GetMapping("/emps")public String list(Model model){Collectionemployees=employeeDao.getAll();

//放在请求域中

model.addAttribute(attributeName:"emps",employees);

// thymeleaf默认就会拼串

// classpath:/templates/xxxx. htmlreturn "emp/list";}}

访问)  localhost:8080/crud/emps  输入登录名密码,进入主页面localhost:8080/crud/main.html 看到侧边栏的 Cuctomers 被改变成员工管理。点击员工管理,进入 localhost:8080/crud/emps 页面,看到 Company name 标题栏,侧边栏等还未改变。仍旧可以抽取。

3、在 dashboard.html 页面抽取 topbar

<body>

<nav class="navbar navbar-dark sticky-top bg-dark flex-md- nowrap p-0" th:fragment= "topbar">

……

来到 list.html 页面, 的  标签可以删掉了,改为引入:

<body>

<!--引入抽取的topbar-->

<!--模板名:会使用thymeleaf的前后缀配置规则进行解析-->

<div th:replace="dashboard: :topbar"></div>

刷新 localhost:8080/crud/emps 页面。可以看见原来的标题标签 Company name 变为 admin 。说明抽取没有问题。

同样,在 dashboard.html 里, 的  标签页可以抽取。

<body>

<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0" th:fragment="topbar">

……

<nav class="col-md-2 d-none d-md-block bg-light sidebar" id= "sidebar">

<div class="sidebar-sticky">

……

回到 list.htm,同样将

<div class=" container-fluid">

<div class="row">

后边的  标签删除。继续编辑:

……

<div class="container-fluid">

<div class="row">

<!--引入侧边栏-->

<div th:replace= "~{dashboard: :#sidebar}"></div>

……

然后,刷新,看到侧边栏的 Cuctomers 变成 员工管理。引入抽取成功。



相关文章
|
SQL 算法 大数据
深入解析力扣181题:超过经理收入的员工(自连接方法详解及模拟面试问答)
深入解析力扣181题:超过经理收入的员工(自连接方法详解及模拟面试问答)
|
机器学习/深度学习 缓存 Prometheus
Spring Boot 框架之自动配置原理、SpringBoot 监控、自定义封装 Starter 启动器等使用指南
Spring Boot 框架之自动配置原理、SpringBoot 监控、自定义封装 Starter 启动器等使用指南
898 0
Spring Boot 框架之自动配置原理、SpringBoot 监控、自定义封装 Starter 启动器等使用指南
|
存储 Java 索引
Java集合框架:ArrayList和LinkedList的区别是什么?
Java集合框架:ArrayList和LinkedList的区别是什么?
109 0
|
数据采集 弹性计算 供应链
阿里云服务器付费模式怎么选择?有什么区别?
阿里云服务器ECS付费类型包年包月、按量付费和抢占式实例有什么区别?包年包月先付费后使用,最低购买一个月时长,平均下来价格优惠;按量付费先使用后付费,按小时结算费用,适合短期使用,平均下来费用要比包年包月贵一些;抢占式实例和按量付费相类似,只是价格上要比按量的优惠90%,但是抢占式实例可能会被系统释放,抢占式实例适合无状态的应用。阿里云百科来详细说下阿里云服务器付费模式包年包月、按量付费和抢占式实例区别以及选择方法:
348 0
阿里云服务器付费模式怎么选择?有什么区别?
|
关系型数据库 MySQL
MySQL基础-删除/更新行为
添加了外键之后,再删除父表数据时产生的约束行为,我们就称为删除/更新行为。具体的删除/更新行为有以下几种:
300 0
|
存储 网络协议 算法
iptables学习笔记
iptables学习笔记
iptables学习笔记
|
XML 开发工具 C++
Visual Studio Package 插件开发(Visual Studio SDK)(二 )
Visual Studio Package 插件开发(Visual Studio SDK)(二 )
295 0
Visual Studio Package 插件开发(Visual Studio SDK)(二 )
|
Shell
导出EXCEL中的文件到资源管理器
可以编写一宏,使导出的文件与原文件完全一样 Sub DoIt()    Dim o As OLEObject    Dim TargetDir As String    '导出文件的目标目录   TargetDir = "D:\"      Dim CmdLine As String     '使用"",是为了防止文件夹带空格   CmdLine = "explorer.
793 0