14. Spring Boot的 thymleaf公共页抽取

 

5)、CRUD-员工列表
实验要求:
1)、RestfulCRUD:CRUD满足Rest风格;
URI: /资源名称/资源标识 HTTP请求方式区分对资源CRUD操作

 

 普通CRUD(uri来区分操作)RestfulCRUD
查询getEmpemp---GET
添加addEmp?xxxemp---POST
修改updateEmp?id=xxx&xxx=xxemp/{id}---PUT
删除deleteEmp?id=1emp/{id}---DELETE

 

 

 

 

 

 

 

 

 2)、实验的请求架构;

 

 

 

 

 

 thymeleaf公共页面元素抽取

1、抽取公共片段,在公共页面footer.html的片段上加上th:fragment属性
<div th:fragment="copy"> 
  &copy; 2011 The Good Thymes Virtual Grocery
</div>
2、引入公共片段,
<div th:insert="~{footer :: copy}"></div>  模板是:footer.html
~{templatename::selector}:模板名::选择器   选择器:标签的id属性值
~{templatename::fragmentname}:模板名::片段名 
3、默认效果:
insert的公共片段在div标签中
如果使用th:insert等属性进行引入,可以不用写~{}:

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

 

 

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

 

<footer th:fragment="copy">
  &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>
    &copy; 2011 The Good Thymes Virtual Grocery
  </footer>
</div>
<footer>
  &copy; 2011 The Good Thymes Virtual Grocery
</footer>
<div>
  &copy; 2011 The Good Thymes Virtual Grocery
</div>

 

 

 

 

 

 

 

 

 侧边栏高亮切换:

1. 公共抽取文件commons/bar.html

<!--sidebar 侧边栏-->
<nav class="col-md-2 d-none d-md-block bg-light sidebar" id="sidebar">
    <div class="sidebar-sticky">
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link active"  href="#" th:href="@{/main.html}" th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-home">
                        <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
                        <polyline points="9 22 9 12 15 12 15 22"></polyline>
                    </svg>
                    已开发票<span class="sr-only">(current)</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#" th:href="@{/addInvoice.html}"  th:class="${activeUri=='addInvoice.html'?'nav-link active':'nav-link'}">
                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart">
                        <circle cx="9" cy="21" r="1"></circle>
                        <circle cx="20" cy="21" r="1"></circle>
                        <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
                    </svg>
                    新开发票<span class="sr-only">(current)</span>
                </a>
            </li>
        </ul>
    </div>
</nav>

 

 员工管理

<li class="nav-item">
    <a class="nav-link" href="#"  th:href="@{/emps}" th:class="${activeUrl == 'emps' ? 'nav-link active' : 'nav-link'}">
        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-users">
            <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
            <circle cx="9" cy="7" r="4"></circle>
            <path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
            <path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
        </svg>
        员工管理
    </a>
</li>

 

 

 

 

 

2. 实际引用页面

  2.1 main.html

<!--引入topbar-->
<div th:replace="commons/bar::topbar"></div>
<div class="container-fluid" height="600px" width="1200px">
    <div class="row">
        <!--引入sidebar commons/bar意为commons路径下的bar.html; #sidebar为bar.html中id为siderbar的标签;括号中是参数-->
        <div th:replace="commons/bar::#sidebar(activeUri='main.html')"></div>
。。。。。。。

 

 

  2.2 add.html

<!--引入抽取的topbar-->
<!--模板名:会使用thymeleaf的前后缀配置规则进行解析-->
<div th:replace="commons/bar::topbar"></div>
    <div class="container-fluid">
        <div class="row">
          <!--引入侧边栏-->
          <div th:replace="commons/bar::#sidebar(activeUri='addInvoice.html')"></div>
。。。。。。

 

3.页面跳转解析

@Bean //将组件注册在容器
public WebMvcConfigurer webMvcConfigurer(){
   WebMvcConfigurer adapter = new WebMvcConfigurer() {
       @Override
       public void addViewControllers(ViewControllerRegistry registry) {
          registry.addViewController("/main.html").setViewName("invoice/main");
          registry.addViewController("/addInvoice.html").setViewName("invoice/add");
       }    
   }
}

 

@GetMapping("/emps")
public String addEmpPage(Model model) {
    return "emp/list";
}

 

员工列表页:

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
    <h2><a class="btn btn-sm btn-success" href="emp" th:href="@{/emp}">员工添加</a></h2> <!-- get请求-->
    <div class="table-responsive">
        <table class="table table-striped table-sm">
            <thead>
                <tr>
                    <th>#</th>
                    <th>lastName</th>
                    <th>email</th>
                    <th>gender</th>
                    <th>department</th>
                    <th>birth</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                <tr th:each="emp:${emps}">
                    <td th:text="${emp.id}"></td>
                    <td>[[${emp.lastName}]]</td>
                    <td th:text="${emp.email}"></td>
                    <td th:text="${emp.gender}==0?'女':'男'"></td>
                    <td th:text="${emp.department.departmentName}"></td>
                    <td th:text="${#dates.format(emp.birth, 'yyyy-MM-dd HH:mm')}"></td>
                    <td>
                        <a class="btn btn-sm btn-primary" th:href="@{/emp/}+${emp.id}">编辑</a>
                        <button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</main>

 

 

转载于:https://www.cnblogs.com/guchunchao/p/9993041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值