首页 > 其他分享 >thymeleaf

thymeleaf

时间:2024-05-09 11:36:52浏览次数:8  
标签:输出 片段 thymeleaf html th 操作 2011

1、通过${}来获取model中的变量,注意这不是el表达式,而是ognl表达式,但是语法非常像

<h2 th:object="${user}">
    <p>Name: <span th:text="*{name}">Jack</span>.</p>
    <p>Age: <span th:text="*{age}">21</span>.</p>
    <p>friend: <span th:text="*{friend.name}">Rose</span>.</p>
</h2>

<h2 th:object="${user}">
    <p>FirstName: <span th:text="*{name.split(' ')[0]}">Jack</span>.</p>
    <p>LastName: <span th:text="*{name.split(' ')[1]}">Li</span>.</p>
</h2>

<span th:text="${#dates.format(today,'yyyy-MM-dd')}">2018-04-25</span>

<span th:text="'thymeleaf'">template</span> //字符串常量值

2、判断

<div th:if="true">
    当条件为true则显示。
</div>

<div th:unless ="false">
    当条件为false 则显示
</div>

 3、进行文本替换

<span th:text="你好"></span>  //不会解析html
<span th:text="你好"></span>  //会解析html

4、switch使用

<div th:switch="${user.name}">
      <p th:case="'ljk'">User is  ljk</p>
      <p th:case="ljk1">User is ljk1</p>
</div>

5、each使用

<tr th:each="user : ${userList}" th:class="${userStat.odd}? 'odd'">
      <td th:text="${user.name}">Onions</td>
      <td th:text="${user.age}">2.41</td>
 </tr>

6、属性设置

//用于声明在标签上class 属性信息(th:class)
<p th:class=" 'even'? 'even' : 'odd'" th:text=" 'even'? 'even' : 'odd'"></p> 

//用于声明html中或自定义属性信息(th:attr)
<img  th:attr="src=@{/images/gtvglogo.png}" /> 输出为<img src="/sbe/images/gtvglogo.png">

//用于声明html中value属性信息(th:value)
<input type="text" th:value="${name}" />

//用于声明html from标签中action属性信息(th:action)
<form action="subscribe.html" th:action="@{/subscribe}">
       <input type="text" name="name" value="abc"/>
</form>
输出结果:
<form action="/sbe/subscribe">
       <input type="text" name="name" value="abc">
</form>

//用于声明htm id属性信息(th:id)
<p th:id="${id}"></p>
输出结果:
<p id="123"></p>

//用于声明htm 中的onclick事件(th:onclick)
<script type="text/javascript">
    function showUserInfo(){
        alert("i am zhuoqianmingyue!")
    }
</script>

<body>
   <p th:onclick="'showUserInfo()'">点我</p>
</body>

//用于声明htm 中的selected属性信息(th:selected)
<select>
    <option name="sex"></option>
    <option th:selected="1 == ${sex}">男</option>
    <option th:selected="0 == ${sex}">女</option>
</select>
输出结果:
<select>
<option name="sex"></option>
    <option selected="selected">男</option>
    <option>女</option>
</select>

//用于thymeleaf 模版页面中局部变量定义的使用(th:with)
<p th:with="df='dd/MMM/yyyy HH:mm'">
        Today is: <span th:text="${#dates.format(today,df)}">13 February 2011</span>
    </p>

<div th:with="firstEle=${users[0]}">
    <p>
        第一个用户的名称是: <span th:text="${firstEle.name}"></span>.
    </p>
</div>

7、Elvis运算符

//Elvis运算可以理解成简单的判断是否为null的三元运算的简写,如果值为nullzhe显示默认值,如果不为null 则显示原有的值
<p>Age: <span th:text="${age}?: '年龄为nll'"></span></p>

8、三元表达式

//我们可以在thymeleaf 的语法中使用三元表达式 具体使用方法是在th:x 中通过 表达式?1选项:2选项
<p th:class=" 'even'? 'even' : 'odd'" th:text=" 'even'? 'even' : 'odd'"></p>
<p th:value="${name eq 'ljk' ? '帅哥':'丑男'}" th:text="${name eq 'ljk' ? '帅哥':'丑男'}"></p>

 9、Elvis运算符

//一种特殊简写操作,当显示的值为null 是就什么都不做
<span th:text="${name} ?: _">no user authenticated</span>

10、如何使用内连操作

我们可以通过 在父标签声明 th:inline="text" 来开启内联操作。当然如果想整个页面使用可以直接声明在body上即可。具体使用方式如下面代码所示。
<div th:inline="text">
<p>Hello, [[${user.name}]]!</p>
</div>
等同于:
<div>
<p>Hello,zhuoqianmingyue!</p>
</div>

