thymeleaf使用_thymeleafautoconfig-程序员宅基地

技术标签: thymeleaf  

一、总结与计划

1.1 总结

  • SpringBoot自动配置回顾

  • 基于SpringBoot整合SSM开发

    • 一个SpringBoot项目本身就是依赖Spring:spring-boot-starter

    • 整合SpringMVC:spring-boot-starter-web

    • 整合MyBatis

      • Druid:druid-spring-boot-starter

      • MyBatis:mybatis-spring-boot-starter

    • 使用案例

      • 数据表

      • 实体类

      • DAO接口

      • Mapper映射

      • servcie

        • 接口

        • 实现类

      • 控制器

    • 基于SpringBoot的单元测试

      • @RunWith(SpringRunner.class)

      • @SpringBootTest(***Application.class)

    • 全局异常处理

    • SpringBoot基于Maven配置profile

1.2 计划

  • 自定义starter

  • thymeleaf

  • 基于thymeleaf练习

二、自定义starter

学习自定义starter目的:

  • 加深对SpringBoot自动配置的理解

  • 封装特定业务实现、提高代码的重用

  • 对SpringBoot内置starter的扩展

2.1 自定义starter的流程说明

  • SpringBoot自动配置流程图

     
  • 自定义starter流程

     

2.2 自定义starter的实现

2.2.1 创建一个新的maven项目

  • 步骤略

  • 导入SpringBoot的依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/>
    </parent>
​
    <groupId>com.qfedu</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>2.2.1</version>
    <packaging>jar</packaging>
​
    <dependencies>
        <!-- SpringBoot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
​
</project>

2.2.2 创建starter的业务实现类

public class HelloTemplate {
    
    public void add(){
        System.out.println("HelloTemplate----add");
    }
​
    public void del(){
        System.out.println("HelloTemplate----del");
    }
​
    public void list(){
        System.out.println("HelloTemplate----list");
    }
​
}

2.2.3 创建自动类

@SpringBootConfiguration
@ConditionalOnClass(HelloTemplate.class)
public class HelloAutoConfig {
    @Bean
    public HelloTemplate getHelloTemplate(){
        return new HelloTemplate();
    }
}

2.2.4 创建并配置spring.factories

  • 在resources目录下创建META-INF目录

  • META-INF目录下创建spring.factories,配置如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.qfedu.hello.HelloAutoConfig

2.2.5 打包

  • clean

  • install

2.2.6 在SpringBoot项目中使用自定义starter

  • 添加自定义starter的依赖

<dependency>
    <groupId>com.qfedu</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>2.2.1</version>
</dependency>
  • 测试类测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootSsmApplication.class)
public class HelloTemplateTest {
​
    @Resource
    private HelloTemplate helloTemplate;
​
    @Test
    public void testMethod(){
        helloTemplate.add();
    }
​
}

2.3 自定义starter的实现扩展

当我们自定义starter在实现具体的业务时,需要全局属性配置

  • 创建属性类:HelloProperties.java

    @ConfigurationProperties(prefix = "spring.hello")
    public class HelloProperties {
    ​
        private String pro1; //spring.hello.pro1
        private String pro2; //spring.hello.pro2
        private String pro3; //...
    ​
        //getter & setter
    }
  • 在业务实现类中通过构造器强制要求传递属性类对象

    public class HelloTemplate {
    ​
        private HelloProperties helloProperties;
        
        //有参构造器传递属性类对象
        public HelloTemplate(HelloProperties helloProperties) {
            this.helloProperties = helloProperties;
        }
    ​
        public void add(){
            System.out.println("HelloTemplate----add:"+helloProperties.getPro1());
        }
    ​
        public void del(){
            System.out.println("HelloTemplate----del:"+helloProperties.getPro2());
        }
    ​
        public void list(){
            System.out.println("HelloTemplate----list:"+helloProperties.getPro3());
        }
    ​
    }
  • 在自动配置类中加载属性

    @SpringBootConfiguration
    @ConditionalOnClass(HelloTemplate.class)
    @EnableConfigurationProperties(HelloProperties.class)
    @ConditionalOnProperty(prefix = "spring.hello",name = {"pro1","pro2","pro3"},matchIfMissing = false)
    public class HelloAutoConfig {
    
        @Autowired
        private HelloProperties helloProperties;
    
        @Bean
        public HelloTemplate getHelloTemplate(){
            return new HelloTemplate(helloProperties);
        }
    }
    
  • 再次测试

    • 添加自定义starter的依赖

      <dependency>
          <groupId>com.qfedu</groupId>
          <artifactId>hello-spring-boot-starter</artifactId>
          <version>2.2.1</version>
      </dependency>
      
    • 在全局配置文件apllication.yml配置属性

      spring:
        hello:
          pro1: aaa
          pro2: bbb
          pro3: ccc
      

三、Thymeleaf

3.1 概述

  • 前端技术

  • JSP

    • JSP优点

      • 动态显示数据

      • 支持逻辑控制(条件、循环)

      • 写Java代码

    • JSP不足

      • JSP必须依赖web容器(Tomcat)才能运行

      • JSP中可以包含java\jstl\el\html\js\css组合,页面的可读性比较差

