springboot学习日志(二)-- thymeleaf学习

本次学习如何使用thymeleaf以及相关语法
一、在上一章写的那样 引入jar包到maven工程css

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

二、同理配置ymlhtml

### springboot配置
spring:
  ##模板设置
  thymeleaf:
    prefix: classpath:/templates
    suffix: .html
    mode: LEGACYHTML5
    encoding: utf-8
    servlet:
      content-type: text/html
    cache: false

三、在须要引用thymeleaf添加引用头spring

<html xmlns:th="http://www.thymeleaf.org">

下面记录一下thymeleaf的模板语法 和jsp稍微有些出入 不过好在不须要修改文件类型 直接将html进行头部引用就能够使用
(1)标签引入路径或地址api

<a th:href="@{http://www.baidu.com}"></a> //绝对路径进行访问
<script th:src="@{/}"></script>//相对路径进行访问
<link rel="stylesheet" th:href="@{css/base.css}">//默认访问static下的文件夹
<img th:src="@{}"/>//图片路径引用

(2)使用变量动态替换springboot

<div th:text="hello ${roms}">hello world</div>

使用spring想也面传值roms:xxx便可在页面汇总替换${roms}进行内容修改
须要注意的是 th:text 会替换掉标签内的全部内容
(3)条件适用less

//使用if进行判断是否为真
<div th:text="hello world" th:if=${flag != true}>Login</div>
//使用unless  表示除外
<div th:text="hello world" th:unless=${flag != true}>Login</div>

(4)循环的使用jsp

<table>
    <tr th:each="list: ${list}">
      <td th:text="${list.id}">1122334</td>
      <td th:text="${plistod.name}">tony</td>
    </tr>
  </table>

(5)工具方法使用maven

//日期格式化
${#dates.format(date, 'yyyy/MMM/dd HH:mm')}
//当前时间
${#dates.createNow()}
//当前日期
${#dates.createToday()}

还有其余的工具类#Calendars,#numbers,#strings,#objects,#bools,#arrays,#lists,#sets,#maps,#aggregates,#messages,#ids
详细的api文档能够查看官网
http://www.thymeleaf.org/doc/...spring-boot

相关文章
相关标签/搜索