[[...]]对应于th:text,[(...)]对应于th:utext

11、禁用内联操作

这我们可以通过在父标签或者本标签上声明th:inline="none"来禁用内联的操作,如下面代码所示:
模版页面

12、JavaScript内联

如果我们想在JavaScript 中使用内联操作,需要在 script 标签上声明 th:inline="javascript" 然后我们就可以 script 标签中使用内联操作了。具体使用方式如下面代码所示:
<script th:inline="javascript">
    var username = [[${user.name}]];
</script>

<script th:inline="javascript">
    var username = "zhuoqianmingyue";
</script>

13、CSS内联

设我们将两个变量设置为两个不同的String值:
classname = 'main elems'
align = 'center'
我们可以像以下一样使用它们:
<style th:inline="css">
    .[[${classname}]] {
      text-align: [[${align}]];
    }
</style>

14、定义footer.html页面 该页面就是我们的引用片段代码

定义footer.html页面 该页面就是我们的引用片段代码
<body>
    <div th:fragment="copy">
        &copy; 2011 The Good Thymes Virtual Grocery
    </div>
</body>
定义引用页面 index.html
<div th:insert="~{footer :: copy}"></div>
通过 th:insert 和 ~{...}片段引用表达式 进行引入footer.html中定义的片段

通过id属性来声明片段
<body>
<div id="copy-section" >
    &copy; 2011 The Good Thymes Virtual Grocery
</div>
</body>

<div th:insert="~{footer :: #copy-section}"></div>

15、th:insert和th:replace(和th:include)之间的区别

th:insert 是最简单的:他会将使用th:insert的标签 和引用片段的内容都显示出来
th:replace 插入引用片段的标签和内容
th:include类似于th:insert,只插入此片段的内容。
<body>
<footer th:fragment="copy">
  &copy; 2011 The Good Thymes Virtual Grocery
</footer>
</body>

<div th:insert="footer2 :: copy"></div>
<div th:replace="footer2 :: copy"></div>
<div th:include="footer2:: copy"></div>

输出结果:
<div>
<footer>
  ? 2011 The Good Thymes Virtual Grocery
</footer>
</div>

<footer>
  ? 2011 The Good Thymes Virtual Grocery
</footer>

<div>
  ? 2011 The Good Thymes Virtual Grocery
</div>

16、带参数的引用片段

<body>
<div th:fragment="frag (onevar,twovar)">
    <p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>
</body>

<body>
    <div th:insert="footer :: frag('a','b')"></div>
</body>

17、用th:remove="all" 删除模版片段

 <tr class="odd" th:remove="all">
    <td>Blue Lettuce</td>
    <td>9.55</td>
    <td>no</td>
    <td>
      <span>0</span> comment/s
    </td>
  </tr>

all:删除包含标记及其所有子标记。
body:不要删除包含标记,但删除其所有子标记。
tag:删除包含标记,但不删除其子项。
all-but-first:删除除第一个之外的所有包含标记的子项。
none: 没做什么。此值对于动态评估很有用。

18、#dates

处理日期数据 生成,转换,获取日期的具体天数 年数。
format操作
<span th:text="${#dates.format(date)}">4564546</span>
获取日期属性操作
<p th:text="${#dates.day(date)} "></p>
生成日期操作
<p th:text="${#dates.createNow()}"></p>
#numbers
处理数字数据的转换。包括:
对不够位数的数字进行补0(formatInteger )<p th:text="${#numbers.formatInteger('123',4)}"></p> 输出<p>0123</p>
设置千位分隔符(formatInteger)<p th:text="${#numbers.formatInteger('1000',2,'POINT')}"></p> 输出:<p>1.000</p>
精确小数点(formatDecimal )<p th:text="${#numbers.formatDecimal('10.123',3,2)}"></p> 输出:<p>010.12</p>
钱显示符号操作 <p th:text="${#numbers.formatCurrency('1000')}"></p> 输出:<p>¥1,000.00</p>
设置百分号(formatPercent )<p th:text="${#numbers.formatPercent('0.2',2, 4)}"></p> 输出:<p>20.0000%</p>
生成数组(sequence ):
<div th:each="num : ${#numbers.sequence(0,4)}" >
          <p th:text="${num}"></p>
</div>

输出:
<div><p>0</p></div>
<div><p>1</p></div> 
<div><p>2</p></div>
<div><p>3</p></div>
<div><p>4</p></div>

19、#strings

