11、SpringBoot-CRUD-thymeleaf公共页面元素抽取

thymeleaf公共页面元素抽取

存在一种现象:两个文件的代码只有一部分代码不一样

其余的均相同,此时就可以提取公共的代码去简化开发

1、抽取公共片段
<div th:fragment="copy">
&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">
    &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>

效果
th:insert:
<div>
    <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
</div>

th:replace:
<footer>
&copy; 2011 The Good Thymes Virtual Grocery
</footer>

th:include:
<div>
&copy; 2011 The Good Thymes Virtual Grocery
</div>

关于模板名:

1.假设在同一目录:

a.html

b.html

此时b用a中的公共片段就是  th:replace="~{a::#selector}"

2.不在同一目录

commons

-a.html

b.html

commons和b.html在同一级目录

此时b用a中的公共片段是  th:replace="~{commons/a::#selector}"

实例:

1.dashboard.html

<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0"     th:fragment="top">
   <a class="navbar-brand col-sm-3 col-md-2 mr-0" >[[${session.user}]]</a>
   <input class="form-control form-control-dark w-100" type="text" placeholder="Search" aria-label="Search">
   <ul class="navbar-nav px-3">
      <li class="nav-item text-nowrap">
         <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">Sign out</a>
      </li>
   </ul>
</nav>

list.html

<!-- 引入公共的模板 -->
<!-- 模板名:会使用默认的thymeleaf的前后配置规则进行解析 -->
<div th:replace="~{dashboard::#selector}"></div>

2.dashboard.html

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

list.html

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

此时使用的是选择器

class 用   .

id 用   

引入片段的时候传入参数

判断属性进行高亮的设置

主要是class属性active值是否存在

<li class="nav-item">
    <a class="nav-link active"
       th:class="${activeUri==‘main.html‘?‘nav‐link active‘:‘nav‐link‘}"
       href="#" th:href="@{/main.html}">
        <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>
        Dashboard <span class="sr-only">(current)</span>
    </a>
</li>
<li class="nav-item">
    <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
        <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-file">
            <path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path>
            <polyline points="13 2 13 9 20 9"></polyline>
        </svg>
        Orders
    </a>
</li>
<li class="nav-item">
    <a class="nav-link" href="http://getbootstrap.com/docs/4.0/examples/dashboard/#">
        <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>
        Products
    </a>
</li>
<li class="nav-item">
    <a class="nav-link active"
       th:class="${activeUri==‘emps‘?‘nav‐link active‘:‘nav‐link‘}"
       th:href="@{/emps}" href="@{/emps}">
        <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>
<div th:replace="~{commons/bar::.sider(activeUri=‘emps‘)}"></div>

前台接受手台的数据

public class Employee {

   private Integer id;
    private String lastName;

    private String email;
    //1 male, 0 female
    private Integer gender;
    private Department department;
    private Date birth;
...}

public class Department {

   private Integer id;
   private String departmentName;
...}
@Repository
public class DepartmentDao {
   private static Map<Integer, Department> departments = null;
   static{
      departments = new HashMap<Integer, Department>();
      departments.put(101, new Department(101, "D-AA"));
      departments.put(102, new Department(102, "D-BB"));
      departments.put(103, new Department(103, "D-CC"));
      departments.put(104, new Department(104, "D-DD"));
      departments.put(105, new Department(105, "D-EE"));
   }
   public Collection<Department> getDepartments(){
      return departments.values();
   }
   public Department getDepartment(Integer id){
      return departments.get(id);
   }
}
@Repository
public class EmployeeDao {

   private static Map<Integer, Employee> employees = null;

   @Autowired
   private DepartmentDao departmentDao;

   static{
      employees = new HashMap<Integer, Employee>();

      employees.put(1001, new Employee(1001, "E-AA", "[email protected]", 1, new Department(101, "D-AA")));
      employees.put(1002, new Employee(1002, "E-BB", "[email protected]", 1, new Department(102, "D-BB")));
      employees.put(1003, new Employee(1003, "E-CC", "[email protected]", 0, new Department(103, "D-CC")));
      employees.put(1004, new Employee(1004, "E-DD", "[email protected]", 0, new Department(104, "D-DD")));
      employees.put(1005, new Employee(1005, "E-EE", "[email protected]", 1, new Department(105, "D-EE")));
   }

   private static Integer initId = 1006;

   public void save(Employee employee){
      if(employee.getId() == null){
         employee.setId(initId++);
      }

      employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
      employees.put(employee.getId(), employee);
   }

   public Collection<Employee> getAll(){
      return employees.values();
   }

   public Employee get(Integer id){
      return employees.get(id);
   }