问题:有没有一种页面显示技术,技能保留JSP的动态显示数据的优点又能避免JSP存在的不足呢?

  • Thymeleaf

    • 是一个用于前端数据显示的模版引擎,他完全可以替代JSP,同时弥补了JSP的不足。

    • 特点:

      • 基于HTML的动态网页(一个thymeleaf页面其实就是一个HTML页面)

      • 基于浏览器运行可以看到静态效果,同时基于服务器运行能够动态显示数据

      • 开发便捷、效率高

3.2 Thymeleaf的基本使用

  • 导入依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  • 配置thymeleaf模版属性(SpringBoot中包了thymeleaf的相关配置,如果我们需要对默认配置进行修改,则可以在application.yml中进行)

    spring:
      thymeleaf:
        prefix: classpath:/templates/
        suffix: .html
        encoding: UTF-8
        cache: false
        mode: HTML5
    
  • 创建控制器

    @Controller
    public class TestController {
    
        @RequestMapping("/test")
        public String test(Model model){
            System.out.println("------->>>>>>>>>>test");
            model.addAttribute("str","从控制器传递到页面的数据");
            return "index";
        }
    
    }
    
  • 创建对应的HTML

    • HTML文件放在templates

    • 在HTML文件的html标签中引入th命名空间

    • 通过th标签可以获取动态数据(从控制器传递到HTML数据)

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        ---test  ---- index
        <label th:text="${str}"></label>
    </body>
    </html>
    

     

3.3 Thymeleaf常用语法

3.3.1 获取变量值

  • 简单数据

<tag th:text="${key}"></tag>
  • 对象数据

<p th:text="${book.bookName}"></p>

3.3.2 运算符

  • 算数运算符

  • 关系运算符

<p th:text="${book.bookName} == 'Java'?'我买':'我不买'"></p>
<p th:text="${book.bookPrice + 3 }"></p>
<p th:text="${book.bookPrice} * 0.8"></p>
<p th:text="'作者:'+${book.bookAuthor}"></p>

3.3.3 *的使用

<div th:object="${book}">
    <p th:text="*{bookId}"></p>
    <p th:text="*{bookName}"></p>
    <p th:text="*{bookAuthor}"></p>
    <p th:text="*{bookPrice}"></p>
    <p th:text="*{bookDesc}"></p>
</div>

3.3.4 内联使用

  • HTML标签内部(innerHTML)取值 th:inline="text"

    <label th:inline="text">[[${str}]]</label>
    
  • 在Javascript标签内部( JS代码)取值

    <script type="text/javascript" th:inline="javascript">
        var s = [[${str}]];
        alert(s);
    </script>
    
  • 在style标签内部取值

    <style type="text/css" th:inline="css">
        div{color: [[${color}]]}
    </style>
    

3.3.5 流程控制

分支语句(条件)

  • if 如果条件成立就显示

    <label th:if="${book.bookPrice} >= 30">太贵了</label>
    
  • unless 如果条件不成立就显示

    <label th:unless="${book.bookPrice} >= 30">-太贵了-</label>
    
  • switch

    <div th:switch="${user.gender}">
        <p th:case="M">男</p>
        <p th:case="F">女</p>
        <p th:case="*">性别不详</p>
    </div>
    

循环语句

  • th:each

    <table>
        <caption>图书信息列表</caption>
        <tr>
            <th>编号</th>
            <th>名称</th>
            <th>作者</th>
            <th>价格</th>
            <th>描述</th>
        </tr>
        <tr th:each="b:${books}">
            <td th:text="${b.bookId}"></td>
            <td th:text="${b.bookName}"></td>
            <td th:text="${b.bookAuthor}"></td>
            <td th:text="${b.bookPrice}"></td>
            <td th:text="${b.bookDesc}"></td>
        </tr>
    </table>
    

     

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u014748504/article/details/108490970

智能推荐

调用系统震动-程序员宅基地

文章浏览阅读1.2k次。如果iPhone OS设备支持振动,则运行在该设备上的应用程序可以通过系统声音服务触发振动,振动的选项通过kSystemSoundID_Vibrate标识符来指定。AudioServicesPlaySystemSound函数可以用于触发振动。#import #_系统震动

十大排序总结_那些排序第一趟可以确定位置-程序员宅基地