处理String的相关操作,包括:
字符串转换(toString)
检查字符串是否为空(isEmpty)
字符串是为空替换操作(defaultString)
检查字符串中是否包含某个字符串(contains containsIgnoreCase)
检查字符串是以片段开头还是结尾(startsWith endsWith)
截取(substring substringAfter)
替换(replace)
追加(prepend append)
变更大小写(toUpperCase toLowerCase)
拆分和组合字符串(arrayJoin arraySplit)
去空格(trim)
缩写文本(abbreviate)
字符串连接(concat)

20、#bools

判断对象是否为ture或者是否为false的操作。
数字 1 为 ture , 0 为 false;
"on" 为 true, "off" 为false;
"true" 为true, "false"为 false;

<p th:text="${#bools.isTrue(1)} "></p> 输出结果:<p>true</p>

21、#arrays

处理数组的相关操作的内置对象,包含:
转换数组 toStringArray toIntegerArray,
获取数组的长度(length ),
判断数组是否为空(isEmpty )
是否包含某个元素(contains)
是否包含一批元素(containsAll)

22、#lists

计算长度(size)
检查list是否为空(isEmpty)
检查元素是否包含在list中(contains,containsAll)
对给定list的副本排序(sort)

23、#sets

转换为Set(toSet)
计算长度(size)
检查set是否为空(isEmpty)
检查元素是否包含在set中 (contains,containsAll)

24、#maps

计算长度(size)
检查map是否为空(isEmpty)
检查映射中是否包含键或值(containsKey,containsAllKeys,containsValue)

25、#aggregates

用户处理集合或者数组的一些统计操作,包括:
求和(sum)
求平均值(avg)
处理包装类型或基本类型的数组或集合

 



标签:输出,片段,thymeleaf,html,th,操作,2011
From: https://www.cnblogs.com/Gourlay/p/17635848.html

相关文章

  • SpringBoot+Thymeleaf渲染下拉框异常解决
    常规方式<selectclass="form-control"name="operationType"th:field="${itemTemp.operationType}"style="width:80%"th:disabled="${readonly}"><optionvalue="">选......
  • 使用springboot+thymeleaf 在html中获取session
    Controllerimportorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.GetMapping;importjavax.servlet.http.HttpSession;@ControllerpublicclassUserController{@GetMappin......
  • thymeleaf模版引擎
    简介Thymeleaf是一个java流行的模版引擎Thymeleaf是一款现代化的服务器端Java模板引擎,适用于Web和独立应用场景。它具备处理HTML、XML、JavaScript、CSS以及纯文本的能力,其核心目标是为开发者提供一种优雅且自然的模板设计方式,使得开发者能够更加便捷地构建、创建和维护结构化且......
  • Thymeleaf SSTI模板注入分析
    环境搭建先搭建一个SpringMVC项目,参考这篇文章,或者参考我以前的spring内存马分析那篇文章https://blog.csdn.net/weixin_65287123/article/details/136648903SpringMVC路由简单写个servletpackagecom.example.controller;importorg.springframework.stereotype.Controlle......
  • Thymeleaf详细教程(SpringBoot版)
    Thymeleaf详细教程(SpringBoot版):https://blog.csdn.net/YuanFudao/article/details/129085281?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171151147816800222817242%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=17115114781......
  • 解决Thymeleaf模板修改不实时更新问题的有效方法
    修改yml文件,thymeleaf中的prefix:file:D:/resources是重点,如果只修改了cache:false也会不生效spring:thymeleaf:#不启用模版缓存cache:false#修改模板存放位置,使用file方式修改模板文件实时生效不需要重新编译prefix:file:D:/resources#如......
  • thymeleaf 用法
    使用注意事项 1.pom中添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>2.application.properties文件的配置#THYME......
  • thymeleaf—02—模板
    一、th:fragment模板片段我们可以使用模板,定义一些会经常复用的代码,使用th:fragment定义然后使用th:insert引入这个模板内容,或者使用th:replace进行内容替换;还有一个th:include标签也是引入模板内容,但是这个不推荐了; 除了增加模板,还可以使用th:remove进行模板的删除操作; ......
  • Spring Boot 3 集成 Thymeleaf
    在现代的Web开发中,构建灵活、动态的用户界面是至关重要的。SpringBoot和Thymeleaf的结合为开发者提供了一种简单而强大的方式来创建动态的Web应用。本文将介绍如何在SpringBoot项目中集成Thymeleaf,并展示一些基本的使用方法。什么是Thymeleaf?Thymeleaf是一款用于Web和独立环境......
  • Spring Boot学习随笔- 第一个Thymeleaf应用(基础语法th:,request、session作用域取值)
    学习视频:【编程不良人】2021年SpringBoot最新最全教程第十五章、ThymeleafThymeleaf是一种现代化的服务器端Java模板引擎,专门用于Web和独立环境。Thymeleaf在有网络和无网络的环境下皆可运行,即可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页......