   public void delete(Integer id){
      employees.remove(id);
   }
}
@Controller
public class EmployeeController {
    @Autowired
    EmployeeDao employeeDao;
    //查询所有员工返回列表页面
    @GetMapping("/emps")
    public String list(Model model){

        Collection<Employee> emps = employeeDao.getAll();
        //将查询到的数值放在请求域中
        model.addAttribute("emps",emps);

        //目标引擎会自动进行拼串
        return "emp/list";
    }
}

前段页面进行数据的获取

<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>
      </tr>
   </thead>
   <tbody>
      <!-- 遍历emps,取出的数值叫emp-->
      <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>
            <button class="btn btn-sm btn-primary">修改</button>
            <button class="btn btn-sm btn-danger">删除</button>
         </td>
      </tr>
   </tbody>
</table>

原文地址:https://www.cnblogs.com/Mrchengs/p/10354884.html

时间: 2024-11-09 15:06:45

11、SpringBoot-CRUD-thymeleaf公共页面元素抽取的相关文章

thymeleaf公共页面元素抽取

thymeleaf公共页面元素抽取 1.抽取公共片段 <div th:fragment="copy"> © 2011 The Good Thymes Virtual Grocery </div> 2.引入公共片段 <div th:insert="~{footer :: copy}"></div> ~{templatename::selector}:模板名::选择器 ~{templatename::fragmentnam

Thymeleaf静态资源引入方式及公共页面代码抽取

静态资源引入 Thymeleaf模板引擎url问题,要用如下的方式写,确保在任何情况下都能访问到 <!-- Bootstrap core CSS --> <link href="bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"> <!-- 引用webjars的方式引入静态资源 --> <link th:href="@{/webjars/bootstrap/

SpringBoot 结合 Thymeleaf 进行页面的跳转

1.引入thymeleaf依赖 <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 2.在application.yml进行thymeleaf配置 # 配置页面地址 spring: thyme

如何针对Thymeleaf模板抽取公共页面

对于公共页面(导航栏nav.页头head.页尾footer)的抽取有三种方式:???????1)基于iframe进行抽取,这种方式很有效,但比较老了,另外为了页面的自适应性,还得做不少工作:???????2)如果是jsp页面,则可以通过<%@ include file="head.jsp"%>进行抽取???????3)利用ajax进行页面加载,但有不少问题???????基于Springboot后台开发框架,JSP虽然还是可以用,但已经强烈推荐使用Thymeleaf模板了,这

SpringBoot学习------SpringBoot使用Thymeleaf模块访问不了静态页面

SpringBoot使用Thymeleaf模块访问不了静态页面 最近学习SpringBoot的过程中使用了Thymeleaf模块引擎,页面发送请求后老是无法显示静态页面,所有的步骤都是参考资料来执行,自我检查好久都没有找到问题的答案,哎呦,我这暴脾气就上来了,一个小页面就想难倒我?那我还怎么找到ONE PIECE? 下面就给大家分享一下我悲惨的心路历程: 要使用Thymeleaf模块引擎,我们首先在pom文件中引入相关依赖如下: 这边我们不需要指定版本,因为SpringBoot默认会使用spri

thymeleaf引用公共页面

本例采用的是Springboot+thymeleaf,因为公共页面属于动态页,因此需要放在templates目录下(具体几层自定义即可),我们这里做一个引用头部.主体和底部公共信息的示例 1. 公共信息页面 head.html,使?th:fragment 属性来定义被包含的模版?段,以供其他模版引用或者包含,这里我们定义了头部(admin_head(title)).主体(admin_common).底部(admin_bottom)三个模板片段 <!DOCTYPE html> <html&

Springboot集成Thymeleaf中遇到的问题------不能返回页面,只返回字符串

springboot集成thymeleaf中遇到的问题: 不能返回页面,只返回字符串 原因:在controller中使用了注解@RestController 或者注解@ResponseBody 解决方法:用 @Controller 代替 @RestController: 不使用@ResponseBody注解. 原文地址:https://www.cnblogs.com/BenNiaoXianFei/p/12672384.html

【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限

开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他URL,均跳转至登录页面 ② 登录涉及帐号和密码,帐号错误提示帐号错误,密码错误提示密码错误 ③ 登录成功跳转至首页,首页显示登录者帐号信息,并有注销帐号功能,点击注销退出系统 ------------------------------------------------------------------

基于页面元素定位的操作失败自动截图

web自动化脚本运行失败,很大一部分问题出在元素定位上,博主在这里介绍一个循环定位及失败截图的方法.以方便脚本维护,定位问题. 1.以下代码是WebDriver自带的方法,封装一下,使用即可. 1 /** 2 * 截取屏幕截图并保存到指定路径 3 * 4 * @param filepath 5 * 保存屏幕截图完整文件名称及路径 6 * @return 无 7 */ 8 public void captureScreenshot(String filepath) { 9 File screenS