文章浏览阅读126次。冒泡排序(平均时间复杂度为O(n^2) ,最好情况为顺序 O(n) ,最坏为逆序O(n^2),空间复杂度为O(1)(1)改进后的冒泡排序算法:void BubbleSort2( Sqlist *L){ int i , j; Status flag = true; //用来作标记,当交换时设为true, for(i = 1; i < L->Le..._那些排序第一趟可以确定位置

linux下dhcp与dhcp中继-程序员宅基地

文章浏览阅读95次。Dhcp就是动态主机配置协议,采用UDP协议,客户端用68号端口,服务器端用67号端口。Dhcp原理:Dhcp中继的原理:把广播转换成单播案例:拓扑图:目标:在不同网段获得ip地址。实验步骤:1、搭建dhcp服务器[root@zlj~]#mkdir/mnt/cdrom[root@zlj~]#..._linux debian dchp中继

C语言 | 三个整数的排序_三个整数排序c语言-程序员宅基地

文章浏览阅读1.6w次,点赞16次,收藏71次。题目:输入三个正整数x,y,z,请把这三个数由小到大输出。【方法一】这是C语言教程里常出现的一道题。处理方法是:假设最小的数为x上,先将x与y进行比较,如果x>y则将x与y的值进行..._三个整数排序c语言

基于spring validation多层对象校验_spring validation 多层-程序员宅基地

文章浏览阅读6.9k次。1、第一层对象定义package com.ybw.validation.demo.vo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import javax.validation.Valid;import java.io.Serializable;/** * 请求参数 * * @author geoffrey * @version V1.0 * @cl_spring validation 多层

落谷 P1140 相似基因_家都知道,基因可以看作一个碱基对序列。它包含了 444 种核苷酸,简记作 a,c,g,ta,c-程序员宅基地

文章浏览阅读916次。题目背景大家都知道,基因可以看作一个碱基对序列。它包含了4种核苷酸,简记作A,C,G,T。生物学家正致力于寻找人类基因的功能,以利用于诊断疾病和发明药物。在一个人类基因工作组的任务中,生物学家研究的是:两个基因的相似程度。因为这个研究对疾病的治疗有着非同寻常的作用。题目描述两个基因的相似度的计算方法如下:对于两个已知基因,例如AGTGATG和GTTAG,将它们的碱基互相对应。当然,中间可以加入一些空_家都知道,基因可以看作一个碱基对序列。它包含了 444 种核苷酸,简记作 a,c,g,ta,c

随便推点

Django 通过JS实现ajax过程详解_django js ajax-程序员宅基地

文章浏览阅读344次。ajax的优缺点AJAX使用Javascript技术向服务器发送异步请求AJAX无须刷新整个页面因为服务器响应内容不再是整个页面,而是页面中的局部,所以AJAX性能高小练习:计算两个数的和方式一:这里没有指定contentType:默认是urlencode的方式发的index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8">_django js ajax

HarmonyOS Full SDK的安装-程序员宅基地

文章浏览阅读308次。OpenHarmony的应用开发工具HUAWEI DevEco Studio现在随着OpenHarmony版本发布而发布,只能在版本发布说明中下载,例如最新版本的OpenHarmony 4.0 Release。下载好后,分别解压出ets js native previewer toolchains,把它们一起放在DevEco Studio中设置的sdk目录下(注意上一级目录要为数字,如果是要替换api9,这个数字就是9。首先,需要到OpenHarmony数字协作平台 下载Full SDK,如下图。

java二分法细节理解_java 二分法 细节-程序员宅基地

文章浏览阅读170次。添加链接描述_java 二分法 细节

Elasticsearch+Logstash+Kibana教程-程序员宅基地

文章浏览阅读235次。参考资料累了就听会歌吧!Elasticsearch中文参考文档Elasticsearch官方文档Elasticsearch其他——那些年遇到的坑Elasticsearch 管理文档Elasticsearch集群配置以及REST API使用Elasticsearch集群管理Elasticsearch 数据搜索篇·【入门级干货】Elasticsea..._kibana 动态地图

ABAQUS 钢筋混凝土结构建模_abaqus中混凝土本构模型-程序员宅基地

文章浏览阅读1k次。复合材料是一种具有优异性能的材料,广泛应用于航空、汽车、建筑、船舶、体育器材等领域。ABAQUS 作为现阶段应用最广泛的有限元仿真模拟软件,优秀的分析能力和模拟复杂系统的可靠性使得 ABAQUS 被各国的工业和科学研究中广泛采用。通过合理的建模和分析,可以更好地理解复合材料的力学行为,为复合材料的设计和应用提供参考。(2D 网格划分、3D 网格划分、过渡网格划分、网格质量检查)四、钢筋混凝土结构静力与动力分析模拟实例(一)10、钢筋混凝土柱的轴压与偏压模拟(实例)4、 ABAQUS 中的混凝土材料模型。_abaqus中混凝土本构模型

FL Studio 21.2.3.4004中文版安装图文教程及fl Studio适用哪些人群_fl studio21.2.3.4004-程序员宅基地

文章浏览阅读748次,点赞37次,收藏14次。音乐制作人:无论是专业的音乐制作人还是初学者,FL Studio 2024都为他们提供了一个完整的音乐制作环境。音乐爱好者和独立音乐人:即使是没有专业音乐背景的音乐爱好者和独立音乐人,也可以通过FL Studio 21.2.3.4004轻松入门音乐制作。声音设计师和音效师:对于需要创作和处理各种声音效果的声音设计师和音效师来说,FL Studio 21.2.3.4004提供了强大的音频编辑和处理工具,可以帮助他们创作出独特而富有创意的声音效果。_fl studio21.2.3.4004

推荐文章

热门文章

相关标签