【Thymeleaf】使用学习

【Thymeleaf】使用学习

===================================================================

1、spring boot 整合 Thymeleaf

2、spring security 使用

3、文本语法,没有 html 标签

4、标签使用

===================================================================

1、spring boot 整合 Thymeleaf

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

配置

server:
  port: 8088
spring:
  devtools:
    restart:
      enabled: true
  mvc:
    # 静态资源的请求方式
    static-path-pattern: /static/**
  resources:
    # # 静态资源的配置位置
    static-locations: classpath:/static/
  # 模板
  thymeleaf:
    # 开发不缓存false,生产环境缓存true
    cache: false
  # 数据源
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql.dev:3306/gh?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false&autoReConnect=true
    username: root
    password: 123456
    # hikari连接池的参数 相关设置
    hikari:
      # 生效超时
      validationTimeout: 3000
      # 定义获取连接的超时时间。最小250ms,默认30s
      connectionTimeout: 60000
      # 定义连接空闲时间。最小10s,默认10m
      idleTimeout: 60000
      # 定义最小的空闲连接数。推荐不设置。或与最大连接数一致;保持固定的连接数目
      minimumIdle: 10
      # 定义最大的连接数。默认10
      maximumPoolSize: 10
      # 定义连接的最大生命周期。推荐设置该属性。最小30s,默认30m
      maxLifeTime: 60000
      # 从连接池获取到连接后,进行检查的查询语句。推荐设置该属性。默认值为none
      connectionTestQuery: select 1
  # redis 配置
  redis:
    # 数据库
    database: 0
    # 地址
    host: redis.dev
    # 端口,默认为6379
    port: 6379
    # 密码
#    password: 123456
    # 连接超时时间
    timeout: 10s
    lettuce:
      pool:
        # 连接池中的最小空闲连接
        min-idle: 4
        # 连接池中的最大空闲连接
        max-idle: 10
        # 连接池的最大数据库连接数
        max-active: 10
        # 连接池最大阻塞等待时间(使用负值表示没有限制)
        max-wait: -1ms
# MyBatis配置
mybatis:
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapper-locations: classpath*:mapper/*Mapper.xml

2、spring security 使用

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>

页面引入

<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:sc="http://www.thymeleaf.org/extras/spring-security">

${#authentication.principal.menus}

3、文本语法,没有 html 标签

嵌入表达式

[[...]] 相当于 th:text 会转义 HTML-escaped

[(...)] 相当于 th:utext 不会转义

比如 msg 的值是 “<b>你好</b>”

th:text 会执行 html 转义 HTML-escaped,页面结果和值一样:<b>你好</b>

th:utext 不会转义,页面结果是标签执行后的值:你好

也就是说 th:text 原样显示,而 th:utext会把标签解析成html

记录条数:[[${result.total}]]
记录条数:[(${result.total})]

4、标签使用

文本:th:text 或 th:utext

比如 msg 的值是 “<b>你好</b>”

th:text 会执行 html 转义 HTML-escaped,页面结果和值一样:<b>你好</b>

th:utext 不会转义,页面结果是标签执行后的值:你好

也就是说 th:text 原样显示,而 th:utext会把标签解析成html

th:text="${msg}"
th:utext="${msg}"

循环:th:each

        <li th:class="${page} == ${menu.permission} ? 'h' : ''"
            th:each="menu : ${#authentication.principal.menus}"
            th:attr="request=${menu.request}"
            th:text="${menu.name}"></li>

日期格式化

<span th:text="${#dates.format(row.expireDate, 'yyyy-MM-dd')}"></span>
原文地址:https://www.cnblogs.com/yangchongxing/p/12275746.html