码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-12-02 19:07:38      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:mode   dep   end   oms   添加引用   calendar   http   rom   login   

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

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

2、同理配置yml

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

3、在需要引用thymeleaf添加引用头

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

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

<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)使用变量动态替换

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

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

//使用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)循环的使用

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

(5)工具方法使用

//日期格式化
${#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/...

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

标签:mode   dep   end   oms   添加引用   calendar   http   rom   login   

原文地址:https://www.cnblogs.com/homehtml/p/11972502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!